-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodies.py
More file actions
101 lines (85 loc) · 3.51 KB
/
Goodies.py
File metadata and controls
101 lines (85 loc) · 3.51 KB
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
# Goodie and GoddieMgr classes
import pygame
import pygwidgets
import random
from Constants import *
class Goodie():
MIN_SIZE = 10
MAX_SIZE = 40
MIN_SPEED = 1
MAX_SPEED = 8
# Load the image once
GOODIE_IMAGE = pygame.image.load('images/goodie.png')
RIGHT = 'right'
LEFT = 'left'
def __init__(self, window):
self.window = window
size = random.randrange(Goodie.MIN_SIZE, Goodie.MAX_SIZE + 1)
self.y = random.randrange(0, GAME_HEIGHT - size)
self.direction = random.choice([Goodie.LEFT, Goodie.RIGHT])
if self.direction == Goodie.LEFT: # start on right side of the window
self.x = WINDOW_WIDTH
self.speed = - random.randrange(Goodie.MIN_SPEED,
Goodie.MAX_SPEED + 1)
self.minLeft = - size
else: # start on left side of the window
self.x = 0 - size
self.speed = random.randrange(Goodie.MIN_SPEED,
Goodie.MAX_SPEED + 1)
self.image = pygwidgets.Image(self.window,
(self.x, self.y), Goodie.GOODIE_IMAGE)
percent = int((size * 100) / Goodie.MAX_SIZE)
self.image.scale(percent, False)
def update(self):
self.x = self.x + self.speed
self.image.setLoc((self.x, self.y))
if self.direction == Goodie.LEFT:
if self.x < self.minLeft:
return True # needs to be deleted
else:
return False # stays in window
else:
if self.x > WINDOW_WIDTH:
return True # needs to be deleted
else:
return False # stays in window
def draw(self):
self.image.draw()
def collide(self, playerRect):
collidedWithPlayer = self.image.overlaps(playerRect)
return collidedWithPlayer
class GoodieMgr():
GOODIE_RATE_LO = 90
GOODIE_RATE_HI = 111
def __init__(self, window):
self.window = window
self.reset()
def reset(self): # Called when starting a new game
self.goodiesList = []
self.nFramesTilNextGoodie = GoodieMgr.GOODIE_RATE_HI
def update(self, thePlayerRect):
# Tell each Goodie to update itself.
# If a Goodie goes off an edge, remove it
# Count up all Goodies that contact the player and remove them.
nGoodiesHit = 0
goodiesListCopy = self.goodiesList.copy()
for oGoodie in goodiesListCopy:
deleteMe = oGoodie.update()
if deleteMe:
self.goodiesList.remove(oGoodie) # remove this Goodie
elif oGoodie.collide(thePlayerRect):
self.goodiesList.remove(oGoodie) # remove this Goodie
nGoodiesHit = nGoodiesHit + 1
# If the correct amount of frames have passed,
# add a new Goodie (and reset the counter)
self.nFramesTilNextGoodie = self.nFramesTilNextGoodie - 1
if self.nFramesTilNextGoodie == 0:
oGoodie = Goodie(self.window)
self.goodiesList.append(oGoodie)
self.nFramesTilNextGoodie = random.randrange(
GoodieMgr.GOODIE_RATE_LO,
GoodieMgr.GOODIE_RATE_HI)
return nGoodiesHit # return number of Goodies that contacted player
def draw(self):
for oGoodie in self.goodiesList:
oGoodie.draw()