-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
60 lines (45 loc) · 2.01 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
55
56
57
58
59
60
import pygame
from bullet import Bullet
from circleshape import CircleShape
from constants import PLAYER_RADIUS, PLAYER_COLOR, PLAYER_WIDTH, PLAYER_TURN_SPEED, PLAYER_MOVE_SPEED, BULLET_RADIUS, BULLET_SPEED, BULLET_RATE_OF_FIRE
class Player(CircleShape):
def __init__(self, x, y):
super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0
self.__shot_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):
pygame.draw.polygon(screen, PLAYER_COLOR, self.triangle(), PLAYER_WIDTH)
def update(self, delta_time):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rotate(-delta_time)
if keys[pygame.K_RIGHT]:
self.rotate(delta_time)
if keys[pygame.K_UP]:
self.move(delta_time)
if keys[pygame.K_DOWN]:
self.move(-delta_time)
self.decreaseShotTimer(delta_time)
if keys[pygame.K_SPACE]:
self.shoot()
def rotate(self, delta_time):
self.rotation += PLAYER_TURN_SPEED * delta_time
def move(self, delta_time):
forward = pygame.Vector2(0, 1).rotate(self.rotation)
self.position += forward * PLAYER_MOVE_SPEED * delta_time
def shoot(self):
if self.__shot_timer <= 0:
bullet = Bullet(self.position.x, self.position.y, BULLET_RADIUS)
bullet.velocity = pygame.Vector2(0, 1).rotate(self.rotation) * BULLET_SPEED
self.setShotTimer(BULLET_RATE_OF_FIRE)
def setShotTimer(self, delta_time):
self.__shot_timer = BULLET_RATE_OF_FIRE
def decreaseShotTimer(self, delta_time):
self.__shot_timer -= delta_time