-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
54 lines (45 loc) · 1.65 KB
/
player.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
import pygame
import circleshape as cs
import constants as c
import shot as s
class Player(cs.CircleShape):
def __init__(self, x, y, radius):
super().__init__(x, y, radius)
self.rotation = 0
self.shoot_cooldown = 0
# in the player class
def triangle(self):
forward = pygame.Vector2(0, 1).rotate(self.rotation)
right = pygame.Vector2(0, 1).rotate(self.rotation + 90) * self.radius / 1.5
a = self.position + forward * self.radius
b = self.position - forward * self.radius - right
c = self.position - forward * self.radius + right
return [a, b, c]
def draw(self, screen):
pygame.draw.polygon(screen, "white", self.triangle(), width=2)
def rotate(self, dt):
self.rotation += (c.PLAYER_TURN_SPEED * dt)
def update(self, dt):
key = pygame.key.get_pressed()
if key[pygame.K_a]:
self.rotate(dt * -1)
if key[pygame.K_d]:
self.rotate(dt)
if key[pygame.K_w]:
self.move(dt)
if key[pygame.K_s]:
self.move(dt * -1)
if key[pygame.K_SPACE]:
self.shoot()
self.shoot_cooldown -= dt
def move(self, dt):
forward = pygame.Vector2(0, 1).rotate(self.rotation)
self.position += forward * c.PLAYER_SPEED * dt
def shoot(self):
if self.shoot_cooldown > 0:
return
shot = s.Shot(self.position.x, self.position.y, c.SHOT_RADIUS)
shot.velocity = pygame.Vector2(0, 1)
shot.velocity = shot.velocity.rotate(self.rotation)
shot.velocity *= c.PLAYER_SHOOT_SPEED
self.shoot_cooldown = c.PLAYER_SHOOT_COOLDOWN