-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflap.py
222 lines (142 loc) · 3.39 KB
/
flap.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
from random import randint
from processing import *
from time import sleep
from math import *
def setup():
frameRate(30)
size(500, 500)
class Bird:
def __init__ (self):
self.x = 100
self.y = 250
self.v = 0
self.playing = 2
def display (self):
stroke(0)
fill(252, 33, 73)
rectMode(CENTER)
fc = environment.frameCount
radius = 25 + 2*sin(fc /4)
ellipse(self.x, self.y, radius, radius);
def isTouching(self, wall):
if (wall.x < bird.x < wall.x + 75):
if (bird.y < wall.y - 75) or (bird.y > wall.y + 75):
bird.playing = 3
else:
bird.playing = 1
class Wall:
def __init__ (self):
self.spacing = 75
self.x = 500
self.y = randint(10 + self.spacing, 500 - (10 + self.spacing))
def display (self):
rectMode(CORNER)
stroke(80)
fill(240, 103, 133)
rect(self.x, self.y - (500 + self.spacing), 75, 500)
rect(self.x, self.y + self.spacing, 75, 500)
class Counter:
def __init__ (self):
self.count = 0
self.highscore = 0
def display (self):
ellipseMode(CENTER)
fill(300, 300, 300)
ellipse(10, 10, 200, 100)
fill(270, 50, 100)
textSize(32)
text(self.count, 10, 30)
class Cloud:
def __init__ (self):
self.x = 250
self.y = 100
self.size = 4
def display(self):
fill(300, 300, 300)
noStroke()
for i in range(self.size):
if i % 2 == 0:
ellipse(self.x + (i * 40), self.y + 20, 85, 65)
else:
ellipse(self.x + (i * 40), self.y - 20, 85, 65)
x = 10
wall1 = Wall()
wall2 = Wall()
wall2.x = 800
g = .8
bird = Bird()
counter = Counter()
cloud1 = Cloud()
cloud2 = Cloud()
cloud1.x = 500
cloud2.x = 800
cloud2.size = 7
def draw():
if bird.playing == 1:
background(192, 226, 237)
fill(42, 115, 53)
rectMode(CORNER)
rect(0, 400, 500, 100)
cloud1.display()
cloud2.display()
if cloud1.x == -500:
cloud1.x = 500
if wall2.x == -700:
wall2.x = 500
cloud1.x -= 2
cloud2.x -= 2
wall1.display()
wall2.display()
wall1.x -= 5
wall2.x -= 5
bird.display()
bird.v += g
bird.y += bird.v
if wall1.x == -100:
wall1.x = 500
if wall2.x == -100:
wall2.x = 500
if (wall1.x == bird.x - 30) or (wall2.x == bird.x -30):
counter.count += 1
bird.isTouching(wall1)
if bird.playing == 1:
bird.isTouching(wall2)
if bird.y < 0 or bird.y > 500:
bird.playing = 3
counter.display()
elif bird.playing == 2:
background(192, 226, 237)
textSize(62)
fill(94, 11, 27)
text("Click to Start", 80, 250)
textSize(18)
fill(0, 0, 0)
text("Flappy Bird by Naveen", 165, 150)
elif bird.playing == 3:
if counter.count >= counter.highscore:
counter.highscore = counter.count
background(192, 226, 237)
textSize(62)
fill(94, 11, 27)
text("Click to Try Again", 10, 250)
textSize(30)
fill(0, 0, 0)
text("Score: " + str(counter.count), 185, 120)
text("Highscore: " + str(counter.highscore), 145, 170)
wall2.x = 800
wall1.x = 500
bird.x = 100
bird.y = 250
def keyPressed():
key = keyboard.keyCode
if key == UP or key == 32:
bird.v = -10
elif key == 82:
bird.playing = 3
def mousePressed():
if 0 < mouseX() < 500 and 0 < mouseY() < 500:
if bird.playing == 3:
counter.count = 0
bird.playing = 1
bird.v = -10
run()