-
Notifications
You must be signed in to change notification settings - Fork 2
/
objects.py
235 lines (175 loc) · 6.63 KB
/
objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import time
import random
import math
from decimal import Decimal
import pygame
from pygame.locals import *
from pygame.math import Vector2
from geometry import calculateIntersectPoint;
class Entity(object):
def draw(self, window): # rendering tick
pass
def update(self, collisions=[]): # logic/physics tick
pass
def handle(self, event): # event tick
pass
class Timer(Entity):
def __init__(self):
self.is_paused = False
self.__elapsed = Decimal(0)
self.__last = self.now()
def now(self):
return Decimal(time.perf_counter())
def elapsed(self):
now = self.now() # avoid losing time by caching
if not self.is_paused:
self.__elapsed += now - self.__last
self.__last = now
return self.__elapsed
def consume(self, time=None):
if time:
self.__elapsed -= Decimal(time)
else:
self.__elapsed = 0
def clear(self, time=None):
if time:
self.__elapsed %= time
else:
self.__elapsed = 0
def pause(self):
self.is_paused = True
def resume(self):
self.is_paused = False
class Ecosystem(Entity):
def __init__(self, surface, food_rate=5, food_max=20):
self.food_rate = food_rate
self.food_max = food_max
self.limits = Vector2(surface.get_width(), surface.get_height())
self.food_timer = Timer()
self.foods = {}
self.make_food()
def draw(self, window):
for f in self.foods.values():
f.draw(window)
def update(self, collisions=[]):
if self.food_timer.elapsed() > self.food_rate:
self.food_timer.consume(self.food_rate)
self.make_food()
def make_food(self):
if len(self.foods) < self.food_max:
point = self.randpoint()
while (int(point.x), int(point.y)) in self.foods:
point = self.randpoint()
size = random.randint(4, 8)
self.foods[(int(point.x), int(point.y))] = Food(point.x, point.y, size)
def randpoint(self):
return Vector2(random.randint(0, self.limits.x), random.randint(0, self.limits.x))
def check_eaten(self, vector_point):
new_foods = {}
eaten = 0
for pos, f in self.foods.items():
if not f.is_inside(vector_point):
new_foods[pos] = f
else:
eaten += math.pi * f.radius ** 2
self.foods = new_foods
return eaten
class Food(Entity):
def __init__(self, x, y, radius=6):
self.radius = radius
self.position = Vector2(x, y)
def draw(self, window, patterns=set()):
self.draw_circle(window, *self.position, self.radius)
self.draw_circle(window, *self.position, self.radius - 1, Color('blue'))
def draw_circle(self, window, x, y, radius, color=Color('white')):
pygame.draw.circle(
window.get_surface(),
color,
[int(val) for val in [x, y]],
radius
)
def is_inside(self, vector_point):
return self.position.distance_to(vector_point) <= self.radius + 5
def update(self, collisions=[]):
pass
class Snake(Entity):
def __init__(self, surface, x, y, keys, movescale=3.5):
self.position = Vector2(x, y)
self.velocity = Vector2(1, 0.5)
self.velocity.scale_to_length(2)
self.keys = keys
self.movescale = movescale
self.limits = Vector2(surface.get_width(), surface.get_height())
self.max_length = 100
self.segments = [self.position]
self.init_themes()
def init_themes(self):
mono = ['white']
basic = ['yellow']
poison = ['yellow', 'yellow', 'green', 'yellow', 'green', 'yellow']
rainbow = ['green', 'yellow', 'orange', 'red', 'violet', 'blue']
scarlet_king = ['red', 'red', 'red', 'red', 'red', 'black', 'black', 'yellow', 'yellow', 'yellow', 'black', 'black']
coral_snake = ['white', 'white', 'black', 'red', 'red', 'red', 'black']
sea_krait = ['white', 'black', 'black', 'white', 'white', 'white']
python = ['0x366E9D', '0x366E9D', '0x366E9D', '0xFFCE3E', '0xFFCE3E', '0xFFCE3E']
self.themes = [mono, basic, poison, python, sea_krait, coral_snake, scarlet_king, rainbow]
self.theme = mono
self.theme_timer = Timer()
self.theme_index = 0
def draw(self, window):
colors = [pygame.Color(c) for c in self.theme]
c_i = 0
for i in range(len(self.segments) - 1):
pygame.draw.circle(
window.get_surface(),
colors[c_i],
[int(val) for val in self.segments[i]],
5
)
c_i = (c_i + 1) % len(colors)
def update(self, collisions=[]):
pressed = pygame.key.get_pressed()
offset_x = -pressed[self.keys['left']] +pressed[self.keys['right']]
self.velocity.rotate_ip(self.movescale * offset_x)
next_pos = self.position + self.velocity
if self.head().distance_to(next_pos) > 1:
self.segments.insert(0, next_pos)
if self.length() >= self.max_length:
self.segments.pop()
if self.theme_timer.elapsed() > 20:
self.theme_index +=1
self.theme_timer.consume(20)
self.theme_index %= len(self.themes)
self.theme = self.themes[self.theme_index]
self.position += self.velocity
def length(self):
accumulator = 0
for i in range(len(self.segments) - 1):
accumulator += self.segments[i].distance_to(self.segments[i+1])
return accumulator
def head(self):
return self.segments[0]
# TODO: Linear Algebra
def overlaps(self):
s = self.segments
if len(self.segments) > 5:
for i in range(4, len(s) - 1):
if calculateIntersectPoint(s[0], s[1], s[i], s[i+1]):
return True
return False
class NumberDisplay(Entity):
def __init__(self, x, y, amount=0, color='white', size=48, label='', center=False):
self.position = Vector2(x, y)
self.color = color
self.size = size
self.amount = amount
self.label = label
self.center = center
def draw(self, window):
window.set_font_size(self.size)
window.set_font_color(self.color)
text = self.label + str(round(self.amount, 1))
x_pos = self.position.x
if self.center:
x_pos = self.position.x - window.get_string_width(text) / 2
window.draw_string(text, x_pos, self.position.y)