-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
192 lines (146 loc) · 4.63 KB
/
Copy pathmain.py
File metadata and controls
192 lines (146 loc) · 4.63 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
import pygame
import random
class GameObject:
def draw(self):
screen.blit(self.image, (self.x, self.y))
class Player(GameObject):
def __init__(self) -> None:
self.image = pygame.image.load('player.png')
self.x = 185
self.y = 480
self.speed_x = 0
def set_position(self):
self.x += self.speed_x
# restrict movement at boundries
if self.x <= 0:
self.x = 0
elif self.x >= 336:
self.x = 336
def move_right(self):
self.speed_x = 0.5
def move_left(self):
self.speed_x = -0.5
def stop(self):
self.speed_x = 0
class Enemy(GameObject):
def __init__(self) -> None:
self.image = pygame.image.load('enemy.png')
self.x = random.randint(0, 337)
self.y = random.randint(50, 150)
self.speed_x = 0.5
self.speed_y = 50
def set_position(self):
self.x += self.speed_x
# change Direction at boundries
if self.x <= 0:
self.move_right()
self.y += self.speed_y
elif self.x >= 336:
self.move_left()
self.y += self.speed_y
def move_right(self):
self.speed_x = 0.5
def move_left(self):
self.speed_x = -0.5
def respawn(self):
self.x = random.randint(0, 373)
self.y = random.randint(50, 150)
class Bullet(GameObject):
def __init__(self) -> None:
self.image = pygame.image.load('bullet.png')
self.x = 386.5
self.y = 490
self.speed_y = -2
self.state = 'ready'
def set_position(self, x, y):
if self.state == 'ready':
self.x = x + 16.5
self.y = y + 10
elif self.state == 'fired':
self.y += self.speed_y
if self.y <= 0:
self.state = 'ready'
def fire(self):
self.state = 'fired'
class GameTexts:
def __init__(self):
self.font = pygame.font.Font('freesansbold.ttf', 32)
def show(self):
score = self.font.render(self.text, True, (255, 255, 255))
screen.blit(score, (self.x, self.y))
class Score(GameTexts):
def __init__(self):
super().__init__()
self.value = 0
self.x = 10
self.y = 10
self.text = 'Score : 0'
def increase(self):
self.value += 1
self.text = 'Score :' + str(self.value)
class GameOver(GameTexts):
def __init__(self):
super().__init__()
self.x = 200
self.y = 250
self.text = "GAME OVER"
# Initialize the pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((400, 600))
# Title
pygame.display.set_caption("Space Invaders")
# Icon
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# Background
background = pygame.image.load('bg.jpg')
if __name__=='__main__':
player = Player()
enemies = []
for i in range(5):
enemies.append(Enemy())
bullet = Bullet()
score = Score()
#Game Loop
running = True
while running:
# Background image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# KeyDown Event Handling for Player Movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.move_left()
if event.key == pygame.K_RIGHT:
player.move_right()
if event.key == pygame.K_SPACE:
bullet.fire()
# KeyUp Event Handling for Player Movement
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player.stop()
# Setting Positions
player.set_position()
for enemy in enemies:
enemy.set_position()
bullet.set_position(player.x, player.y)
# Display Characters
player.draw()
bullet.draw()
for enemy in enemies:
enemy.draw()
score.show()
# Enemy Kill
for enemy in enemies:
if bullet.x >= enemy.x and bullet.x <= enemy.x+32 and bullet.y >= enemy.y and bullet.y <= enemy.y+32:
enemy.respawn()
score.increase()
# Player-Enemy Collision
for enemy in enemies:
if enemy.x >= player.x-62 and enemy.x <= player.x+62 and enemy.y >= player.y-62 and enemy.y <= enemy.y+62:
running = False
# Update all changes of while loop
pygame.display.update()