-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (113 loc) · 3.6 KB
/
main.py
File metadata and controls
136 lines (113 loc) · 3.6 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
import pygame, sys
pygame.init()
screenW = 400
screenH = 600
screen = pygame.display.set_mode((screenW, screenH))
pygame.display.set_caption('Space Invaders')
ship = pygame.image.load('ship.png')
ship = pygame.transform.scale(ship, (90, 90))
sx = 157
sSpeed = 0
bullet = pygame.image.load('bullet.png')
bullet = pygame.transform.scale(bullet, (10, 18))
by = -300
bx = -200
bSpeed = .3
alienList = []
xPos = []
yPos = []
num = 0
aspeed = .2
# sounds
alien_sound = pygame.mixer.Sound("../pythonProject11/other sound.wav")
bullet_sound = pygame.mixer.Sound("../pythonProject11/sound2.wav")
# scores
score = 0
# define the score board function
def display_scores():
font = pygame.font.SysFont('comic sans', size=25)
text = font.render(f"Score: {score}", True, (255, 255, 255))
text_rect = text.get_rect(center=(screenW / 2, 20))
screen.blit(text, text_rect)
for i in range(5):
for j in range(11):
alienList.append(pygame.image.load('alien.png').convert_alpha())
alienList[num] = pygame.transform.scale(alienList[num], (55, 50))
xPos.append(j * 30 + 1)
yPos.append(i * 35)
num += 1
# set the start screen flag
start_screen = True
# start screen loop
while start_screen:
# events
for event in pygame.event.get():
if event.type == pygame.QUIT:
start_screen = False
game_over = True
if event.type == pygame.KEYDOWN:
start_screen = False
# draw the start screen
screen.fill((0, 0, 0))
font = pygame.font.SysFont('comic sans', size=40)
font2 = pygame.font.SysFont('comic sans', size=30)
title = font.render("Space Invaders", True, (255, 255, 255))
message = font2.render("Press any key to start", True, (255, 255, 255))
start_text_rect = title.get_rect(center=(screenW / 2, 200))
screen.blit(title, start_text_rect)
start_text_rect2 = title.get_rect(center=(188, 400))
screen.blit(message, start_text_rect2)
pygame.display.update()
gameOn = True
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_RIGHT:
sSpeed += .2
if e.key == pygame.K_LEFT:
sSpeed -= .2
if e.key == pygame.K_SPACE and by < 0:
pygame.mixer.Sound.play(bullet_sound)
bx = sx + 40
by = 520 - 29
bSpeed = -.5
if e.type == pygame.KEYUP:
if e.key == pygame.K_RIGHT:
sSpeed -= .2
if e.key == pygame.K_LEFT:
sSpeed += .2
# logic
sx += sSpeed
if sx > 340:
sx = 340
if sx < 0:
sx = 0
by += bSpeed
changeD = False
for i in range(55):
xPos[i] += aspeed
if xPos[i] > screenW - 25 or xPos[i] < 0:
changeD = True
if changeD:
changeD = False
aspeed *= -1
for i in range(55):
yPos[i] += 20
for i in range(55):
if pygame.rect.Rect(bx, by, bullet.get_width(), bullet.get_height()).colliderect(pygame.rect.Rect(xPos[i], yPos[i], alienList[i].get_width(), alienList[i].get_height())):
pygame.mixer.Sound.play(alien_sound)
score += 1
yPos[i] -= 3000
by -= 500
pygame.rect.Rect(bx, by, bullet.get_width(), bullet.get_height())
# display
screen.fill((0, 0, 0))
screen.blit(ship, (sx, 500))
screen.blit(bullet, (bx, by))
for i in range(55):
screen.blit(alienList[i], (xPos[i], (yPos[i])))
display_scores()
pygame.display.update()