-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrash Sorter2.py
296 lines (222 loc) · 8.34 KB
/
Trash Sorter2.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
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 4 21:09:39 2021
@author: apeng
"""
import pygame
import os, sys
from pygame import mixer
import random
pygame.font.init()
WIDTH, HEIGHT = 900,500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("SmartSorter")
BIN_FONT = pygame.font.SysFont('comicsans', 30)
WHITE = (255,255,255)
GREEN = (0,192,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
GREY = (128,128,128)
SILVER = (192,192,192)
PURPLE = (75,0,130)
BACKGROUND = pygame.image.load((os.path.join('Assets','field3.png'))) #house
FPS = 60
RECYCLE_BIN = pygame.image.load(os.path.join('Assets','RECYCLING BIN.png'))
RECYCLE_BIN = pygame.transform.scale(RECYCLE_BIN, (125,140))
TRASH_BIN = pygame.image.load(os.path.join('Assets','TRASH BIN.png'))
TRASH_BIN = pygame.transform.scale(TRASH_BIN, (110,150))
COMPOST_BIN = pygame.image.load(os.path.join('Assets','COMPOST BIN.png'))
COMPOST_BIN = pygame.transform.scale(COMPOST_BIN, (110,153))
RECYCLE_BIN_X_ORI = 185
RECYCLE_BIN_Y_ORI = 103
TRASH_BIN_X_ORI = 400
TRASH_BIN_Y_ORI = 100
COMPOST_BIN_X_ORI = 600
COMPOST_BIN_Y_ORI = 100
SMALL_BONES = pygame.image.load(os.path.join('Assets','SmallBones.png'))
PLASTIC = pygame.image.load(os.path.join('Assets','Non-Foods.png'))
def load_sprite(path, image_filename, xsize, ysize):
sprite = pygame.image.load(os.path.join('Assets/' + path, image_filename))
sprite = pygame.transform.scale(sprite, (xsize ,ysize))
return sprite
item = ''
whichbin =''
RECYCLEBIN = ['cardboardbox','cerealbox','magazines','milk','newspaper','shampoo','sodacan','waterbottle','winebottle','fabricsoftener']
TRASHBIN = ['candywrapper','chipbag','cup','takeoutbox','ketchup','straws','plasticbag','plasticplate','napkin','shoppingbag']
COMPOSTBIN = ['bananapeel', 'leaf','apple','leftovers','pizzabox','pizzacrust','sandwich','stick','eggcarton','toiletpaperroll']
#sprite = pygame.image.load(os.path.join('Assets/' + whichbin,item + '.png'))
def randomNum():
binType = random.randint(1,3)
itemType = random.randint(0,9)
if binType == 1:
whichbin = 'recycle'
item = RECYCLEBIN[itemType]
elif binType == 2:
whichbin = 'trash'
item = TRASHBIN[itemType]
else:
whichbin = 'compost'
item = COMPOSTBIN[itemType]
return item, whichbin
def gameEnd(score):
print(score)
if score >= 75:
sprite = load_sprite('', 'youwin.jpg', 200, 200)
elif score <= -25:
sprite = load_sprite('', 'youlose.jpg', 200, 200)
else:
return score
WIN.fill(BLACK)
print('here')
#draw lose or win screens
WIN.blit(sprite, (350, 100))
pygame.display.update()
pygame.time.wait(3000)
score = 0
return score
def scoring(item,whichbin, score):
if check(item, whichbin):
score = score + 10
else:
score = score - 5
return score
# score_text = BIN_FONT.render(str(score+n), 1, 'BLACK')
# WIN.blit(score_text,(420,25))
# pygame.display.update()
def addScore(score,n):
# score_text = BIN_FONT.render(str(score+n), 1, 'BLACK')
# WIN.blit(score_text,(420,25))
# pygame.display.update()
return score+n
def check(item,whichbin):
if whichbin == 'recycle':
if item in RECYCLEBIN:
return True
else:
return False
if whichbin == 'trash':
if item in TRASHBIN:
return True
else:
return False
if whichbin == 'compost':
if item in COMPOSTBIN:
return True
else:
return False
def soundEffect(item, whichbin):
if check(item, whichbin) == True:
mixer.init()
correct = os.path.join('Assets', 'correct.mp3')
mixer.music.load(correct)
mixer.music.set_volume(0.7)
mixer.music.play()
else:
mixer.init()
wrong = os.path.join('Assets','wrong.mp3')
mixer.music.load(wrong)
mixer.music.set_volume(0.7)
mixer.music.play()
def draw_window2(x, y, trash_sprite, score, item):
#WIN.fill(SILVER)
WIN.blit(BACKGROUND, (0,0))
#draw bins
WIN.blit(RECYCLE_BIN, (RECYCLE_BIN_X_ORI,RECYCLE_BIN_Y_ORI))
WIN.blit(TRASH_BIN, (TRASH_BIN_X_ORI,TRASH_BIN_Y_ORI))
WIN.blit(COMPOST_BIN, (COMPOST_BIN_X_ORI,COMPOST_BIN_Y_ORI))
#make label
sprite_text = BIN_FONT.render(item,1, PURPLE)
WIN.blit(sprite_text, (50,450))
#label bins
recycling_text = BIN_FONT.render("Recycling", 1, BLUE)
trash_text = BIN_FONT.render("Landfill", 1, GREY)
compost_text = BIN_FONT.render("Compost", 1, GREEN)
WIN.blit(recycling_text, (215,75))
WIN.blit(trash_text, (420,75))
WIN.blit(compost_text, (615,75))
#draw trash
WIN.blit(trash_sprite, (x, y))
# display score
score_text = BIN_FONT.render('Score: ' + str(score), 1, 'PURPLE')
WIN.blit(score_text, (420, 25))
pygame.display.update()
def isInBins(trash_sprite, bin_sprite, xdrop, ydrop, x_bin, y_bin):
xs_trash, ys_trash, xe_trash, ye_trash = trash_sprite.get_rect()
xs_bin, ys_bin, xe_bin, ye_bin = bin_sprite.get_rect()
xIsIn = (xdrop > x_bin and xdrop < x_bin + xe_bin) or (xdrop + xe_trash > x_bin and xdrop + xe_trash < x_bin + xe_bin)
yIsIn = (ydrop > y_bin and ydrop < y_bin + ye_bin) or (ydrop + ye_trash > y_bin and ydrop + ye_trash < y_bin + ye_bin)
if xIsIn and yIsIn:
return True
else:
return False
def main():
clock = pygame.time.Clock()
run = True
dragging = False
x = 400
y = 400
xdrop_ori = x
ydrop_ori = y
xdrop = x
ydrop = y
score = 0
# trash_sprite = SMALL_BONES
item, whichbin = randomNum()
trash_sprite = load_sprite(whichbin, item + '.png', 70, 90)
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mx, my = event.pos
xstart, ystart, xend, yend = trash_sprite.get_rect()
if mx < xdrop + xend and mx > xdrop and my < ydrop + yend and my > ydrop: # picked the trash
dragging = True
mx, my = event.pos
x, y = xdrop, ydrop
off_x = x - mx
off_y = y - my
# print(off_x, off_y)
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
dragging = False
xdrop = x
ydrop = y
# check if trash is in any one the bins
isInRecycle = isInBins(trash_sprite, RECYCLE_BIN, xdrop, ydrop, RECYCLE_BIN_X_ORI, RECYCLE_BIN_Y_ORI)
isInTrash = isInBins(trash_sprite, TRASH_BIN, xdrop, ydrop, TRASH_BIN_X_ORI, TRASH_BIN_Y_ORI)
isInCompost = isInBins(trash_sprite, COMPOST_BIN, xdrop, ydrop, COMPOST_BIN_X_ORI, COMPOST_BIN_Y_ORI)
if isInRecycle:
whichbin = 'recycle'
elif isInTrash:
whichbin = 'trash'
elif isInCompost:
whichbin = 'compost'
# print(isInCompost)
# print(isInTrash)
# print(isInRecycle)
if isInRecycle or isInTrash or isInCompost:
soundEffect(item, whichbin)
score = scoring(item, whichbin, score)
score = gameEnd(score)
# if check(item, whichbin):
# score = score + 10
# else:
# score = score - 5
item, whichbin = randomNum()
trash_sprite = load_sprite(whichbin, item + '.png', 70, 90)
x = xdrop_ori
y = ydrop_ori
elif event.type == pygame.MOUSEMOTION:
if dragging:
mx, my = event.pos
x = mx + off_x
y = my + off_y
# print(x, y)
#draw_window()
draw_window2(x, y, trash_sprite, score, item)
pygame.quit()
if __name__ == "__main__":
main()