This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerShip.py
More file actions
82 lines (74 loc) · 3.82 KB
/
PlayerShip.py
File metadata and controls
82 lines (74 loc) · 3.82 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
from Ship import *
from pygame.locals import *
from Globals import *
import math
# This is the player ship object in the Game World. It has a nice initializer to create the Ship.
# It inherits from the Ship class.
class PlayerShip(Ship):
def __init__(self, position, folder_name):
self.health = initial_health
self.angle = 0.0
self.folder_name = folder_name
self.fuel = 0
player_folder = str(folder_name) + "/PlayerShip" + str(self.angle) + ".png"
Ship.__init__(self, position, classic_ship_location + "/PlayerShip" + str(self.angle) + ".png", (ship_scale, ship_scale))
self.image = pygame.image.load(str(self.folder_name) + "/PlayerShip" + str(self.angle) + ".png")
self.image = pygame.transform.scale(self.image, (ship_scale, ship_scale)) # Scale image
self.image.set_colorkey((0, 0, 0, 0))
# Overrides the Object's move so that rotating the ship is possible.
def move(self, x_vel, screen_height):
self.fuel = min(max_fuel, self.fuel)
# Water Friction! - Idk what else to call it!
friction = -self.velocity[1] * friction_ratio
self.velocity = (self.velocity[0], self.velocity[1] + friction)
if abs(self.velocity[1]) < 0.01:
self.velocity = (self.velocity[0], 0)
if self.rect.top <= 0:
self.rect.top = 0
self.velocity = (self.velocity[0], max(0, self.velocity[1]))
if self.rect.bottom >= screen_height:
self.rect.bottom = screen_height
self.velocity = (self.velocity[0], min(0, self.velocity[1]))
# Calculate angle using some good ol' Pythagorean Theorem
new_angle = 0.0
if self.velocity[1] != 0:
number_of_images_used = (90 / angle_deviations) - 1 # This is calculated based on how many images we use.
angle_proportion = math.ceil(abs(self.velocity[1]) / max_y_vel * number_of_images_used)
new_angle = angle_proportion * angle_deviations # Calculate the right angle based on image names
if self.velocity[1] > 0 and new_angle != 0: # Since we only checked for angles in the range [0, 90]
new_angle = 360 - new_angle
# if angle has changed than what it was before switch out the image and set some properties
if self.angle != new_angle:
self.angle = new_angle
center_point = self.rect.center
self.image = pygame.image.load(str(self.folder_name) + "/PlayerShip" + str(self.angle) + ".png")
self.image = pygame.transform.scale(self.image, (ship_scale, ship_scale)) # Scale image
self.image.set_colorkey((0, 0, 0, 0))
# set the rectangle defined for this image for collision detection
self.rect = self.image.get_rect()
self.rect.center = center_point # Reset the position
Object.move(self) # Call super.move so that everything gets updated like usual.
# Use this method to update the health. It returns true if the player ship is alive and
# false if the player ship has no health left.
def damage(self, damage_done):
self.health -= damage_done
def input(self, keys):
if keys[K_UP]:
self.fuel -= reduce_fuel
if self.fuel < 0:
self.fuel = 0
self.velocity = (self.velocity[0], self.velocity[1] - move_amount)
if keys[K_DOWN]:
self.fuel -= reduce_fuel
if self.fuel < 0:
self.fuel = 0
self.velocity = (self.velocity[0], self.velocity[1] + move_amount)
self.velocity = (self.velocity[0], min(max(min_y_vel, self.velocity[1]), max_y_vel))
def normalize_angle(x):
new_angle = math.degrees(x)
while new_angle < 0 or new_angle > 90:
if new_angle < 0:
new_angle += 90
else:
new_angle -= 90
return new_angle