-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
209 lines (159 loc) · 5.24 KB
/
Game.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
#!/usr/bin/python
import sys
import os
import math
import random
import pygame
import MainMenu
import Resources
from pygame.locals import *
from Rabbit import *
from Butterfly import *
from Animation import *
from Object import *
from Resources import *
from PauseGameMenu import *
from Map import *
from GameToolbar import *
class Game():
pygame.mixer.pre_init(44100, -16, 2, 1024)
pygame.mixer.init()
def __init__(self, color1, color2, levelPreset = "empty"):
self.butterflies = []
self.screen = pygame.display.get_surface()
self.backgroundImage, self.backgroundRect = loadPNG("background.png")
self.buttonSound = pygame.mixer.Sound("resources/sound/button.wav")
self.buttonSound.set_volume(float(Resources.getOptionValue("sound"))/100)
self.active = True
if levelPreset == "empty":
self.level = Map()
else:
self.level = Map(True)
self.toolbar = GameToolbar()
self.regis = Rabbit(1, "regis", color1, self.level.objectList, self.level.objectSpritesList)
self.john = Rabbit(2, "john", color2, self.level.objectList, self.level.objectSpritesList)
self.john.appendRabbit(self.regis)
self.regis.appendRabbit(self.john)
self.pauseMenu = PauseGameMenu()
self.deltaCarrot = 0
self.timeCarrot = random.randint(1, 4)
for l in range(0, 6):
while True:
randPos = random.randint(0, 16)
if not self.level.isInBlock(self.level.objectList[randPos].getX() + 10, self.level.objectList[randPos].getY() - 26):
break
butterfly = Butterfly(self.level.objectList[randPos].getX() + 10, self.level.objectList[randPos].getY() - 26, (255, 10, 100), self.level.objectList, self.level.objectSpritesList)
self.butterflies.append(butterfly)
pygame.display.flip()
def update(self):
key = pygame.key.get_pressed()
if self.active:
pygame.mouse.set_visible(0)
for event in pygame.event.get():
if event.type == QUIT or (key[K_F4] and key[K_LALT]):
return False, self
elif event.type == MOUSEMOTION and (key[K_LSHIFT] or key[K_LCTRL]):
mse = pygame.mouse.get_pos()
if not any(obj.rect.collidepoint(mse) for obj in self.level.objectList):
x = (int(mse[0]) / 50)*50
y = (int(mse[1]) / 50)*50
if key[K_LSHIFT]:
self.level.addObject(x, y, "earth")
else:
self.level.addObject(x, y, "boing")
elif event.type == MOUSEMOTION and key[K_LALT]:
mse = pygame.mouse.get_pos()
self.level.removeObjectFromPos(mse)
elif event.type == KEYDOWN:
#KEYS FOR JOHN
if event.key == K_UP:
self.john.jump()
if event.key == K_LEFT:
self.john.moveLeftStart()
if event.key == K_RIGHT:
self.john.moveRightStart()
if event.key == K_KP0:
self.john.throwCarrot()
#KEYS FOR REGIS
if event.key == K_w:
self.regis.jump()
if event.key == K_a:
self.regis.moveLeftStart()
if event.key == K_d:
self.regis.moveRightStart()
if event.key == K_e:
self.regis.throwCarrot()
if event.key == K_c:
self.level.addCarrot()
if event.key == K_ESCAPE:
self.active = False
self.john.moveLeftStop()
self.john.moveRightStop()
self.regis.moveLeftStop()
self.regis.moveRightStop()
elif event.type == KEYUP:
if event.key == K_LEFT:
self.john.moveLeftStop()
if event.key == K_RIGHT:
self.john.moveRightStop()
if event.key == K_a:
self.regis.moveLeftStop()
if event.key == K_d:
self.regis.moveRightStop()
#IF A RABBIT IS TOUCHED
elif event.type == USEREVENT + 1:
print "touche"
if self.john.isTouched():
self.john.moveLeftStop()
self.john.moveRightStop()
elif self.regis.isTouched():
self.regis.moveLeftStop()
self.regis.moveRightStop()
elif event.type == USEREVENT + 2:
print "plus touche"
self.screen.blit(self.backgroundImage, self.backgroundRect, self.backgroundRect)
#LEVEL UPDATE
self.level.update()
#RABBITS UPDATE
self.john.update()
self.regis.update()
#TOOLBAR UPDATE
self.toolbar.update(self.john, self.regis)
#BUTTERFLIES UPDATE
for b in self.butterflies:
b.update()
#NEW CARROTS
if(self.deltaCarrot == self.timeCarrot * 3600):
self.level.addCarrot()
self.deltaCarrot = 0
self.timeCarrot = random.randint(1, 4)
else:
self.deltaCarrot += 1
else:
pygame.mouse.set_visible(1)
for event in pygame.event.get():
if event.type == QUIT or (key[K_F4] and key[K_LALT]):
return False, self
elif event.type == MOUSEBUTTONDOWN:
mse = pygame.mouse.get_pos()
if self.pauseMenu.buttons["resume"].onButton(mse):
self.buttonSound.play()
self.active = True
elif self.pauseMenu.buttons["loadlevel"].onButton(mse):
self.buttonSound.play()
print "okay"
elif self.pauseMenu.buttons["mainMenu"].onButton(mse):
self.buttonSound.play()
return True, MainMenu.MainMenu()
elif event.type == MOUSEMOTION:
mse = pygame.mouse.get_pos()
pygame.mouse.set_cursor(*pygame.cursors.arrow)
for button in self.pauseMenu.buttons.values():
if button.onButton(mse):
pygame.mouse.set_cursor(*pygame.cursors.tri_left)
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
self.active = True
self.pauseMenu.update()
pygame.display.update()
return True, self