-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveballs.py
More file actions
222 lines (194 loc) · 6.36 KB
/
saveballs.py
File metadata and controls
222 lines (194 loc) · 6.36 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
from time import sleep
import shelve
import pygame as pg
from random import randint,randrange
from gtts import gTTS
from pygame import mixer
#SOME STANDARD DECLARATION
pg.init()
mixer.init()
gameIcon = pg.image.load('icon.jpg')
pg.display.set_icon(gameIcon)
window=pg.display.set_mode((800,600),0,32)
pg.display.set_caption("Save the balls")
c=pg.time.Clock()
bar=pg.image.load('redbar.png')
basket=pg.image.load('vball.png')
soccer=pg.image.load('cball.png')
sicon=pg.image.load('start.png')
qicon=pg.image.load('cross.png')
ricon=pg.image.load('repeat.png')
ts=pg.mixer.Sound('Tick.ogg')
gbs=pg.mixer.Sound('glass_break.wav')
my_font = pg.font.SysFont("comicsansms", 25,1)
overfont = pg.font.SysFont("comicsansms", 56)
startfont=pg.font.SysFont("comicsansms",85,1)
byfont=pg.font.SysFont("comicsansms",15,1)
hsfont=pg.font.SysFont("comicsansms",30,1)
d = shelve.open('score.txt') # here you will save the score variable
score=d['score']
#magic=pg.image.load('magic.png')
#FUNCTION DEFINITIONS
def button_action(p1,q1,p2,q2,do=None):
drag=pg.mouse.get_pos()
click=pg.mouse.get_pressed()
if p1<drag[0]<p2 and q1<drag[1]<q2 and click[0]==1:
if do=="play" or do=="reload":
gameplay()
#elif do=="home":
# goHome()
elif do=="quit":
pg.QUIT
raise SystemExit
def speech(str,fname):
#con = gTTS(text=str, lang="hi")
fname+=".mp3"
#con.save(fname)
mixer.music.load(fname)
mixer.music.play()
def startgame(scr):
speech("Welcome!", "wel_tone")
while True:
window.fill([255, 240, 230])
window.blit(sicon,(200,306))
window.blit(qicon,(472,306))
button_action(199, 305, 328, 434,"play")
button_action(471, 305, 600, 434,"quit")
start_text=startfont.render('Save the balls',True,[0,10,250])
window.blit(start_text,(window.get_width() / 2 - start_text.get_rect().width / 2, 140))
byme=byfont.render('By: Shivam Maurya',True,[0,0,0])
window.blit(byme,(630,560))
hscore = hsfont.render('Highest Score: '+str(scr), True, [0, 0, 0])
hstext=hscore.get_rect()
text_hs=window.get_width() / 2 - hstext.width / 2
window.blit(hscore, (text_hs, 500))
for event in pg.event.get():
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
pg.QUIT
raise SystemExit
pg.display.update()
def moveball(x,y,xp,yp,bx,scr,gover):
xc, yc = xp,yp
if x>=779:
xc=randrange(-4,-3,1)
elif y>=560:
if bx-20>x or bx+220<x:
gbs.set_volume(.2)
gbs.play()
sleep(1.5)
gover=True
else:
ts.play()
scr+=5
yc = randrange(-6, -4, 1)
if xc==0:
if bx+110<x:
xc=randint(1,3)
elif bx-112>x:
xc=randrange(-2,-1,1)
elif x<=5:
xc=randint(3,4)
elif y<=5:
yc=randint(4,6)
return gover,scr,xc,yc
def gameover(scr):
if score<scr:
d['score'] = scr
speech("Congrats you have created the highest score", "congo")
elif scr==0:
speech('Ooops','zero')
else:
speech("play again!", "reload_game")
while True:
window.fill([250, 240, 230])
text = overfont.render('Game Over', True, [250, 0, 0])
yscr = overfont.render('You scored: ' + str(scr), True, [0, 50, 250])
text_rect = text.get_rect()
text_x = window.get_width() / 2 - text_rect.width / 2
window.blit(text, (text_x,150 ))
window.blit(yscr, (window.get_width()/2 - yscr.get_rect().width/2, 226 ))
window.blit(ricon,(200,322))
window.blit(qicon, (472, 322))
button_action(200,332,328,460,"reload")
button_action(470,332,598,460,"quit")
for event in pg.event.get():
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
pg.QUIT
raise SystemExit
pg.display.update()
def gameplay():
scr = 0
gover = False
x1, y1 = randint(210, 450), 10
x2, y2 = randint(351, 540), 310
xb, yb = 390, 580
xc1 = randint(-1, 1)
yc1 = randint(3,5)
xc2 = randint(1, 2)
yc2 = randint(3,4)
#to enable third ball uncomment the below lines containing '3'
# x3,y3=randint(510,790),410
# xc3=randint(-1,1)
# yc3=randint(1,2)
while True:
if gover:
gameover(scr)
else:
window.fill([255, 240, 230])
window.blit(bar, (xb, yb))
window.blit(basket, (x1, y1))
window.blit(soccer, (x2, y2))
# window.blit(magic, (x3, y3))
x1 += xc1
y1 += yc1
x2 += xc2
y2 += yc2
# x3+=xc3
# y3+=yc3
gover, scr, xc1, yc1 = moveball(x1, y1, xc1, yc1, xb, scr, gover)
gover, scr, xc2, yc2 = moveball(x2, y2, xc2, yc2, xb, scr, gover)
# gover,scr,xc3,yc3=moveball(x3,y3,xc3,yc3,xb,scr,gover)
scoresurf = my_font.render('score:' + str(scr), True, (0, 0, 0))
window.blit(scoresurf, (10, 10))
for event in pg.event.get():
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
pg.QUIT
raise SystemExit
if event.type == pg.KEYDOWN:
if event.key == pg.K_LEFT and xb > 5:
xb = xb - 5
if event.key == pg.K_RIGHT and xb < 805:
xb = xb + 5
keys_pressed = pg.key.get_pressed()
if keys_pressed[pg.K_RIGHT]:
if xb > 595:
xb += 0
else:
xb += 10
elif keys_pressed[pg.K_LEFT]:
if xb < 1:
xb += 0
else:
xb -= 10
pg.display.update()
c.tick(callfps(scr))
def callfps(scr):
fps=60
if scr<=30:
fps=30
elif scr>30 and scr<=90:
fps=45
elif scr>90 and scr<=150:
fps=60
elif scr>150 and scr<=240:
fps=75
elif scr>240 and scr<=480:
fps =90
elif scr>480 and scr<=960:
fps=120
elif scr>960:
fps=150
return fps
startgame(score)
d.close()
raise SystemExit