-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathships.py
More file actions
151 lines (126 loc) · 5.33 KB
/
ships.py
File metadata and controls
151 lines (126 loc) · 5.33 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
import pygame
import os
import random
from lasers import Laser
# Load Images
RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_red_small.png"))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_green_small.png"))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_blue_small.png"))
YELLOW_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_yellow.png"))
# Load lasers
RED_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_red.png"))
GREEN_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_green.png"))
BLUE_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_blue.png"))
YELLOW_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_yellow.png"))
# Abstract ship class
class Ship:
COOLDOWN = 30
def __init__(self, x, y, health=100):
self.x = x
self.y = y
self.health = health
self.ship_img = None
self.laser_img = None
self.lasers = []
self.cool_down_counter = 0
def draw(self, window):
window.blit(self.ship_img, (self.x, self.y))
for laser in self.lasers:
laser.draw(window)
def move_lasers(self, vel, obj, HEIGHT):
self.cooldown()
for laser in self.lasers:
laser.move(vel)
if laser.off_screen(HEIGHT):
self.lasers.remove(laser)
elif laser.collision(obj):
obj.health -= 10
obj.streak = 0
self.lasers.remove(laser)
def cooldown(self):
if self.cool_down_counter >= self.COOLDOWN:
self.cool_down_counter = 0
elif self.cool_down_counter > 0:
self.cool_down_counter += 1
def shoot(self):
if self.cool_down_counter == 0:
laser = Laser(self.x, self.y, self.laser_img)
self.lasers.append(laser)
self.cool_down_counter = 1
def get_width(self):
return self.ship_img.get_width()
def get_height(self):
return self.ship_img.get_height()
# Player object, inherits from Ship
class Player(Ship):
def __init__(self, x, y, health=100):
super().__init__(x, y, health)
self.ship_img = YELLOW_SPACE_SHIP
self.laser_img = YELLOW_LASER
self.mask = pygame.mask.from_surface(self.ship_img)
self.max_health = health
self.streak = 0
def move_lasers(self, vel, objs, special_items, HEIGHT):
self.cooldown()
for laser in self.lasers:
laser.move(vel)
if laser.off_screen(HEIGHT):
self.lasers.remove(laser)
else:
# If lasers hit an enemy, remove them
for obj in objs:
if laser.collision(obj):
objs.remove(obj)
self.streak += 1
if laser in self.lasers:
self.lasers.remove(laser)
# If lasers hit a special object, perform some action
for item in special_items:
if laser.collision(item):
item.on_collision(self)
special_items.remove(item)
print("Hit a special object...")
if laser in self.lasers:
self.lasers.remove(laser)
def draw(self, window):
super().draw(window)
self.healthbar(window)
def healthbar(self, window):
pygame.draw.rect(window, (255,0,0), (self.x, self.y + self.ship_img.get_height() + 10, self.ship_img.get_width(), 10))
pygame.draw.rect(window, (0,255,0), (self.x, self.y + self.ship_img.get_height() + 10, self.ship_img.get_width() * (self.health/self.max_health), 10))
def heal(self, heal_amount):
self.health = 100 if self.health + heal_amount > 100 else self.health + heal_amount
def shoot(self, level=0):
if self.cool_down_counter == 0:
if level < 3:
laser = Laser(self.x, self.y, self.laser_img)
self.lasers.append(laser)
else:
laser1 = Laser(int(self.x - self.get_width()/4), self.y, self.laser_img)
self.lasers.append(laser1)
laser2 = Laser(int(self.x + self.get_width()/4), self.y, self.laser_img)
self.lasers.append(laser2)
self.cool_down_counter = 1
# Enemy Ship
class Enemey(Ship):
COLOR_MAP = {
"red" : (RED_SPACE_SHIP, RED_LASER, 0),
"green": (GREEN_SPACE_SHIP, GREEN_LASER, 3),
"blue" : (BLUE_SPACE_SHIP, BLUE_LASER, -3)
}
def __init__(self, x, y, color, health=100):
super().__init__(x, y, health)
self.ship_img, self.laser_img, self.multiplier = self.COLOR_MAP[color]
self.mask = pygame.mask.from_surface(self.ship_img)
def move(self, vel, WIDTH):
self.y += vel
r_int = random.randrange(0, 200)
if r_int == 1:
self.x -= 15 if self.x - 15 > 0 else 0
elif r_int == 2:
self.x += 15 if self.x + 15 + self.get_width() < WIDTH else 0
def shoot(self):
if self.cool_down_counter == 0:
laser = Laser(self.x-20, self.y, self.laser_img, self.multiplier)
self.lasers.append(laser)
self.cool_down_counter = 1