-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacewars.py
166 lines (134 loc) · 4.53 KB
/
spacewars.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
import pygame
import random
import math
# from pygame import mixer
# from pygame import mixer_music
pygame.init() #initialize pygame
screen=pygame.display.set_mode((600,700)) #Creating the screen
#For setting the title and icon of the game
pygame.display.set_caption("SpaceWars")
icon=pygame.image.load('spaceship.png')
pygame.display.set_icon(icon)
#background
background=pygame.image.load('space.png')
#bg sound
# mixer.music.load("bg.wav")
# mixer.music.play(-1) #-1 for playing sound on loop
#player Ship
playship=pygame.image.load('playership.png')
playerX=280
playerY=550
playerX_change=0
#alien
alienimg=[]
alienX=[]
alienY=[]
alienX_change=[]
alienY_change=[]
num_enemies=9
for i in range(num_enemies):
alienimg.append(pygame.image.load('alien.png'))
alienX.append(random.randint(0,535)) #using randint to spawwn the alien anywhere between those mentioned values.
alienY.append(random.randint(50,160))
alienX_change.append(3.1)
alienY_change.append(20)
#gameover
game_over=pygame.font.Font("freesansbold.ttf",60)
gameoverX=300
gameoverY=350
#score
score=0
font=pygame.font.Font("freesansbold.ttf",32)
fontX=10
fontY=10
#bullet
bulletimg=pygame.image.load('bullet.png')
bulletX=0 #using randint to spawwn the alien anywhere between those mentioned values.
bulletY=550
bulletX_change=0
bulletY_change=10
bullet_state='ready' # cant see bullet on the screen and fire state means we can see bullet on the screen
def gam_over():
overtext=game_over.render("GAMEOVER",True,(255,255,255))
screen.blit(overtext,(100,350))
def scoce_val(x,y):
score_val=font.render("SCORE: "+ str(score),True,(255,255,255))
screen.blit(score_val,(x,y))
def player(x,y):
screen.blit(playship,(x,y)) #blit means drawing,here drawing player ship on the screen
def alien(x,y,i):
screen.blit(alienimg[i],(x,y))
def bullet(x,y):
global bullet_state
bullet_state='fire'
screen.blit(bulletimg,(x+16,y+10))
def collision(alienX,alienY,bulletX,bulletY):
distance=math.sqrt((math.pow(alienX-bulletX,2)) +(math.pow(alienY-bulletY,2)))
if distance<27:
return True
else:
return False
#To avoid the closing of window quickly
running=True
while running:
screen.fill((0,0,0)) #RGB values
screen.blit(background,(0,0)) #adding background
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
#Checking keystroke if it is right or left
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
# print("Left Key")
playerX_change=-5
if event.key == pygame.K_RIGHT:
# print("Right key")
playerX_change=5
if event.key==pygame.K_SPACE:
if bullet_state=='ready':
bulletX=playerX
bullet(bulletX,bulletY)
if event.type==pygame.KEYUP:
if event.key==pygame.K_LEFT or event.key == pygame.K_RIGHT:
# print("key pressed was released")
playerX_change=0
playerX+=playerX_change #adding playerX_change to x coordinate to move the spaceship.
#Creating Boundaries for the spaceship
if playerX <=0:
playerX=0
elif playerX >= 536:
playerX=536
#moving the alien
for i in range(num_enemies):
if alienY[i]>500:
for j in range(num_enemies):
alienY[j]=2000
gam_over()
break
alienX[i]+=alienX_change[i]
if alienX [i]<=0:
alienX_change[i]=3.1
alienY[i]+=alienY_change[i]
elif alienX[i] >= 536:
alienX_change[i]=-3.1
alienY[i]+=alienY_change[i]
#collision
collide=collision(alienX[i],alienY[i],bulletX,bulletY)
if collide:
bulletY=550
bullet_state="ready"
score+=1
print(score)
alienX[i]=random.randint(0,600) #spawn the alien again.
alienY[i]=random.randint(50,160)
alien(alienX[i],alienY[i],i) #calling the alien function
#moving the bullet
if bulletY<=0:
bulletY=550
bullet_state='ready'
if bullet_state=='fire':
bullet(bulletX,bulletY)
bulletY-=bulletY_change
player(playerX,playerY) #Calling the player function from above
scoce_val(fontX,fontY)
pygame.display.update() #to update anything inside the game window running