-
Notifications
You must be signed in to change notification settings - Fork 0
/
food.py
30 lines (25 loc) · 1015 Bytes
/
food.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
import random
class Food:
def __init__(self, block_size, bounds, lifetime=1e8):
self.color = (150, 0, 0)
self.x, self.y = 0, 0
self.block_size = block_size
self.bounds = bounds
self.lifetime = lifetime
self.time_left = lifetime
self.respawn()
def draw(self, game, window):
game.draw.rect(window, self.color, (self.x, self.y, self.block_size, self.block_size))
def respawn(self):
self.time_left = self.lifetime
blocks_in_x = (self.bounds[0]) / self.block_size
blocks_in_y = (self.bounds[1]) / self.block_size
self.x = random.randint(0, blocks_in_x - 1) * self.block_size
self.y = random.randint(0, blocks_in_y - 1) * self.block_size
def update(self):
if random.random() < 0.01:
self.color = tuple([random.randint(30, 220) for _ in range(3)])
self.time_left -= 1
if self.time_left < 0:
self.time_left = self.lifetime
self.respawn()