-
Notifications
You must be signed in to change notification settings - Fork 0
/
flappybird.py
159 lines (137 loc) · 3.76 KB
/
flappybird.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
import pygame
import os
import random
pygame.init()
width, height = 1100, 700
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption('Flappy Bird')
icon = pygame.image.load(os.getcwd() + '\\pics\\flappybirdico.jpg')
pygame.display.set_icon(icon)
screen.fill([255, 255, 255])
pygame.display.flip()
run = True
boost = 0
y = 450
birdtexture = pygame.image.load(os.getcwd() + '\\pics\\flappybird.png')
gameovertexture = pygame.image.load(os.getcwd() + '\\pics\\GameOver.png')
#pipetexture = pygame.image.load(os.getcwd() + '\\pics\\pipes72930.png')
screen.blit(birdtexture, [300, y])
pygame.display.flip()
boostfl = -10
spawntime = 0
def isHeightNormal(birdy):
if birdy > 670 or birdy < 0:
return False
else:
return True
def isQuitPressed():
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
return True
def IsSpacePressed():
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
return False
elif ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_SPACE:
return True
else:
return 'NoEvent'
def IsBirdTouchingPipe(pipey, birdy, pipex):
print(pipey)
if 270 < pipex < 330:
if pipey - 390 < birdy or pipey - 540 > birdy:
return True
else:
return False
else:
return False
def startscreen():
screen.fill([255, 255, 255])
birdy = 450
screen.blit(birdtexture, [300, birdy])
for pipea in pipes:
pipes.remove(pipea)
pygame.sprite.Sprite.kill(pipea)
pygame.display.flip()
while True:
if IsSpacePressed() == True:
return True
if IsSpacePressed() == False:
return False
def gameover():
gameend = True
screen.blit(gameovertexture, [464, 290])
pygame.display.flip()
got = 0
while gameend:
pygame.time.delay(10)
if got == 2000:
return True
else:
got += 10
pygame.time.delay(10)
if isQuitPressed():
pygame.quit()
return False
def pipespawn():
pipe = PipeClass(os.getcwd() + '\\pics\\pipes72930.png', random.randint(-230, 0))
pipes.add(pipe)
class PipeClass(pygame.sprite.Sprite):
def __init__(self, pipe_file, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(pipe_file)
self.rect = self.image.get_rect()
self.rect.left = 1200
self.rect.top = y
def move(self):
self.rect = self.rect.move(-2, 0)
def IsOutOfScreen(self):
if self.rect.left < -50:
return True
else:
return False
pipes = pygame.sprite.Group()
pipespawn()
if not startscreen():
run = False
pygame.quit()
while run: # основной цикл
spawntime += 10
screen.fill([255, 255, 255])
pygame.time.delay(10)
boostfl += 0.2
boost = int(boostfl)
y += boost
if spawntime == 3000:
pipespawn()
spawntime = 0
screen.blit(birdtexture, [300, y])
pygame.display.flip()
for pipe in pipes:
pipe.move()
screen.blit(pipe.image, pipe.rect)
pygame.display.flip()
print('aksfj')
if pipe.IsOutOfScreen():
pipes.remove(pipe)
pygame.sprite.Sprite.kill(pipe)
if IsBirdTouchingPipe(pipe.rect.top, y, pipe.rect.left):
gameover()
startscreen()
run = False
if not run:
break
if not isHeightNormal(y):
boostfl = -10
y = 450
if not gameover():
run = False
pygame.quit()
break
a = startscreen()
if not a:
run = False
pygame.quit()
break