forked from ksiondag/gamejam_flowers
/
__main__.py
132 lines (105 loc) · 3.04 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
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
import pygame
import collections
import sys
import colors
import event as e
import terrain as t
import unit as u
import flower as f
import rabbit as r
import manager as m
TITLE = "It's Game Time!"
FPS = 60
def do_nothing( event ):
return
def quit( event ):
pygame.quit()
sys.exit()
def mouse_motion( event ):
result = t.collision( event.pos )
if result is not None:
result.set_highlight()
def mouse_button_down( event ):
result = t.collision( event.pos )
if result is not None:
return
u.Unit( result )
def manage( event ):
if m.listens_for( event.key ):
if m.process( event ):
u.Unit.activate_next()
def key_down( event ):
manage( event )
def trigger_process( event ):
if not event.target:
manage( event )
else:
m.special_delivery( event )
def init():
pygame.init()
m.init()
t.init()
u.init()
def main():
init()
# Setup the screen
size = t.screen_size()
screen = pygame.display.set_mode(size)
pygame.display.set_caption(TITLE)
# Initialize clock
clock = pygame.time.Clock()
# Initialize background
screen.fill(colors.BLACK)
pygame.display.flip()
# Event managing dictionary
# For events we do not process, we are defaulting to doing nothing
process = collections.defaultdict( lambda: do_nothing )
process.update( [
(pygame.QUIT, quit),
#(pygame.MOUSEMOTION, mouse_motion),
(pygame.MOUSEBUTTONDOWN, mouse_button_down),
(pygame.KEYDOWN, key_down)
] )
while True:
dt = clock.tick( FPS ) / 1000.
# Handle events and change state as necessary
for event in pygame.event.get():
process[event.type]( event )
for event in e.Event.get():
trigger_process( event )
# Update all units
for unit in u.all():
unit.update( dt )
flower_count = 0
rabbit_count = 0
for unit in u.all():
if isinstance(unit, f.Flower):
flower_count += 1
if isinstance(unit, r.Rabbit):
rabbit_count +=1;
# Redraw screen
screen.fill(colors.BLACK)
for terrain in t.all():
terrain.draw(screen)
for unit in u.all():
unit.draw(screen)
pygame.display.flip()
if rabbit_count == 0:
pygame.font.init()
font = pygame.font.SysFont("Monospace",100)
win = font.render("You win!!!",1,colors.BLUE)
screen.blit(win,(0,0))#self.terain
pygame.display.flip()
pygame.time.wait(5000)
quit( None )
if flower_count == 0:
pygame.font.init()
font = pygame.font.SysFont("Monospace",100)
lose = font.render("You lose!!!",1,colors.RED)
screen.blit(lose,(0,0))#self.terain
pygame.display.flip()
pygame.time.wait(5000)
quit( None )
if __name__ == '__main__':
main()