-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygame
More file actions
220 lines (162 loc) · 6.72 KB
/
pygame
File metadata and controls
220 lines (162 loc) · 6.72 KB
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
import pygame
from random import randint
from sys import exit
def display_score():
current_time = (pygame.time.get_ticks() // 1000) - start_time
score_surf = test_font.render(f'Score: {current_time}', False, (64, 64, 64))
score_rect = score_surf.get_rect(center = (400, 50))
screen.blit(score_surf, score_rect)
return current_time
def obstacle_movement(obstacle_list):
if obstacle_list:
for obstacle_rect in obstacle_list:
obstacle_rect.x -= 5
if obstacle_rect.bottom == 300:
screen.blit(snail_surf, obstacle_rect)
else:
screen.blit(fly_surf, obstacle_rect)
obstacle_list = [obstacle for obstacle in obstacle_list if obstacle.x > -100]
return obstacle_list
else: return []
def collisions(player,obstacles):
if obstacles:
for obstacle_rect in obstacles:
if player.colliderect(obstacle_rect):
return False
return True
def player_animation():
global player_surf, player_index
# play walking animation if the player is on the floor
# display the jump surface when the player leaves the floor
if player_rect.bottom < 300:
player_surf = player_jump
else:
player_index += 0.1
if player_index >= len(player_walk):player_index = 0
player_surf = player_walk[int(player_index)]
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('Cool')
clock = pygame.time.Clock()
test_font = pygame.font.Font('font/Pixeltype.ttf', 50)
game_active = False
start_time = 0
score = 0
highest_score = 0
ground_surf = pygame.image.load('graphics/ground.png').convert()
sky_surf = pygame.image.load('graphics/Sky.png').convert()
# score_surf = test_font.render('My Game!', False, (64, 64, 64))
# score_rect = score_surf.get_rect(center = (400, 50))
#obstacles
snail_frame_1 = pygame.image.load('graphics/snail/snail1.png').convert_alpha()
snail_frame_2 = pygame.image.load('graphics/snail/snail2.png').convert_alpha()
snail_frames = [snail_frame_1, snail_frame_2]
snail_frame_index = 0
snail_surf = snail_frames[snail_frame_index]
fly_frame_1 = pygame.image.load('graphics/fly/fly1.png').convert_alpha()
fly_frame_2 = pygame.image.load('graphics/fly/fly2.png').convert_alpha()
fly_frames = [fly_frame_1, fly_frame_2]
fly_frame_index = 0
fly_surf = fly_frames[fly_frame_index]
obstacle_rect_list = []
player_walk_1 = pygame.image.load('graphics/player/player_walk_1.png').convert_alpha()
player_walk_2 = pygame.image.load('graphics/player/player_walk_2.png').convert_alpha()
player_walk = [player_walk_1, player_walk_2]
player_index = 0
player_jump = pygame.image.load('graphics/player/jump.png').convert_alpha()
player_surf = player_walk[player_index]
player_rect = player_surf.get_rect(midbottom = (80, 300))
player_gravity = 0
# intro screen
title_surf = test_font.render('Super Snail Jump', False, (111, 196, 169))
title_rect = title_surf.get_rect(center = (400, 50))
player_stand = pygame.image.load('graphics/player/player_stand.png').convert_alpha()
player_stand = pygame.transform.rotozoom(player_stand, 0, 2)
player_stand_rect = player_stand.get_rect(center = (400, 200))
play_surf = test_font.render('Press Space to Start', False, (111, 196, 169))
play_rect = play_surf.get_rect(center = (400, 350))
#timer
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 1500)
snail_animation_timer = pygame.USEREVENT + 2
pygame.time.set_timer(snail_animation_timer, 500)
fly_animation_timer = pygame.USEREVENT + 3
pygame.time.set_timer(fly_animation_timer, 200)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if not game_active:
if event.type == pygame.MOUSEBUTTONDOWN:
if True:
pass
if game_active:
if event.type == pygame.MOUSEBUTTONDOWN:
if player_rect.collidepoint(event.pos) and player_rect.bottom >= 300:
player_gravity = -20
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and player_rect.bottom >= 300:
player_gravity = -20
else:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
game_active = True
start_time = (pygame.time.get_ticks() // 1000)
if game_active:
if event.type == obstacle_timer:
if randint(0,2):
obstacle_rect_list.append(snail_surf.get_rect(bottomright = (randint(900,1100), 300)))
else:
obstacle_rect_list.append(fly_surf.get_rect(bottomright = (randint(900,1100), 210)))
if event.type == snail_animation_timer:
if snail_frame_index == 0:
snail_frame_index = 1
else:
snail_frame_index = 0
snail_surf = snail_frames[snail_frame_index]
if event.type == fly_animation_timer:
if fly_frame_index == 0:
fly_frame_index = 1
else:
fly_frame_index = 0
fly_surf = fly_frames[fly_frame_index]
if game_active:
screen.blit(ground_surf, (0, 300))
screen.blit(sky_surf, (0, 0))
# pygame.draw.rect(screen, (255,153,255), score_rect)
# pygame.draw.rect(screen, (255,153,255), score_rect, 15)
# screen.blit(score_surf, score_rect)
score = display_score()
if score > highest_score:
highest_score = score
#
# snail_rect.x -= 4
# if snail_rect.right <= 0:
# snail_rect.left = 800
# screen.blit(snail_surf, snail_rect)
#
# player
player_gravity += 1
player_rect.y += player_gravity
if player_rect.bottom >= 300:
player_rect.bottom = 300
player_animation()
screen.blit(player_surf,player_rect)
#obstacle movement
obstacle_rect_list = obstacle_movement(obstacle_rect_list)
game_active = collisions(player_rect, obstacle_rect_list)
else:
screen.fill((94,129,162))
screen.blit(player_stand, player_stand_rect)
obstacle_rect_list.clear()
player_rect.midbottom = (80, 300)
player_gravity = 0
score_message = test_font.render(f'Your score: {score}, highest: {highest_score}', False, (111, 196, 169))
score_message_rect = score_message.get_rect(center = (400, 330))
screen.blit(title_surf, title_rect)
if score == 0:
screen.blit(play_surf, play_rect)
else:
screen.blit(score_message, score_message_rect)
pygame.display.update()
clock.tick(60)