-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameSprites.py
More file actions
242 lines (178 loc) · 7.19 KB
/
gameSprites.py
File metadata and controls
242 lines (178 loc) · 7.19 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# from typing_extensions import Self
from copyreg import constructor
from importlib.metadata import packages_distributions
from operator import truediv
from re import L, T
from sys import displayhook
from tkinter import W
from turtle import width
import pygame
bullet_width = 10
bullet_height = 33
spaceship_width = 50
spaceship_height = 62
class Bullet():
def __init__(self,surface):
self.bullet = pygame.image.load('./assets/img/bullet2.png')
self.surface = surface
self.bullet_rect = self.bullet.get_rect()
self.bullet_rect.x = pygame.mouse.get_pos()[0]+spaceship_width/2-bullet_width/2
self.bullet_rect.y = pygame.mouse.get_pos()[1]-bullet_height
self.bullet_velocity = 10
self.bullet_width = 10
self.bullet_height = 33
def Move(self):
self.bullet_rect.y -= self.bullet_velocity
self.surface.blit(self.bullet,(self.bullet_rect.x,self.bullet_rect.y))
def getPos(self):
return (self.bullet_rect.x,self.bullet_rect.y)
def getRect(self):
return self.bullet_rect
class Spaceship():
def __init__(self,surface):
self.spaceship = pygame.image.load('./assets/img/plane.png')
self.spaceship_rect = self.spaceship.get_rect()
self.spaceship_lives = 3
self.spaceship_is_alive = True
self.surface = surface
self.bullets = []
def Move(self):
if self.spaceship_is_alive:
self.spaceship_rect.x = pygame.mouse.get_pos()[0]
self.spaceship_rect.y = pygame.mouse.get_pos()[1]
self.surface.blit(self.spaceship,(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]))
if len(self.bullets) > 0:
for i in self.bullets:
i.Move()
def getRect(self):
return self.spaceship_rect
def Shoot(self):
newbullet = Bullet(self.surface)
self.bullets.append(newbullet)
newbullet = None
def isCollided(self,enbullets):
if len(enbullets) > 0:
for i in enbullets:
if self.spaceship_rect.colliderect(i.getRect()):
self.spaceship_lives -= 1
enbullets.remove(i)
if self.spaceship_lives == 0:
self.spaceship_is_alive = False
pygame.mouse.set_visible(True)
return enbullets
class EnemyBullet():
def __init__(self,surface,x,y):
self.bullet = pygame.image.load('./assets/img/bullet2.png')
self.bullet_rect = self.bullet.get_rect()
self.surface = surface
self.bullet_rect.x = x
self.bullet_rect.y = y
self.direction = "upward"
self.bullet_velocity = 5
def setVelocity(self,vel):
self.bullet_velocity = vel
def setDirection(self,dr):
self.direction = dr
def getPos(self):
return (self.bullet_rect.x,self.bullet_rect.y)
def Move(self):
self.bullet_rect.y += self.bullet_velocity
self.surface.blit(self.bullet,(self.bullet_rect.x,self.bullet_rect.y))
def getRect(self):
return self.bullet_rect
class Enemy():
def __init__(self,surface,x,y):
self.enemy = pygame.image.load('./assets/img/enemy.png')
self.bg = pygame.image.load('./assets/img/background.jpg')
self.enemy_rect = self.enemy.get_rect()
self.enemy_rect.x = x
self.enemy_rect.y = y
self.enemyspaceship_width = 75
self.enemyspaceship_height = 68
self.enemy_velocity = 2
self.surface = surface
self.enemybullets = []
self.life_counter = 0
def Move(self):
self.enemy_rect.y += self.enemy_velocity
self.surface.blit(self.enemy,(self.enemy_rect.x,self.enemy_rect.y))
def Shoot(self):
self.bulletimg = pygame.image.load('./assets/img/bullet.png')
newbullet = EnemyBullet(self.surface,self.enemy_rect.x+self.enemyspaceship_width/2,self.enemy_rect.y+self.enemyspaceship_height)
self.enemybullets.append(newbullet)
newbullet = None
def MoveEnemybullets(self):
if len(self.enemybullets) > 0:
for bl in self.enemybullets:
if bl.getPos()[1] >= 600:
self.enemybullets.remove(bl)
else:
bl.Move()
def getAllBullets(self):
if len(self.enemybullets) > 0:
return self.enemybullets
else:
return None
def isBulletsCollided(self,plbull):
if len(plbull) > 0 and len(self.enemybullets) > 0:
for i in plbull:
for j in self.enemybullets:
if i.getRect().colliderect(j.getRect()):
plbull.remove(i)
self.enemybullets.remove(j)
return True
def getRect(self):
return self.enemy_rect
def isEnemyCollided(self,enemies,plbull):
if len(enemies) > 0:
for i in plbull:
for j in enemies:
if i.getRect().colliderect(j.getRect()):
self.life_counter += 1
if self.life_counter == 3:
self.life_counter = 0
enemies.remove(j)
plbull.remove(i)
return True
plbull.remove(i)
class Ticker():
def __init__(self,ticks,tickspeed):
self.total_ticks = ticks
self.speed_of_tick = tickspeed
self.timeOver = True
self.tick_counter = 0
def tick(self):
if self.isticksOver() != True:
self.tick_counter = self.tick_counter+self.speed_of_tick
elif self.isticksOver() == True:
self.tick_counter = 0
def isticksOver(self):
if self.tick_counter >= self.total_ticks:
self.tick_counter = 0
return self.timeOver
def Reset(self):
self.tick_counter = 0
def getAllBullets(self):
if len(self.enemybullets) > 0:
return self.enemybullets
else:
return None
class Score():
def __init__(self,surface,fr_width,fr_height):
self.score = 0
self.surface = surface
self.fr_width = fr_width
self.fr_height = fr_height
self.color = (255,255,255)
# self.display = True
def add(self,inc):
self.score += inc
def ChangeColor(self,c_color):
self.color = c_color
def getScore(self):
return self.score
def update(self):
font = pygame.font.SysFont('Comic Sans ms', 50)
img = font.render(f'Score {self.score} ', True, self.color)
self.surface.blit(img, (self.fr_width/7-img.get_rect().centerx, 20))
# pygame.display.update()