-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrogger.py
211 lines (177 loc) · 5.58 KB
/
Frogger.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
import pygame
import random
import threading
pygame.init()
pygame.font.init()
default_font = pygame.font.get_default_font()
font_renderer = pygame.font.Font(default_font, 15)
ORANGE = (255, 140, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 10)
FRAME_W = 520
FRAME_H = 520 #11 posible lines for obstacles
FROG_W = 20
MOVE = FROG_W
screen = pygame.display.set_mode((FRAME_H, FRAME_W))
pygame.display.set_caption("Frogger")
alive = True
running = True
score = 0
stage = 0
current_move = " "
frogpos_x = 0
frogpos_y = 0
clock = pygame.time.Clock()
clock.tick(1)
DRIVE = pygame.USEREVENT+1
pygame.time.set_timer(DRIVE, 100)
def init():
global frogpos_y, frogpos_x
frogpos_x = FRAME_W / 2
frogpos_y = FRAME_H - 2 * FROG_W
def frog_up():
global frogpos_y
if frogpos_y-MOVE >= 0:
frogpos_y -= MOVE
def frog_left():
global frogpos_x
if frogpos_x-MOVE >= 0:
frogpos_x -= MOVE
def frog_right():
global frogpos_x
if frogpos_x+MOVE < FRAME_W:
frogpos_x += MOVE
def frog_down():
global frogpos_y
if frogpos_y+MOVE < FRAME_H:
frogpos_y += MOVE
# lines: [l1,l2,l3,...,]
# line (l1): [v1,v2,v3]
# vehicle (v1): [length, cur_x]
lines = [[] for i in range(11)]
lines[0] = [[2, 0]]
lines[1] = [[2, FRAME_W]]
lines[2] = [[2, 0]]
lines[3] = [[2, FRAME_W]]
lines[4] = [[2, 0]]
lines[5] = [[2, FRAME_W]]
lines[6] = [[2, 0]]
lines[7] = [[2, FRAME_W]]
lines[8] = [[2, 0]]
lines[9] = [[2, FRAME_W]]
lines[10] = [[2, 0]]
def die():
global alive, FRAME_H, FRAME_W, BLACK, score
print("died with a score of: "+str(score))
alive = False
pygame.time.set_timer(DRIVE, -1)
#TODO draw endscreen
font_renderer = pygame.font.Font(default_font, 30)
label = font_renderer.render("DEFEATED\nSCORE: " + str(score), False, BLACK)
screen.blit(label, (FRAME_W/2, FRAME_H/2))
def line_to_y(line_number):
return FRAME_H - (line_number + 1) * 40 - FROG_W
def calculate_vehic():
global lines, FROG_W, FRAME_W, FRAME_H
for vehicles in lines:
obst_count = 0
ix = lines.index(vehicles)
for v in vehicles:
if ix % 2 == 0:
v[1] += FROG_W
if v[1] > FRAME_W:
vehicles.remove(v)
else:
obst_count += v[0]
else:
v[1] -= FROG_W
if v[1] < 0 - v[0] * FROG_W:
vehicles.remove(v)
else:
obst_count += v[0]
if obst_count < 5:
r = random.randint(2, 6)
# coming from left
if ix % 2 == 0:
if vehicles[len(vehicles) - 1][1] - vehicles[len(vehicles) - 1][0] * FROG_W > 0:
vehicles.append([r, 0])
# coming from right
else:
if vehicles[len(vehicles) - 1][1] + vehicles[len(vehicles) - 1][0] * FROG_W < FRAME_W:
vehicles.append([r, FRAME_W])
if obst_count < 15:
r = random.randint(0, 5)
if r == 1:
r = random.randint(2, 6)
# coming from left
if ix % 2 == 0:
if vehicles[len(vehicles) - 1][1] - vehicles[len(vehicles) - 1][0] * FROG_W > 0:
vehicles.append([r, 0])
# coming from right
else:
if vehicles[len(vehicles) - 1][1] + vehicles[len(vehicles) - 1][0] * FROG_W < FRAME_W:
vehicles.append([r, FRAME_W])
def check_alive():
global alive, frogpos_x, frogpos_y, lines
for vehicles in lines:
ix = lines.index(vehicles)
for v in vehicles:
if frogpos_y == line_to_y(ix) and v[1] <= frogpos_x <= v[1] + v[0] * FROG_W:
die()
def redraw():
if alive:
# Redraw
screen.fill(BLACK)
else:
screen.fill(RED)
# draw score
label = font_renderer.render("Score: " + str(score), False, WHITE)
screen.blit(label, (0, 0))
# draw obstacles
for vehicles in lines:
ix = lines.index(vehicles)
for v in vehicles:
pygame.draw.rect(screen, YELLOW, [v[1], line_to_y(ix), FROG_W * v[0], FROG_W])
# draw Frog
pygame.draw.rect(screen, WHITE, [frogpos_x, frogpos_y, FROG_W, FROG_W])
pygame.display.flip()
init()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_w or event.key == pygame.K_UP:
current_move = "w"
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
current_move = "a"
if event.key == pygame.K_s or event.key == pygame.K_DOWN:
current_move = "s"
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
current_move = "d"
if event.type == DRIVE:
if current_move == "w":
frog_up()
current_move = ""
elif current_move == "a":
frog_left()
current_move = ""
elif current_move == "s":
frog_down()
current_move = ""
elif current_move == "d":
frog_right()
current_move = ""
calculate_vehic()
check_alive()
redraw()
if frogpos_y < 40:
stage += 1
score += 100
init()
pygame.quit()