-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
291 lines (220 loc) · 9.63 KB
/
main.py
File metadata and controls
291 lines (220 loc) · 9.63 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
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
'''Title: Recycling Game
Team: nullptr
Members: Jessie Kuo, Brandon Phan, Hannah Dinh, An Hoang'''
import random
import pygame
from variables import *
# Initialize Pygame
pygame.init()
pygame.display.set_caption('Recycling Game')
font = pygame.font.SysFont('papyrus', 38, True)
bigFont = pygame.font.SysFont('papyrus', 30, True)
#icon
gameIcon = pygame.image.load('img/icon.png')
pygame.display.set_icon(gameIcon)
#Functions: ?????????
class Item(pygame.sprite.Sprite):
playerWidth = 0
playerHeight = 0
def __init__(self, x, y, scale):
super().__init__()
# #if no more items in availableList, then win game
# if (len(availableList) < 1): #pick random item to load, then remove from availableList
# win = True
# gameOver = True #TODO
# else:
if not availableList:
gameOver = True
self.item = None
return # Exit the __init__ early if the game is won
index = random.randint(0, len(availableList)-1)
#print("aaa = ", index)
itemImage = pygame.image.load(availableList[index][2])
self.itemAnswers = availableList[index][1]
self.label = availableList[index][3]
self.index = index
self.item = pygame.transform.scale(itemImage, (int(itemImage.get_width()*scale),
int(itemImage.get_height()*scale)))
self.playerWidth = itemImage.get_width()*scale
self.playerHeight = itemImage.get_height()*scale
self.rect = self.item.get_rect() #current position of item
def getAnswers(self):
if (self.item):
return self.itemAnswers
def move(self):
#TODO: implement drag and drop
key = pygame.key.get_pressed()
#TODO: implement collision detection
def draw(self):
if (self.item):
screen.blit(self.item, (SCREEN_WIDTH/2-int(self.item.get_width()/2),
SCREEN_HEIGHT/4 - int(self.item.get_height()/2)))
#def update(self):
#maybe later
class Bin(pygame.sprite.Sprite):
binWidth = 0
binHeight = 0
def __init__(self, binNumber, x, scale):
super().__init__()
self.binNumber = binNumber
bin = pygame.image.load(binList[binNumber-1][2])
self.bin = pygame.transform.scale(bin, (int(bin.get_width()*scale),
int(bin.get_height()*scale)))
self.binWidth = bin.get_width()*scale
self.binHeight = bin.get_height()*scale
self.rect = self.bin.get_rect() #current position of bin
self.xBin = x
def draw(self):
screen.blit(self.bin, (self.xBin,
SCREEN_HEIGHT*3/4 - int(self.bin.get_height()/2)))
class Button():
def __init__(self, x, y, width, height, buttonText='Button', onclickFunction=None, onePress=False):
self.x = x
self.y = y
self.width = width
self.height = height
self.onclickFunction = onclickFunction
self.onePress = onePress
self.alreadyPressed = False
#Can change this
self.fillColors = {
'normal': 'white',
'hover': 'grey',
'pressed': 'black',
}
self.buttonSurface = pygame.Surface((self.width, self.height))
self.buttonRect = pygame.Rect(self.x, self.y, self.width, self.height)
self.buttonSurf = font.render(buttonText, True, (20, 20, 20))
objects.append(self)
def process(self):
mousePos = pygame.mouse.get_pos()
#button fill color
self.buttonSurface.fill(self.fillColors['normal'])
if self.buttonRect.collidepoint(mousePos): #if mouse on button
self.buttonSurface.fill(self.fillColors['hover']) #change to hover color
if pygame.mouse.get_pressed(num_buttons=3)[0]:
self.buttonSurface.fill(self.fillColors['pressed'])
if self.onePress: #if pressed
self.onclickFunction()
elif not self.alreadyPressed:
self.onclickFunction()
self.alreadyPressed = True
else:
self.alreadyPressed = False
self.buttonSurface.blit(self.buttonSurf, [
self.buttonRect.width/2 - self.buttonSurf.get_rect().width/2,
self.buttonRect.height/2 - self.buttonSurf.get_rect().height/2])
screen.blit(self.buttonSurface, self.buttonRect)
def myFunction(self):
print('Open Option Menu')
#Below is example of multipress button
#Button(30, 140, 400, 100, 'Button Two (multiPress)', Button.myFunction, True)
#define entities
#optionsButton = Button(optionsBoxCoord[0], optionsBoxCoord[1],
# optionsBoxCoord[2], optionsBoxCoord[3], 'Options', Button.myFunction)
myItem = Item(itemx, itemy - 400, .1)
###### NEED TO CHANGE TRASH BIN X-POSITION FOR DIFFERENT SCREEN SIZE
trashBin = Bin(1,10,0.12)
recycleBin = Bin(2,SCREEN_WIDTH/2-120,0.12)
compostBin = Bin(3,SCREEN_WIDTH-275,0.12)
run = True
while run:
screen.fill('#D1FFBD')
myItem.draw()
trashBin.draw()
recycleBin.draw()
compostBin.draw()
#process objects
for object in objects:
object.process()
#draw score box
pygame.draw.rect(screen, 'black', (textBoxCoord[0], textBoxCoord[1],
textBoxCoord[2], textBoxCoord[3]), 5)
#draw option box
#pygame.draw.rect(screen, 'white', (optionsBoxCoord[0], optionsBoxCoord[1],
# optionsBoxCoord[2], optionsBoxCoord[3]), 3)
#event handling
for event in pygame.event.get():
if (hearts_num <1):
availableList = defaultItemList[:] #reset availableList
gameOver = True
# Key pressed
if event.type == pygame.KEYDOWN:
# Check if the key pressed is 1, 2 or 3 and game is not over
if event.key in key_to_bin_mapping and not gameOver:
bin_index = key_to_bin_mapping[event.key]
if myItem.getAnswers() is None:
win = True
print("Congrats you win")
availableList = defaultItemList[:] #reset availableList
gameOver = True
elif binList[bin_index][1] in myItem.getAnswers():
score +=1
availableList.pop(myItem.index)
else:
hearts_num -= 1
if (availableList):
myItem = Item(itemx, itemy - 400, .1)
myItem.draw()
else:
win = True
print("Congrats you win")
availableList = defaultItemList[:] #reset availableList
gameOver = True
# Space to replay
elif event.key == pygame.K_SPACE and gameOver:
# Reset variables for new game
hearts_num = maxHearts
gameOver = False
win = False
# Set new high score
if score > highScore:
highScore = score
score = 0
if event.type == pygame.QUIT:
run = False
if gameOver:
if win:
gameOverText = bigFont.render('You Win!', True, 'white', 'black')
else:
gameOverText = bigFont.render('Game Over!', True, 'white', 'black')
#Create end game table
pygame.draw.rect(screen, 'black', (SCREEN_WIDTH*1/3,SCREEN_HEIGHT/3,SCREEN_WIDTH*1/3,SCREEN_HEIGHT*1/3-30))
endScoreText = bigFont.render('Score: ' + str(score) + '/' + str(len(defaultItemList)), True, 'white', 'black')
replayText = bigFont.render('[Space] to Replay', True, 'white', 'black')
gameOverTextRect = gameOverText.get_rect()
endScoreTextRect = endScoreText.get_rect()
replayTextRect = replayText.get_rect()
gameOverTextRect.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2 - 65)
endScoreTextRect.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2 - 15)
replayTextRect.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 25)
screen.blit(gameOverText, gameOverTextRect)
screen.blit(endScoreText, endScoreTextRect)
screen.blit(replayText, replayTextRect)
#item label text
if myItem:
labelText = font.render(myItem.label, True, 'black', None)
#labelText = pygame.transform.scale(labelText,(100,40))
labelTextRect = labelText.get_rect()
labelTextRect.topleft = (SCREEN_WIDTH/2 - (labelText.get_width()/2), SCREEN_HEIGHT/2 - (labelText.get_height()/2))
if not gameOver:
screen.blit(labelText, labelTextRect)
#score text
scoreText = font.render('Score: ' + str(score), True, '#006600', None)
# scoreText = pygame.transform.scale(scoreText,(100,40))
scoreTextRect = scoreText.get_rect()
scoreTextRect.topleft = (SCREEN_WIDTH/22, 35)
screen.blit(scoreText, scoreTextRect)
textBoxCoord = (25, 25, scoreText.get_width() + 20, scoreText.get_height()+10)
#high score
highScoreText = font.render('High Score: ' + str(highScore), True, 'white', 'black')
highScoreText = pygame.transform.scale (highScoreText, (140,50))
highScoreTextRect = highScoreText.get_rect()
highScoreTextRect.topleft = (SCREEN_WIDTH-highScoreText.get_width(), SCREEN_HEIGHT-highScoreText.get_height())
screen.blit(highScoreText, highScoreTextRect)
#Hearts
scaled_heart = pygame.transform.scale(heart_images.get(hearts_num, heart_images[0]), (150, 50))
screen.blit(scaled_heart, (SCREEN_WIDTH*4/5, 20))
#update display and clock
pygame.display.update()
pygame.quit()