-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndibug.py
312 lines (257 loc) · 9.49 KB
/
Andibug.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#import libraries
import pygame
import random
import os
import pickle
#initialize game
pygame.init()
#game window dimensions
SCREEN_WIDTH = 375
SCREEN_HEIGHT = 600
#create game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Andibug <3')
#set frame rate
clock = pygame.time.Clock()
FPS = 40
#game variables
SCROLL_THRESH = 200
GRAVITY = 1
MAX_PLATFORMS = 10
scroll = 0
bg_scroll = 0
game_over = False
score = 0
fade_counter = 0
if os.path.exists('score.txt'):
with open('score.txt', 'r') as file:
high_score = int(file.read())
else:
high_score = 0
#load images
Andibug_image = pygame.image.load('Andibug 1.png').convert_alpha()
bg_image = pygame.image.load('bg image.gif').convert_alpha()
bg2_image = pygame.image.load('1200 (3).jpg').convert_alpha()
bg3_image = pygame.image.load('1200(4).jpg').convert_alpha()
bg4_image = pygame.image.load('1200(5).jpg').convert_alpha()
bg5_image = pygame.image.load('1200(6).jpg').convert_alpha()
bg6_image = pygame.image.load('pretty sky.jpg').convert_alpha()
platform_image = pygame.image.load('minecraft-ideas.jpg').convert_alpha()
#define colors
WHITE = (255, 255, 255)
Black = (0, 0, 0)
PANEL = (123, 104, 238)
#Define font
font_small = pygame.font.SysFont('Lucida Sans', 20)
font_big = pygame.font.SysFont('Lucida Sans', 24)
#Function for outputting text on screen
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit(img, (x, y))
#function for drawing info panel
def draw_panel():
pygame.draw.rect(screen, PANEL, (0, 0, SCREEN_WIDTH, 30))
pygame.draw.line(screen, WHITE, (0, 30), (SCREEN_WIDTH , 30), 2)
draw_text('SCORE: ' + str(score), font_small, WHITE, 0, 0)
#function for drawing background
def draw_bg(bg_scroll):
screen.blit(bg_image, (0, 0 + bg_scroll))
screen.blit(bg2_image, (0, -600 + bg_scroll))
screen.blit(bg3_image, (0, -1200 + bg_scroll))
screen.blit(bg4_image, (0, -1800 + bg_scroll))
screen.blit(bg5_image, (0, -2400 + bg_scroll))
screen.blit(bg6_image, (0, -3000 + bg_scroll))
screen.blit(bg6_image, (0, -3600 + bg_scroll))
screen.blit(bg6_image, (0, -4200 + bg_scroll))
screen.blit(bg6_image, (0, -4800 + bg_scroll))
screen.blit(bg6_image, (0, -5400 + bg_scroll))
screen.blit(bg6_image, (0, -6000 + bg_scroll))
screen.blit(bg6_image, (0, -6600 + bg_scroll))
screen.blit(bg6_image, (0, -7200 + bg_scroll))
screen.blit(bg6_image, (0, -7800 + bg_scroll))
screen.blit(bg6_image, (0, -8400 + bg_scroll))
screen.blit(bg6_image, (0, -9000 + bg_scroll))
screen.blit(bg6_image, (0, -9600 + bg_scroll))
#player class
class Player():
def __init__(self, x, y):
self.image = pygame.transform.scale(Andibug_image, (55, 55))
self.width = 25
self.height = 40
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = (x, y)
self.vel_y = 0
self.flip = False
def move(self):
#reset vairable
scroll = 0
dx = 0
dy = 0
#process keypresses
key = pygame.key.get_pressed()
if key[pygame.K_a]:
dx = -9
self.flip = True
if key[pygame.K_d]:
dx = 9
self.flip = False
#gravity
self.vel_y += GRAVITY
dy += self.vel_y
#ensure player doesn't go off screen
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.left < 0:
self.rect.left = 0
#check collision with platform
for platform in platform_group:
#collison in the y direction
if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
# check if above platform
if self.rect.bottom < platform.rect.centery:
if self.vel_y > 0:
self.rect.bottom = platform.rect.top
dy = 0
self.vel_y = -20
# check if player has bounced off top of screen
if self.rect.top <= SCROLL_THRESH:
# if player is jumping
if self.vel_y < 0:
scroll = -dy
# update rectangle position
self.rect.x += dx
self.rect.y += dy + scroll
return scroll
def draw(self):
screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 12, self.rect.y - 5))
# pygame.draw.rect(screen, WHITE, self.rect, 2)
#player instance
Andibug = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150)
#platform class
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width, moving):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(platform_image, (width, 22))
self.moving = moving
self.move_counter = random.randint(0, 50)
self.direction = random.choice([-1, 1])
self.speed = random.randint(1, 2)
if score > 1000:
self.speed = random.randint(1, 3)
if score > 4000:
self.speed = random.randint(1, 4)
if score > 5000:
self.speed = random.randint(1, 5)
if score > 6000:
self.speed = random.randint(1, 6)
if score > 7000:
self.speed = random.randint(1, 7)
if score > 8000:
self.speed = random.randint(1, 8)
if score > 9000:
self.speed = random.randint(1, 9)
if score > 9000:
self.speed = random.randint(1, 10)
if score > 10000:
self.speed = random.randint(2, 10)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self, scroll):
#move platform side to side if moving platform
if self.moving == True:
self.move_counter += 1
self.rect.x += self.direction * self.speed
#change platform if it has moved fully or hit wall
if self.move_counter >= 100 or self.rect.left < 0 or self.rect.right > SCREEN_WIDTH:
self.direction *= -1
self.move_counter = 0
#update platform's vertical position
self.rect.y += scroll
#check if platform has gone off screen
if self.rect.top > SCREEN_HEIGHT:
self.kill()
#create sprite groups
platform_group = pygame.sprite.Group()
#create starting platform
platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False)
platform_group.add(platform)
#game loop
run = True
while run:
clock.tick(FPS)
if game_over == False:
scroll = Andibug.move()
#draw background
bg_scroll += scroll
if bg_scroll >= 5400:
bg_scroll = 4200
draw_bg(bg_scroll)
#gernerate platforms
if len(platform_group) < MAX_PLATFORMS:
p_w = random.randint(40, 60)
p_x = random.randint(0, SCREEN_WIDTH - p_w)
p_y = platform.rect.y - random.randint(80, 120)
p_type = random.randint(1, 2)
if p_type == 1 and score > 100:
p_moving = True
else:
p_moving = False
platform = Platform(p_x, p_y, p_w, p_moving)
platform_group.add(platform)
#update platforms
platform_group.update(scroll)
#update score
if scroll > 0:
score += scroll
#draw line at previous high score
pygame.draw.line(screen, PANEL, (0, score - high_score + SCROLL_THRESH), (SCREEN_WIDTH, score - high_score + SCROLL_THRESH), 3)
draw_text('HIGH SCORE', font_small, PANEL, SCREEN_WIDTH - 130, score - high_score + SCROLL_THRESH)
#draw sprites
platform_group.draw(screen)
Andibug.draw()
#draw panel
draw_panel()
#check game Over
if Andibug.rect.top > SCREEN_HEIGHT:
game_over = True
else:
if fade_counter < SCREEN_WIDTH:
fade_counter += 8
for y in range(0, 6, 2):
pygame.draw.rect(screen, Black, (0, y * 100, fade_counter, 100))
pygame.draw.rect(screen, Black, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100))
else:
draw_text('GAME OVER!', font_big, WHITE, 130, 200)
draw_text('SCORE: ' + str(score), font_big, WHITE, 130, 250)
draw_text('PRESS SPACE TO PLAY AGAIN', font_big, WHITE, 20, 300)
#update high score
if score > high_score:
high_score = score
with open('score.txt', 'w') as file:
file.write(str(high_score))
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
#reset variables
game_over = False
score = 0
scroll = 0
bg_scroll = 0
fade_counter = 0
#reposition Andibug
Andibug.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150)
#reset platforms
platform_group.empty()
# create starting platform
platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100, False)
platform_group.add(platform)
#event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
if score > high_score:
high_score = score
with open('score.txt', 'w') as file:
file.write(str(high_score))
run = False
#update display visibility
pygame.display.update()