-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
190 lines (154 loc) · 5.42 KB
/
main.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
from random import choice, randint
from sys import exit
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
player_walk1 = pygame.image.load("graphics/player/player_walk_1.png").convert_alpha()
player_walk2 = pygame.image.load("graphics/player/player_walk_2.png").convert_alpha()
self.player_walk = [ player_walk1, player_walk2 ]
self.player_index = 0
self.player_jump = pygame.image.load('graphics/player/jump.png')
self.image = self.player_walk[ self.player_index ]
self.rect = self.image.get_rect(midbottom = (80, 300))
self.gravity = 0
self.jump_sound = pygame.mixer.Sound('audio/jump.mp3')
self.jump_sound.set_volume(0.5)
def player_input(self):
keys = pygame.key.get_pressed()
if keys[ pygame.K_SPACE ] and self.rect.bottom >= 300:
self.gravity = -20
self.jump_sound.play()
def apply_gravity(self):
self.gravity += 1
self.rect.y += self.gravity
if self.rect.bottom >= 300:
self.rect.bottom = 300
def animation_state(self):
if self.rect.bottom < 300:
self.image = self.player_jump
else:
self.player_index += 0.1
if self.player_index >= len(self.player_walk):
self.player_index = 0
self.image = self.player_walk[ int(self.player_index) ]
def update(self):
self.player_input()
self.apply_gravity()
self.animation_state()
class Obstacle(pygame.sprite.Sprite):
def __init__(self, type):
super().__init__()
if type == 'fly':
fly_frame_1 = pygame.image.load("graphics/Fly/Fly1.png").convert_alpha()
fly_frame_2 = pygame.image.load("graphics/Fly/Fly2.png").convert_alpha()
self.frames = [ fly_frame_1, fly_frame_2 ]
y_pos = 210
else:
snail_frame_1 = pygame.image.load("graphics/snail/snail1.png").convert_alpha()
snail_frame_2 = pygame.image.load("graphics/snail/snail2.png").convert_alpha()
self.frames = [ snail_frame_1, snail_frame_2 ]
y_pos = 300
self.animation_index = 0
self.image = self.frames[ self.animation_index ]
self.rect = self.image.get_rect(midbottom = (randint(900, 1100), y_pos))
def animation_state(self):
self.animation_index += 0.1
if self.animation_index >= len(self.frames):
self.animation_index = 0
self.image = self.frames[ int(self.animation_index) ]
def update(self):
self.animation_state()
self.rect.x -= 6
self.destroy()
def destroy(self):
if self.rect.x <= -100:
self.kill()
def display_score():
current_time = int(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 collision_sprite():
if pygame.sprite.spritecollide(player.sprite, obstacle_group, False):
return False
return True
# Initialize and create the screen in pygame
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
# Initial game point
GAME_ACTIVE = False
# Import the font into the game
test_font = pygame.font.Font("font/Pixeltype.ttf", 50)
# Scoring
start_time = 0
score = 0
# Add Background Music
bg_music = pygame.mixer.Sound('audio/music.wav')
bg_music.set_volume(0.6)
bg_music.play(loops = -1) # -1 means infinite loop
# Create a surface and load an image
sky_surface = pygame.image.load("graphics/sky.png").convert()
ground = pygame.image.load("graphics/ground.png").convert()
# Groups
player = pygame.sprite.GroupSingle()
player.add(Player())
obstacle_group = pygame.sprite.Group()
# Player for Intro
player_stand = pygame.image.load("graphics/player/player_stand.png").convert_alpha()
player_stand_scaled = pygame.transform.rotozoom(player_stand, 0, 2)
player_stand_rect = player_stand_scaled.get_rect(center = (400, 200))
# Introduction Page
game_name = test_font.render('Pixel Runner', False, (64, 64, 64))
game_name_rect = game_name.get_rect(center = (400, 100))
start_game_surf = test_font.render('Press Space to start', False, (64, 64, 64))
start_game_rect = start_game_surf.get_rect(center = (400, 300))
# Timer
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 1500)
while True:
# Check if the user wants to quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.QUIT
exit()
if GAME_ACTIVE:
if event.type == obstacle_timer:
obstacle_group.add(Obstacle(choice([ 'fly', 'snail', 'snail', 'snail' ])))
else:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
GAME_ACTIVE = True
start_time = int(pygame.time.get_ticks() / 1000)
if GAME_ACTIVE:
# Fill the display (screen, position)
screen.blit(sky_surface, (0, 0))
screen.blit(ground, (0, 300))
# Display Score
score = display_score()
# Display Player
player.draw(screen)
player.update()
# Display obstacles
obstacle_group.draw(screen)
obstacle_group.update()
# Collisions
GAME_ACTIVE = collision_sprite()
else:
# If the game is not active, display the introduction page
screen.fill((94, 129, 162))
screen.blit(player_stand, player_stand_rect)
screen.blit(game_name, game_name_rect)
# If score is present, display it
score_board = test_font.render(f'Score: {score}', False, (64, 64, 64))
score_board_rect = score_board.get_rect(center = (400, 300))
if score == 0:
screen.blit(start_game_surf, start_game_rect)
else:
screen.blit(score_board, score_board_rect)
# Drawing all the elements
pygame.display.update()
# Limit the frame rate to 60 FPS(Maximum FPS)
clock.tick(60)