-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainobj.py
290 lines (211 loc) · 8.09 KB
/
mainobj.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
import pygame,sys,random,time,os
pygame.init()
class flappy():
def __init__(self,x,y):
self.movex=0
self.movey=1
self.rect=pygame.Rect((x, y, 36, 26))
self.flapsound = pygame.mixer.Sound(os.path.relpath("flap.wav"))
self.crashsound = pygame.mixer.Sound(os.path.relpath("hurt.wav"))
self.scoresound = pygame.mixer.Sound(os.path.relpath("score.wav"))
self.birdimg = pygame.image.load(os.path.relpath("bird_sing.png"))
def move(self, y):
self.rect=self.rect.move(0,y)
def render(self,screen):
screen.blit(self.birdimg, self.rect)
def collide(self,firstwallpair):
return self.rect.colliderect(firstwallpair.upperwall) or self.rect.colliderect(firstwallpair.lowerwall)
def experimental(self,y):
self.rect.y=self.rect.y+y
class pillarpair():
def __init__(self, width,height,xcord,screen):
self.screen=screen
self.width = width
self.height = height
self.xcord=xcord
self.widthofwall = 20
self.newwall(xcord)
self.assetloader(width,height)
def assetloader(self,width, height):
self.pillar1 = pygame.image.load(os.path.relpath("tube1.png"))
self.pillar2 = pygame.image.load(os.path.relpath("tube2.png"))
self.pillar1dest = pygame.transform.scale(self.pillar1, (self.widthofwall, self.upperwall.height))
self.pillar2dest = pygame.transform.scale(self.pillar2, (self.widthofwall, self.lowerwall.height))
def newwall(self,xcord):
self.firstrectlength = random.randint(50, 300)
self.secondrectlength = self.height - 200 - self.firstrectlength
self.upperwall = pygame.Rect(xcord, 0, self.widthofwall, self.firstrectlength)
self.lowerwall = pygame.Rect(xcord, self.height - self.secondrectlength, self.widthofwall, self.secondrectlength)
def renderpillars(self):
self.screen.blit(self.pillar1dest, self.upperwall)
self.screen.blit(self.pillar2dest, self.lowerwall)
def move(self,x):
self.upperwall=self.upperwall.move(x,0)
self.lowerwall=self.lowerwall.move(x,0)
def copy(self,obj2):
self.upperwall=obj2.upperwall
self.lowerwall=obj2.lowerwall
def getgoing():
size = width, height = 300, 510
screen = pygame.display.set_mode(size)
pygame.display.set_caption("GK Flappy bird")
x, y = 10, height / 2
movey = 4
bird=flappy(x,y)
clock = pygame.time.Clock()
score = 0
frame = 0
accel=0
upaccel=0
moveyes=0
qa = 1
setyes=1
firstwallx = 200
secondwallx = 350
thirdwallx = 500
totaltimesinceevent=0
initializer=0
background = pygame.image.load(os.path.relpath("bg.png"))
background = pygame.transform.scale(background, (width, height))
firstwallpair = pillarpair(width, height, firstwallx,screen)
secondwallpair = pillarpair(width, height, secondwallx,screen)
thirdwallpair = pillarpair(width, height, thirdwallx,screen)
while True:
a=clock.tick_busy_loop(120)
totaltimesinceevent+=a
if initializer==0:
totaltimesinceevent=8
initializer=1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
movey=1
#bird.move(movey)
moveyes=1
#movey = -90
bird.flapsound.play()
#movey = 4
totaltimesinceevent=0
frame=0
if bird.collide(firstwallpair):
scorecarddisp = collided(score, width, height)
bird.crashsound.play()
anotherdraw(scorecarddisp, width, height,firstwallpair,secondwallpair, bird, background)
break
elif bird.rect.left > firstwallpair.upperwall.right and qa == 1:
score += 1
bird.scoresound.play()
qa = 0
frame += 1
firstwallpair.move(-1)
secondwallpair.move(-1)
thirdwallpair.move(-1)
if firstwallpair.upperwall.left == -firstwallpair.widthofwall:
firstwallpair.copy(secondwallpair)
secondwallpair.copy(thirdwallpair)
var=thirdwallpair.upperwall.x
thirdwallpair.newwall(var+150)
firstwallpair.assetloader(width,height)
secondwallpair.assetloader(width,height)
thirdwallpair.assetloader(width,height)
qa = 1
if bird.rect.bottom >= height:
bird.rect.bottom = height
if bird.rect.top <= 0:
bird.rect.top = 0
if moveyes==0:
if frame <= 15:
movey = 1
bird.experimental(movey)
if frame == 15:
movey = 4
bird.experimental(movey)
movey = 1
accel = 0
else:
accel += 1
if accel == 46:
if movey < 10:
movey += 1
accel = 0
bird.experimental(movey)
else:
upaccel += 1
if upaccel == 1:
if movey > -6:
movey -= 0.5
print movey
if movey <= -6:
moveyes = 0
bird.experimental(movey)
upaccel = 0
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
firstwallpair.renderpillars()
secondwallpair.renderpillars()
thirdwallpair.renderpillars()
bird.render(screen)
pygame.display.flip()
def collided(score, width, height):
scorefont = pygame.font.get_default_font()
scorecard = pygame.font.Font(scorefont, 50)
scorecarddisp = scorecard.render(str(score), 0, (0, 0, 0))
return scorecarddisp
def anotherdraw(scorecarddisp, width, height,firstwallpair,secondwallpair , bird, background):
screen = pygame.display.set_mode()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
getgoing()
screen.blit(background, (0, 0))
firstwallpair.renderpillars()
secondwallpair.renderpillars()
bird.render(screen)
screen.blit(scorecarddisp, (width / 2, height / 2))
pygame.display.flip()
getgoing()
"""
""""""
def exp(self,y,tick,totaltimesinceevent,acceleration,moveyes,movey):
if moveyes==0:
if totaltimesinceevent <= 15 * tick:
movey = 0.1
self.experimental(movey)
if totaltimesinceevent == 15 * tick:
movey = 4
self.experimental(movey)
movey = 1
elif totaltimesinceevent == 43 * tick:
if movey < 10:
movey += 1
self.experimental(movey)
totaltimesinceevent=0
elif moveyes==1:
if self.movey > -6:
print self.movey
self.movey =self.movey- 1
if self.movey <= -6:
moveyes = 0
self.move(movey)
#if self.movey != y:
# self.movey = (self.movey + acceleration)
# self.experimental(self.movey)
#if totaltimesinceevent <= 135:
# if moveyes == 0:
# if setyes == 1:
# tempy = 0.1
# self.experimental(tempy)
# setyes = 0
# return 12
"""
"""
else:
bird.exp(-6,a,totaltimesinceevent,-0.5,moveyes,setyes)
if bird.movey<-6:
moveyes=0
setyes=1
"""