Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (130 loc) · 4.33 KB
/
Copy pathmain.py
File metadata and controls
157 lines (130 loc) · 4.33 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
import time
import pygame
import space_ship
import pipe
from events import *
import pygame.gfxdraw
pygame.init()
# Main window
dimensions = [900, 600]
screen = pygame.display.set_mode(tuple(dimensions))
pygame.display.set_caption("Game")
# Images of surfaces
background = pygame.image.load("Space Background.png").convert()
player = pygame.sprite.GroupSingle()
player.add(
space_ship.SpaceShip(
pygame.transform.rotate(
pygame.transform.scale(
pygame.image.load("Main Ship - Base - Damaged.png").convert_alpha(),
(34 * 2, 27 * 2),
),
-90,
),
(200, 0),
600,
"player",
)
)
score = 0
main_font = pygame.font.Font("ARCADECLASSIC.ttf", 50)
# Initial setting for the score text
score_text = main_font.render(f"{score}", False, pygame.color.Color(255, 255, 255))
score_text_rect = score_text.get_rect()
score_text_rect.topleft = (10, 10)
# Creating the gameover text
game_over_text = main_font.render(
"Game Over!Press H to restart", False, pygame.color.Color(0, 0, 0)
)
game_over_text_rect = game_over_text.get_rect(
center=(dimensions[0] // 2, dimensions[1] // 2)
)
# Gamestates
game_states = ["menu", "play", "game_over"]
game_state = game_states[1]
# Loop Vars
is_running = True
clock = pygame.time.Clock()
# Spawning pipes
spawn_timer = pygame.time.get_ticks()
spawn_interval = 3000
pipes_track = pygame.sprite.Group()
def reset():
global game_state
global score
player.empty()
player.add(
space_ship.SpaceShip(
pygame.transform.rotate(
pygame.transform.scale(
pygame.image.load("Main Ship - Base - Damaged.png").convert_alpha(),
(34 * 2, 27 * 2),
),
-90,
),
(200, 0),
600,
"player",
)
)
pipes_track.empty()
game_state = game_states[1]
score = 0
game_over = True
if __name__ == "__main__":
while is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_h:
pygame.event.post(pygame.event.Event(RESTART_EVENT))
if event.key == pygame.K_q:
pygame.event.post(pygame.event.Event(pygame.QUIT))
if event.type == GAME_OVER_EVENT:
game_state = game_states[2]
game_over = True
if event.type == RESTART_EVENT:
reset()
if event.type == SCORE_INCREMENT_EVENT:
score += 1
print(f"Score: {score}")
if game_state == game_states[1]:
score_text_rect.topleft = (30, 30)
score_text = main_font.render(
f"{score}", False, pygame.color.Color(255, 255, 255)
) # Updating the text as well
# Spawn Enemies
current_time = pygame.time.get_ticks()
if current_time - spawn_timer > spawn_interval:
pipes_track.add(pipe.PipePair(screen, player.sprites()[0]))
spawn_timer = current_time
screen.fill((0, 255, 0))
screen.blit(background, (0, 0)) # Displaying background
# Displaying the shield
pygame.gfxdraw.box(screen, player.sprites()[0].rect, (0, 0, 255, 100)) # draw a transparent box
# GameObjects displayed here
pipes_track.update()
player.draw(screen)
player.update()
for (
p
) in (
pipes_track.sprites()
): # Garbage collection (bassically deleteing something when out of screen)
if p.upper_pipe.x <= -52:
pipes_track.remove(p)
del p
break
screen.blit(score_text, score_text_rect)
if game_state == game_states[2]:
if game_over:
time.sleep(1)
game_over = False
screen.fill("#EFBC68")
screen.blit(game_over_text, game_over_text_rect)
pygame.display.update() # Updating all displays
pygame.display.flip()
clock.tick(60) # Setting max framerate
pygame.quit()
exit()