-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.py
81 lines (61 loc) · 2.98 KB
/
Ship.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
import math
import numpy as np
import pygame
from Parameters import *
class Ship(pygame.sprite.Sprite):
def __init__(self, x, y, color, speed, theta):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# position
self.pos = np.array([x, y]).astype(float)
# speed
self.theta = theta
self.speedVector = np.array([speed * math.cos(self.theta), speed * math.sin(self.theta)])
# acceleration
self.thetaSpeed = 0.0
self.acceleration = 0.0
self.color = color
self.firing = False
self.firingStartDate = -1.0
# unpowered ship image
self.unpoweredShipImage = pygame.Surface((SHIP_SIZE * 2, SHIP_SIZE + 1))
self.unpoweredShipImage.set_colorkey((0, 0, 0))
pygame.draw.aalines(self.unpoweredShipImage, self.color, False,
[(0, 0), (SHIP_SIZE * 2, SHIP_SIZE * 0.5), (0, SHIP_SIZE)], 1)
pygame.draw.aalines(self.unpoweredShipImage, self.color, False,
[(SHIP_SIZE * 0.5, SHIP_SIZE * 0.125), (SHIP_SIZE * 0.5, SHIP_SIZE * 0.875)], 1)
# powered ship image
self.poweredShipImage = self.unpoweredShipImage.copy()
pygame.draw.aalines(self.poweredShipImage, (255, 0, 0), False,
[(SHIP_SIZE * 0.5, SHIP_SIZE * 0.25), (0, SHIP_SIZE * 0.5),
(SHIP_SIZE * 0.5, SHIP_SIZE * 0.75)], 1)
# generating rect
self.rect = pygame.Rect(self.pos[0], self.pos[1], SHIP_SIZE, SHIP_SIZE * 2)
self.rect.center = (self.pos[0], self.pos[1])
self.mask = pygame.mask.from_surface(self.unpoweredShipImage)
self.image = self.unpoweredShipImage
def update(self, delta):
if SHIP_USE_MOUSE:
thetaVector = pygame.mouse.get_pos() - self.pos
thetaVector /= np.linalg.norm(thetaVector)
self.theta = math.atan2(thetaVector[1], thetaVector[0])
else:
self.theta += delta * self.thetaSpeed
thetaVector = np.array([math.cos(self.theta), math.sin(self.theta)])
self.speedVector += delta * self.acceleration * thetaVector
self.speedVector *= SHIP_DRAG_COEFF
self.pos += delta * self.speedVector
self.pos = np.mod(self.pos, [SCREEN_WIDTH, SCREEN_HEIGHT])
if self.acceleration > 0.0:
self.image = pygame.transform.rotozoom(self.poweredShipImage, -self.theta * 180 / math.pi, 1.0)
else:
self.image = pygame.transform.rotozoom(self.unpoweredShipImage, -self.theta * 180 / math.pi, 1.0)
self.image.set_colorkey((0, 0, 0))
self.rect = self.image.get_rect(center=(self.pos[0], self.pos[1]))
self.mask = pygame.mask.from_surface(self.unpoweredShipImage)
def toggleFire(self, toggle):
oldFiring = self.firing
self.firing = toggle
if toggle != oldFiring:
if toggle:
self.firingStartDate = pygame.time.get_ticks()