forked from mitkrieg/Gravity
/
bar.py
373 lines (322 loc) · 13.5 KB
/
bar.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python
import system
import pygame
from pygame.locals import *
from pygame.sprite import Sprite, Group
from goal import Goal
from user_items import UserPlanet
class Bar(Sprite):
image = None
def __init__(self,x,y,friends,place,image=str('taskbar.png')):
Sprite.__init__(self)
if self.image == None:
self.image = system.load_graphics(image)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.add(friends)
self.group = friends
self.screen = place
self.nextPosition = 0
self.images = [(str('saturny.png')),(str('ring_planet.png')),
(str('earth.png'))]
class ToolBar(Bar):
def __init__(self,x,y,friends,place,other,goal,image=str('taskbar.png')):
Bar.__init__(self,x,y,friends,place,image)
self.lives = Lives(self.rect.x+353,self.rect.y+71,self.group,self.screen,str('5lives.png'))
self.goRect = pygame.Rect(717,658,118,75)
self.barGoal = Goal(self.rect.x+605,self.rect.y+70,self.group)
self.barGoal.resize(40,40)
self.score = Score(250,665,friends,self.screen,2000,26,(0,0,0))
self.arrow = Arrow(872,665,friends,self.screen)
self.menuWidget = MenuWidget(self.rect.x+694,723,self.group,self.screen,self.arrow)
self.itemsTab = ItemsTab(self.rect.x-912,self.rect.y+15,self.group,self.screen,-917,-954,-3)
self.game = other
self.grabbed = None
self.item_one_reset()
self.items_one_limit = 1
self.items_one_placed = 0
self.items_two_limit = 0
self.items_two_placed = 0
self.items_three_placed = 0
self.items_three_limit = 0
self.goal = goal
def lives_over(self):
self.lives.no_lives()
def lives_update(self):
self.lives.update()
def reset_lives(self):
#print "resetting lives"
self.lives.back_to_three(False)
def reset_lives_over(self):
self.lives.back_to_three(True)
def update(self,pos=(0,0)):
if self.grabbed != None:
self.grabbed.update(pos,self)
def clear_grabbed(self):
if self.grabbed != None:
self.grabbed.dropped()
self.grabbed = None
def collision_test(self,pos,player,button):
if self.goRect.collidepoint(pos) and not self.itemsTab.open and not self.menuWidget.open and button == 1:
player.makeANew = True
elif self.goRect.collidepoint(pos) and not self.itemsTab.open and not self.menuWidget.open and button == 3:
return True
elif self.itemsTab.items_rect.collidepoint(pos) and not self.menuWidget.open:
self.grabbed = self.itemsTab
elif self.menuWidget.widget_rect.collidepoint(pos) and not self.itemsTab.open:
self.grabbed = self.menuWidget
elif self.menuWidget.quit_rect.collidepoint(pos):
self.game.quit()
elif self.items_one.collidepoint(pos) and self.itemsTab.open and self.items_one_placed < self.items_one_limit:
x,y = pos
x -= 30
y -= 30
self.grabbed = UserPlanet(x,y,60,60,25,self.game.userPlacedObjects,self.group,self.game.obstacles,self,player,1,self.goal,str('rockplanet.png'))
self.items_one_placed += 1
if self.items_one_placed >= self.items_one_limit:
self.items_one.x = -30
self.itemsTab.rock_item.dark()
elif self.itemsTab.earth_item.rect.collidepoint(pos) and self.itemsTab.open and self.itemsTab.earth_item and self.items_two_placed < self.items_two_limit:
#print "touch earth"
x,y = pos
x -= 30
y -= 30
self.grabbed = UserPlanet(x,y,65,65,30,self.game.userPlacedObjects,self.group,self.game.obstacles,self,player,2,self.goal,str('ringed.png'))
self.items_two_placed += 1
#print self.items_two_placed
if self.items_two_placed >= self.items_two_limit:
self.itemsTab.earth_item.dark()
elif self.itemsTab.venus_item.rect.collidepoint(pos) and self.itemsTab.open and self.itemsTab.venus_item and self.items_three_placed < self.items_three_limit:
#print "touch venus"
x,y = pos
x -= 30
y -= 30
self.grabbed = UserPlanet(x,y,68,68,40,self.game.userPlacedObjects,self.group,self.game.obstacles,self,player,3,self.goal,str('venus.png'))
self.items_three_placed += 1
if self.items_three_placed >= self.items_three_limit:
self.itemsTab.venus_item.dark()
elif self.menuWidget.instructions_rect.collidepoint(pos) and self.menuWidget.open:
self.game.inStructionees()
elif self.menuWidget.save_rect.collidepoint(pos) and self.menuWidget.open:
self.game.saveFile()
return False
def item_one_reset(self):
self.items_one_placed = 0
self.items_one = pygame.Rect(119,667,55,45)
self.itemsTab.rock_item.light()
def item_two_reset(self):
self.items_two_placed = 0
self.itemsTab.earth_item.light()
def item_three_reset(self):
self.items_three_placed = 0
self.itemsTab.venus_item.light()
def next_level(self,level,reset_lives_bool):
#print level-2
self.barGoal.change_image(self.images[level-2])
if level != 3:
self.barGoal.resize(40,40)
else:
self.barGoal.resize(57,40)
self.item_one_reset()
if reset_lives_bool:
self.reset_lives()
self.nextPosition += 1
if level == 2:
self.item_two_reset()
self.items_two_limit = 1
elif level == 3:
self.item_three_reset()
self.items_two_limit = 1
self.items_three_limit = 1
self.item_two_reset()
self.item_one_reset()
elif level == 4:
self.item_three_reset()
self.items_two_limit = 1
self.items_three_limit = 1
self.item_two_reset()
self.item_one_reset()
class Score(Sprite):
def __init__(self,x,y,group,surf,start_score,size,color):
Sprite.__init__(self)
self.group = group
self.surface = surf
self.score = start_score
self.size = size
self.color = color
self.image = system.text_return(str(self.score),self.color,self.size)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.add(group)
def update(self,loss):
self.score += loss
self.image = system.text_return(str(self.score),self.color,self.size)
def reset(self,new_score):
self.score = 0
self.score = new_score
self.update(0)
class ItemsTab(Bar):
def __init__(self,x,y,friends,place,x_open_offset,x_closed_offset,click_offset,image=str('items_tab.png')):
Bar.__init__(self,x,y,friends,place,image)
self.items_rect = pygame.Rect(1,643,40,104)
self.open = False
self.xOpenOffset = x_open_offset
self.xClosedOffset = x_closed_offset
self.clickOffset = click_offset
self.rock_item = RockItem(x+106,652,friends,place)
self.earth_item = EarthItem(x+434,652,friends,place)
self.venus_item = VenusItem(x+720,652,friends,place)
def update(self,pos,other):
newx, newy = pos
if self.rect.x < 2 and self.rect.x >= -912 and not self.open:
self.rect.x = newx-917
self.items_rect.x = newx-self.clickOffset
self.rock_item.rect.x = self.rect.x+106
self.earth_item.rect.x = self.rect.x+422
self.venus_item.rect.x = self.rect.x+720
elif self.rect.x < 2 and self.rect.x >= -912 and self.open:
self.rect.x = newx-954
self.items_rect.x = newx-self.clickOffset
self.rock_item.rect.x = self.rect.x+106
self.earth_item.rect.x = self.rect.x+422
self.venus_item.rect.x = self.rect.x+720
elif self.rect.x > -400:
self.rect.x = 1
self.items_rect.x = 914
self.open = True
other.grabbed = None
self.rock_item.rect.x = self.rect.x+106
self.earth_item.rect.x = self.rect.x+422
self.venus_item.rect.x = self.rect.x+720
elif self.rect.x < -400:
self.rect.x = -912
self.items_rect.x = 7
self.open = False
other.grabbed = None
self.rock_item.rect.x = self.rect.x+106
self.earth_item.rect.x = self.rect.x+422
self.venus_item.rect.x = self.rect.x+720
def dropped(self):
if self.open == False:
self.rect.x = 1
self.items_rect.x = 914
self.open = True
self.rock_item.rect.x = self.rect.x+106
self.earth_item.rect.x = self.rect.x+422
self.venus_item.rect.x = self.rect.x+720
else:
self.rect.x = -912
self.items_rect.x = 1
self.open = False
self.rock_item.rect.x = self.rect.x+106
self.earth_item.rect.x = self.rect.x+422
self.venus_item.rect.x = self.rect.x+720
class MenuWidget(Bar):
def __init__(self,x,y,friends,place,arrow,image=str('widget.png')):
Bar.__init__(self,x,y,friends,place,image)
self.widget_rect = pygame.Rect(x+188,726,27,21)
self.quit_rect = pygame.Rect(x+57,887,99,42)
self.instructions_rect = pygame.Rect(726,1000,149,20)
self.save_rect = pygame.Rect(738,1000,124,35)
self.first_open = False
self.open = False
self.arrow = arrow
def update(self,pos,other,boo = False):
newx, newy = pos
if self.rect.y <= 723 and self.rect.y > 428 and not self.open:
self.rect.y = newy-23
self.widget_rect.y = newy-23
self.quit_rect.y = 654
self.instructions_rect.y = 534
self.save_rect.y = 588
elif self.rect.y <= 723 and self.rect.y > 428 and self.open:
self.rect.y = newy+1
self.widget_rect.y = newy-3
self.quit_rect.y = 1000
self.instructions_rect.y = 1000
self.save_rect.y = 1000
elif self.rect.y > 575:
self.rect.y = 723
self.widget_rect.y = 726
self.open = False
other.grabbed = None
elif self.rect.y < 575:
self.rect.y = 429
self.widget_rect.y = 431
self.open = True
self.quit_rect.y = 654
self.instructions_rect.y = 534
self.save_rect.y = 588
other.grabbed = None
def dropped(self):
if not self.open:
self.rect.y = 429
self.widget_rect.y = 431
self.open = True
self.quit_rect.y = 654
self.instructions_rect.y = 534
self.save_rect.y = 588
if not self.first_open:
self.arrow.kill()
self.first_open = True
else:
self.rect.y = 723
self.widget_rect.y = 726
self.quit_rect.y = 900
self.instructions_rect.y = 1000
self.save_rect.y = 1000
self.open = False
def change_state(self):
if self.open:
self.close_it = True
#print "closing"
if not self.open:
#print "opening"
self.open_it = True
class Lives(Bar):
def __init__(self,x,y,friends,place,image=str('3lives.png')):
Bar.__init__(self,x,y,friends,place,image)
self.next_life = 5
self.five_lives = self.image
self.life_image = [(str('0life.png')),(str('1life.png')),(str('2lives.png')),
(str('3lives.png')),(str('4lives.png')),
(str('5lives.png'))]
def update(self):
self.next_life -= 1
self.image = system.load_graphics(self.life_image[self.next_life])
def reset(self):
self.next_life = 5
self.image = system.load_graphics(self.life_image[self.next_life])
def back_to_three(self,re_group):
self.next_life = 5
self.image = system.load_graphics(self.life_image[self.next_life])
def no_lives(self):
self.kill()
class Arrow(Bar):
def __init__(self,x,y,friends,place,image=str('arrow.png')):
Bar.__init__(self,x,y,friends,place,image)
class Item(Bar):
def __init__(self,x,y,friends,place,image=str('locked_item.png')):
Bar.__init__(self,x,y,friends,place,image)
locked = True
def dark(self):
locked = True
self.image = system.load_graphics(self.img_txt[1])
def light(self):
locked = False
self.image = system.load_graphics(self.img_txt[0])
class RockItem(Item):
def __init__(self,x,y,friends,place,image=str('rock_item.png')):
Item.__init__(self,x,y,friends,place,image)
locked = False
self.img_txt = [(str('rock_item.png')),(str('rock_item_dark.png'))]
class EarthItem(Item):
def __init__(self,x,y,friends,place,image=str('locked_item.png')):
Item.__init__(self,x,y,friends,place,image)
self.img_txt = [str('earth_item.png'),str('earth_item_dark.png')]
class VenusItem(Item):
def __init__(self,x,y,friends,place,image=str('locked_item.png')):
Item.__init__(self,x,y,friends,place,image)
self.img_txt = [(str('venus_item.png')),(str('venus_item_dark.png'))]