Repository navigation
Expand file tree
/
Copy pathobjects.py
More file actions
137 lines (112 loc) · 4.73 KB
/
Copy pathobjects.py
File metadata and controls
137 lines (112 loc) · 4.73 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import math
import random
from copy import copy
import pygame
class Asteroid(pygame.sprite.Sprite):
def __init__(self, location, size, *groups: pygame.sprite.Group):
pygame.sprite.Sprite.__init__(self, *groups) # inherits features of Sprite() class to this class
self.speed = pygame.Vector2(1, 1).copy()
self.speed.rotate_ip(random.randint(0, 360))
self.position = pygame.Vector2(location).copy()
self.size = size
if self.size == 1:
self.image = pygame.image.load("assets/asteroid1.png")
elif self.size == 2:
self.image = pygame.image.load("assets/asteroid2.png")
elif self.size == 3:
self.image = pygame.image.load("assets/asteroid3.png")
self.image = pygame.transform.rotate(self.image, random.randint(0, 360))
self.rect = self.image.get_rect()
self.rect.center = self.position
def update(self):
self.position += self.speed
self.rect.center = self.position
# wrap around screen edges
if self.position.x > 1024:
self.position.x = 0
if self.position.x < 0:
self.position.x = 1024
if self.position.y > 768:
self.position.y = 0
if self.position.y < 0:
self.position.y = 768
def split(self):
if self.size > 1:
for i in range(2):
Asteroid(self.position, self.size - 1, self.groups())
self.kill()
else:
self.kill()
class Bullet(pygame.sprite.Sprite):
def __init__(self, player, *groups: pygame.sprite.Group):
pygame.sprite.Sprite.__init__(self, *groups) # inherits features of Sprite() class to this class
self.image = pygame.image.load("assets/bullet.png")
self.rect = self.image.get_rect()
self.position = copy(player.position)
self.rect.center = self.position
self.timer = 0
self.lifetime = 60 * 1.5
bullet_speed = 6
self.speed = pygame.Vector2(bullet_speed * math.cos(player.angle), bullet_speed * -math.sin(player.angle)) + player.speed
def update(self, *groups: pygame.sprite.Group):
if self.timer > self.lifetime:
self.kill()
else:
self.timer += 1
self.position += self.speed
self.rect.center = self.position
# wrap around screen edges
if self.position.x > 1024:
self.position.x = 0
if self.position.x < 0:
self.position.x = 1024
if self.position.y > 768:
self.position.y = 0
if self.position.y < 0:
self.position.y = 768
class Player(pygame.sprite.Sprite):
def __init__(self, location, *groups: pygame.sprite.Group):
pygame.sprite.Sprite.__init__(self, *groups) # inherits features of Sprite() class to this class
# Fixed Values
self.max_speed = 3
self.r_speed = 0.05
self.acceleration = 0.08
# Altered Values
self.angle = 0
self.speed = pygame.Vector2(0, 0)
self.angle = 0
self.position = pygame.Vector2(location)
# Image and Rect
self.image = pygame.image.load("assets/player.png")
self.image_original = self.image.copy()
self.image_thrust = pygame.image.load("assets/player_thrust.png") # to be changed to a different image w/ blaster on back
self.rect = self.image.get_rect()
def update(self, keys_pressed):
# add or minus r_speed from angle
if keys_pressed[pygame.K_LEFT]:
self.angle += self.r_speed
if keys_pressed[pygame.K_RIGHT]:
self.angle -= self.r_speed
if keys_pressed[pygame.K_DOWN]:
# add acceleration to speed when thrusting
self.speed.x += self.acceleration * math.cos(self.angle)
self.speed.y -= self.acceleration * math.sin(self.angle) # minus because of inverted y-axis
# rotate image according to angle and depending on whether is thrusting
self.image = pygame.transform.rotate(self.image_thrust, math.degrees(self.angle))
else:
self.image = pygame.transform.rotate(self.image_original, math.degrees(self.angle))
# Move the player by speed with max speed
if self.speed.length() > self.max_speed:
self.speed.scale_to_length(self.max_speed)
self.position += self.speed
# wrap position around edges of screen
if self.position.x > 1024:
self.position.x = 0
if self.position.x < 0:
self.position.x = 1024
if self.position.y > 768:
self.position.y = 0
if self.position.y < 0:
self.position.y = 768
# update rect
self.rect = self.image.get_rect(center=self.position)