-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceShipSprite.py
107 lines (93 loc) · 4.61 KB
/
SpaceShipSprite.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
from DestructableObject import DestructableObject
from Projectile import Projectile
import pygame
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE, K_LEFT, K_RIGHT, K_SPACE, K_a, K_d,K_UP,K_DOWN
class SpaceShipSprite(DestructableObject):
deadProjectiles =0;
liveProjectiles =[]
def __init__(self,screen,image,attackDmg= 1,damage=0,health=4,startPosition=(0.0,0.0),ammoMax=5, name="SpaceShip-"):
super().__init__(screen,image,attackDmg,damage,health,startPosition)
self.rect = (image)
self.name = name
self.title = self.font.render(self.name,True, DestructableObject.WHITE)
self.currentAmmo = ammoMax
self.ammoMax = ammoMax
self.screen = screen
self.lives =1
self.maxhp=health
self.ammoCounter =.3
self.firedAt=0
self.maxProjectiles_on_screen =15
self.action_status = 0
SpaceShipSprite.liveProjectiles =[]
def move(self,keys):
self.action_status = 0
event = "none"
if isinstance(keys,type(pygame.key.get_pressed())):
if len(keys)>0:
if (keys[K_SPACE]):
self.fireprojectile()
elif (keys[K_RIGHT]):
event ="right"
elif (keys[K_LEFT]):
event ="left"
elif (keys[K_UP]):
event ="up"
elif (keys[K_DOWN]):
event ="down"
elif isinstance(keys,str):
event = keys
if keys=="space" and len(SpaceShipSprite.liveProjectiles) < self.maxProjectiles_on_screen:
self.fireprojectile()
self.currentPos = self.updatePosition(event)
self.boundsCheck()
return self.action_status
def updatePosition(self,argument):
switcher = {
"left": lambda: (self.currentPos[0]-self.movespeed,self.currentPos[1]),
"right": lambda: (self.currentPos[0]+self.movespeed,self.currentPos[1]),
"up": lambda: (self.currentPos[0],self.currentPos[1]-self.movespeed),
"down": lambda: (self.currentPos[0],self.currentPos[1]+self.movespeed),
}
res = switcher.get(argument,"Invalid Movement")
return self.currentPos if res == "Invalid Movement" else res()
def boundsCheck(self):
if(self.CheckOutOfBounds("ymin")):
self.currentPos = (self.currentPos[0],5)
self.action_status = - 1
elif(self.CheckOutOfBounds("ymax")):
self.currentPos = (self.currentPos[0],DestructableObject.bounds[1]-self.image_size[1]-5)
self.action_status = - 1
elif(self.CheckOutOfBounds("xmin")):
self.currentPos = (5,self.currentPos[1])
self.action_status = - 1
elif(self.CheckOutOfBounds("xmax")):
self.currentPos = (DestructableObject.bounds[0]-self.image_size[0]-5,self.currentPos[1])
self.action_status = - 1
def fireprojectile(self):
newprojectile = Projectile(self.screen,r'Assets\imgs\bullet1.png',movespeed=-8,startPosition=(self.currentPos[0],self.currentPos[1]-14))
mps = pygame.time.get_ticks()/1000 - self.firedAt/1000
if self.currentAmmo > 0 :
if mps > 1 and self.currentAmmo < self.ammoMax:
self.currentAmmo += 1
if mps > .4:
self.firedAt = pygame.time.get_ticks()
SpaceShipSprite.liveProjectiles.append(newprojectile)
self.currentAmmo-=1
else:
self.action_status = -1
else:
if mps >= self.ammoCounter:
self.currentAmmo = self.ammoMax
self.fireprojectile()
else:
self.action_status = -1
def takeDamage(self,obs):
if self.health > 0:
self.health -= obs.attackDmg
else:
self.lives -= 1
self.health= self.maxhp
if self.lives ==0:
return False
return True