-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
76 lines (60 loc) · 2.24 KB
/
player.py
File metadata and controls
76 lines (60 loc) · 2.24 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
72
73
74
75
import pygame
from circleshape import CircleShape
from constants import *
from shot import Shot
class Player(CircleShape):
def __init__(self, x, y):
super().__init__(x, y, PLAYER_RADIUS)
self.rotation = 0
surface_size = int(self.radius * 4) # Make it a bit bigger than needed
self.image = pygame.Surface((surface_size, surface_size), pygame.SRCALPHA)
self.rect = self.image.get_rect(center=(x, y))
self.update_image()
self.timer = 0
# in the player class
def triangle(self):
center = pygame.Vector2(self.image.get_width() / 2, self.image.get_height() / 2)
forward = pygame.Vector2(0, 1).rotate(self.rotation)
right = pygame.Vector2(0, 1).rotate(self.rotation + 90) * self.radius / 1.5
a = center + forward * self.radius
b = center - forward * self.radius - right
c = center - forward * self.radius + right
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):
forward = pygame.Vector2(0, 1).rotate(self.rotation)
self.position += forward * PLAYER_SPEED * dt
self.rect.center = self.position
def update_image(self):
# Clear the previous drawing
self.image.fill((0, 0, 0, 0))
# Get triangle points and draw them
points = self.triangle()
pygame.draw.polygon(self.image, "white", points, 2)
def update(self, 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()
self.timer -= dt
self.update_image()
def shoot(self):
if self.timer > 0:
pass
else:
x = self.position.x
y = self.position.y
shot = Shot(x, y, SHOT_RADIUS)
shot.velocity = pygame.Vector2(0, 1).rotate(self.rotation)
shot.velocity *= PLAYER_SHOOT_SPEED
self.timer = 0.3