-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.py
More file actions
176 lines (135 loc) · 4.45 KB
/
game.py
File metadata and controls
176 lines (135 loc) · 4.45 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
import pygame
import random
import math
from pygame import mixer
# initializing pygame
pygame.init()
# creating screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# caption and icon
pygame.display.set_caption("Welcome to Space Invaders Game by:- styles")
# Score
score_val = 0
scoreX = 5
scoreY = 5
font = pygame.font.Font('freesansbold.ttf', 20)
# Game Over
game_over_font = pygame.font.Font('freesansbold.ttf', 64)
def show_score(x, y):
score = font.render("Points: " + str(score_val), True, (255,255,255))
screen.blit(score, (x , y ))
def game_over():
game_over_text = game_over_font.render("GAME OVER", True, (255,255,255))
screen.blit(game_over_text, (190, 250))
# Background Sound
mixer.music.load('data/background.wav')
mixer.music.play(-1)
# player
playerImage = pygame.image.load('data/spaceship.png')
player_X = 370
player_Y = 523
player_Xchange = 0
# Invader
invaderImage = []
invader_X = []
invader_Y = []
invader_Xchange = []
invader_Ychange = []
no_of_invaders = 8
for num in range(no_of_invaders):
invaderImage.append(pygame.image.load('data/alien.png'))
invader_X.append(random.randint(64, 737))
invader_Y.append(random.randint(30, 180))
invader_Xchange.append(1.2)
invader_Ychange.append(50)
# Bullet
# rest - bullet is not moving
# fire - bullet is moving
bulletImage = pygame.image.load('data/bullet.png')
bullet_X = 0
bullet_Y = 500
bullet_Xchange = 0
bullet_Ychange = 3
bullet_state = "rest"
# Collision Concept
def isCollision(x1, x2, y1, y2):
distance = math.sqrt((math.pow(x1 - x2,2)) + (math.pow(y1 - y2,2)))
if distance <= 50:
return True
else:
return False
def player(x, y):
screen.blit(playerImage, (x - 16, y + 10))
def invader(x, y, i):
screen.blit(invaderImage[i], (x, y))
def bullet(x, y):
global bullet_state
screen.blit(bulletImage, (x, y))
bullet_state = "fire"
# game loop
running = True
while running:
# RGB
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Controling the player movement from the arrow keys
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_Xchange = -1.7
if event.key == pygame.K_RIGHT:
player_Xchange = 1.7
if event.key == pygame.K_SPACE:
# Fixing the change of direction of bullet
if bullet_state is "rest":
bullet_X = player_X
bullet(bullet_X, bullet_Y)
bullet_sound = mixer.Sound('data/bullet.wav')
bullet_sound.play()
if event.type == pygame.KEYUP:
player_Xchange = 0
# adding the change in the player position
player_X += player_Xchange
for i in range(no_of_invaders):
invader_X[i] += invader_Xchange[i]
# bullet movement
if bullet_Y <= 0:
bullet_Y = 600
bullet_state = "rest"
if bullet_state is "fire":
bullet(bullet_X, bullet_Y)
bullet_Y -= bullet_Ychange
# movement of the invader
for i in range(no_of_invaders):
if invader_Y[i] >= 450:
if abs(player_X-invader_X[i]) < 80:
for j in range(no_of_invaders):
invader_Y[j] = 2000
explosion_sound = mixer.Sound('data/explosion.wav')
explosion_sound.play()
game_over()
break
if invader_X[i] >= 735 or invader_X[i] <= 0:
invader_Xchange[i] *= -1
invader_Y[i] += invader_Ychange[i]
# Collision
collision = isCollision(bullet_X, invader_X[i], bullet_Y, invader_Y[i])
if collision:
score_val += 1
bullet_Y = 600
bullet_state = "rest"
invader_X[i] = random.randint(64, 736)
invader_Y[i] = random.randint(30, 200)
invader_Xchange[i] *= -1
invader(invader_X[i], invader_Y[i], i)
# restricting the spaceship so that it doesn't go out of screen
if player_X <= 16:
player_X = 16;
elif player_X >= 750:
player_X = 750
player(player_X, player_Y)
show_score(scoreX, scoreY)
pygame.display.update()