-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
50 lines (44 loc) · 1.63 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
from constants import *
import circleshape
import pygame
from shot import Shot
class Player(circleshape.CircleShape):
def __init__(self, x, y):
super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0
self.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 # pyright: ignore
b = self.position - forward * self.radius - right # pyright: ignore
c = self.position - forward * self.radius + right # pyright: ignore
return [a, b, c]
def draw(self, screen):
pygame.draw.polygon(screen, "white", self.triangle(), 2)
def rotate(self, dt):
self.rotation += PLAYER_TURN_SPEED * dt
def move(self, dt):
fw = pygame.Vector2(0, 1).rotate(self.rotation)
self.position += fw * PLAYER_SPEED * dt
def shoot(self,dt):
if self.cooldown <= 0:
shot = Shot(self.position.x,self.position.y,SHOT_RADIUS)
shot.velocity = pygame.Vector2(0,1).rotate(self.rotation) * PLAYER_SHOT_SPEED
self.cooldown = PLAYER_SHOOT_COOLDOWN
return shot
def update(self, dt):
self.cooldown -= dt
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.rotate(-dt)
if keys[pygame.K_d]:
self.rotate(dt)
if keys[pygame.K_w]:
self.move(dt)
if keys[pygame.K_s]:
self.move(-dt)
if keys[pygame.K_SPACE]:
self.shoot(dt)