-
Notifications
You must be signed in to change notification settings - Fork 0
/
enviroment.py
54 lines (41 loc) · 2.05 KB
/
enviroment.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
import pygame
import random
from settings import *
from predator import *
from prey import *
from tree import *
class Enviroment:
def __init__(self, screen):
self.screen = screen
self.delay_timers = {}
for i in range(TREE_COUNT):
trees.append(Tree([random.randint(0, WINDOW_WIDTH / BLOCK_SIZE) * BLOCK_SIZE, random.randint(0, WINDOW_HEIGHT / BLOCK_SIZE) * BLOCK_SIZE], DARK_GREEN))
for j in range(PREY_COUNT):
preys.append(Prey([random.randint(0, WINDOW_WIDTH / BLOCK_SIZE) * BLOCK_SIZE, random.randint(0, WINDOW_HEIGHT / BLOCK_SIZE) * BLOCK_SIZE], BLOCK_SIZE, GREEN, 10))
for k in range(PREDATOR_COUNT):
predators.append(Predator([random.randint(0, WINDOW_WIDTH / BLOCK_SIZE) * BLOCK_SIZE, random.randint(0, WINDOW_HEIGHT / BLOCK_SIZE) * BLOCK_SIZE], BLOCK_SIZE, RED, 10))
def draw_grid(self):
for x in range(0, WINDOW_WIDTH, BLOCK_SIZE):
for y in range(0, WINDOW_HEIGHT, BLOCK_SIZE):
rect = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(self.screen, BLACK, rect, 1)
def delay(self, action_name, delay_duration):
current_time = pygame.time.get_ticks()
if action_name not in self.delay_timers:
self.delay_timers[action_name] = current_time
if current_time - self.delay_timers[action_name] >= delay_duration:
# Add to a dictionary the name of the timer or update its time
self.delay_timers[action_name] = current_time
return True
return False
def update(self):
self.screen.fill(WHITE)
self.draw_grid()
for prey in preys:
prey.update(self.screen)
for predator in predators:
predator.update(self.screen)
for tree in trees:
tree.draw(self.screen)
if self.delay("tree", 10000):
trees.append(Tree([random.randint(0, WINDOW_WIDTH / BLOCK_SIZE) * BLOCK_SIZE, random.randint(0, WINDOW_HEIGHT / BLOCK_SIZE) * BLOCK_SIZE], DARK_GREEN))