forked from Flosener/asteroids
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.py
More file actions
75 lines (59 loc) · 2.84 KB
/
Copy pathPlayer.py
File metadata and controls
75 lines (59 loc) · 2.84 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
import math
import helper as H
class Player(pygame.sprite.Sprite):
""" Agent object class for the asteroids game. """
def __init__(self):
""" Initialize a player object. """
super().__init__()
# Load and scale player image
self.scale = 50
# spaceship: https://www.flaticon.com/free-icon/spaceship_901787
self.image = pygame.image.load("resources/spaceship.png")
self.image = pygame.transform.scale(self.image, (self.scale, self.scale))
# Get inital position of player in middle of screen
self.pos_x = H.WIDTH//2
self.pos_y = H.HEIGHT//2
# Rotation and collision rectangle
self.angle = 0
# rotozoom instead rotate improves quailty of rotated image
self.img = pygame.transform.rotozoom(self.image, self.angle, 1)
self.rect = self.img.get_rect(center=(self.pos_x, self.pos_y))
# Track the direction the spaceship is facing for forward movement:
# Calculate sin/cos of radians of the angle by which player is turning
# Add this to the current position for x and y
self.speed = 5
self.sin = math.sin(math.radians(self.angle + 90))
self.cos = math.cos(math.radians(self.angle + 90))
self.direction = (self.pos_x + self.cos * self.scale/2,
self.pos_y - self.sin * self.scale/2)
def move(self, thrust, rotate):
""" Method to update position and rotation of our player object. """
# If player is at screen borders, it cannot move further
if self.pos_x <= 0:
self.pos_x = 0
if self.pos_x >= H.WIDTH:
self.pos_x = H.WIDTH
if self.pos_y <= 0:
self.pos_y = 0
if self.pos_y >= H.HEIGHT:
self.pos_y = H.HEIGHT
# Moving forward with inputs from NN
if thrust > 0.95:
self.pos_x += self.cos * self.speed
self.pos_y -= self.sin * self.speed
self.update_direction()
# Rotating with inputs from NN
if rotate < -0.95 or rotate > 0.95:
self.angle += 5 * rotate
self.update_direction()
def update_direction(self):
""" Function to update the direction the spaceship is facing. """
self.img = pygame.transform.rotozoom(self.image, self.angle, 1)
self.rect = self.img.get_rect(center=(self.pos_x, self.pos_y))
self.sin = math.sin(math.radians(self.angle + 90))
self.cos = math.cos(math.radians(self.angle + 90))
self.direction = (self.pos_x + self.cos * self.scale/2,
self.pos_y - self.sin * self.scale/2)
# Return list to be used in Bullet class
return self.direction, self.angle, self.sin, self.cos