-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
71 lines (56 loc) · 2.11 KB
/
player.py
File metadata and controls
71 lines (56 loc) · 2.11 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
from circleshape import CircleShape
from constants import *
import pygame
from shot import Shot
# inherited class from CircleShape
class Player(CircleShape):
def __init__(self, x, y):
# call the parent constructor
super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0
self.cooldown_timer = 0
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):
# draw the triangle
pygame.draw.polygon(screen, (255, 255, 255), self.triangle(), width=2)
def rotate(self, dt):
# rotate the player
return self.rotation + dt * PLAYER_TURN_SPEED
def update(self, dt):
keys = pygame.key.get_pressed()
# check for key presses
if keys[pygame.K_a]:
# rotate left
self.rotation = self.rotate(-dt)
if keys[pygame.K_d]:
# rotate right
self.rotation = self.rotate(dt)
if keys[pygame.K_w]:
# move forward
self.move(dt)
if keys[pygame.K_s]:
# move backward
self.move(-dt)
if keys[pygame.K_SPACE]:
# shoot
if self.cooldown_timer > 0:
self.cooldown_timer -= dt
return
self.shoot()
self.cooldown_timer = PLAYER_SHOOT_COOLDOWN
def move(self, dt):
# move the player
forward = pygame.Vector2(0, 1).rotate(self.rotation)
self.position += forward * PLAYER_SPEED * dt
def shoot(self):
# shoot a bullet
forward = pygame.Vector2(0, 1).rotate(self.rotation)
shot = Shot(self.position.x, self.position.y, SHOT_RADIUS)
shot.velocity = forward * PLAYER_SHOT_SPEED
# The containers will handle adding it to the right groups