-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (94 loc) · 3.57 KB
/
main.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
import pygame
import os
import random as rnd
import copy
import pandas as pd
from misc import Position
from misc import OccupiedArea
import world_parameters
wp = world_parameters.Params()
import beings
class SceneBase(object):
def __init__(self):
self.next = self
def ProcessInput(self, events, pressed_keys):
print("uh-oh, you didn't override this in the child class")
def Update(self):
print("uh-oh, you didn't override this in the child class")
def Render(self, screen):
print("uh-oh, you didn't override this in the child class")
def SwitchToScene(self, next_scene):
self.next = next_scene
def Terminate(self):
self.SwitchToScene(None)
def run_game(width, height, fps, starting_scene):
pygame.init()
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
active_scene = starting_scene
while active_scene != None:
pressed_keys = pygame.key.get_pressed()
# Event filtering
filtered_events = []
for event in pygame.event.get():
quit_attempt = False
if event.type == pygame.QUIT:
quit_attempt = True
elif event.type == pygame.KEYDOWN:
alt_pressed = pressed_keys[pygame.K_LALT] or \
pressed_keys[pygame.K_RALT]
if event.key == pygame.K_ESCAPE:
quit_attempt = True
elif event.key == pygame.K_F4 and alt_pressed:
quit_attempt = True
if quit_attempt:
active_scene.Terminate()
else:
filtered_events.append(event)
active_scene.ProcessInput(filtered_events, pressed_keys)
active_scene.Update()
active_scene.Render(screen)
active_scene = active_scene.next
pygame.display.flip()
clock.tick(fps)
class TitleScene(SceneBase):
def __init__(self):
SceneBase.__init__(self)
def ProcessInput(self, events, pressed_keys):
for event in events:
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
# Move to the next scene when the user pressed Enter
self.SwitchToScene(GameScene())
def Update(self):
pass
def Render(self, screen):
# For the sake of brevity, the title scene is a blank red screen
screen.fill((255, 0, 0))
class GameScene(SceneBase):
def __init__(self):
SceneBase.__init__(self)
self.active_beings = {'fungi': []}
def ProcessInput(self, events, pressed_keys):
for event in events:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
fungus = beings.Fungus()
if fungus.alive == True:
self.active_beings['fungi'].append(fungus)
def Update(self):
active_beings_copy = copy.deepcopy(self.active_beings)
#print "active beings:", len(self.active_beings['fungi'])
for being in active_beings_copy['fungi']:
newBeing = being.attemptToMultiply()
if newBeing == None:
continue
elif newBeing.alive == True:
self.active_beings['fungi'].append(newBeing)
else:
pass
if not being.alive:
print "being is dead"
def Render(self, screen):
screen.fill((0, 0, 0))
for being in self.active_beings['fungi']:
being.drawSelf(screen)
run_game(wp.width, wp.height, wp.fps, TitleScene())