forked from Flosener/asteroids
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.py
More file actions
43 lines (30 loc) · 1.54 KB
/
Copy pathBullet.py
File metadata and controls
43 lines (30 loc) · 1.54 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
import pygame
import helper as H
class Bullet(pygame.sprite.Sprite):
""" Bullet object class for the asteroids game. """
def __init__(self, player):
""" Initialize a bullet object. """
super().__init__()
# Load and scale bullet image
self.scale = 20
# Bullet: https://www.flaticon.com/free-icon/bullet_224681?term=bullet&page=1&position=1&page=1&position=1&related_id=224681&origin=search
self.img = pygame.image.load("resources/bullet.png")
self.img = pygame.transform.scale(self.img, (self.scale//2, self.scale))
# Initial bullet position/rotation is the spaceship's direction/angle
self.direction, self.angle, self.sin, self.cos = player.update_direction()
self.pos_x, self.pos_y = self.direction
self.img = pygame.transform.rotozoom(self.img, self.angle, 1)
# Collision rect and speed
self.rect = self.img.get_rect(center=(self.pos_x, self.pos_y))
self.speed = 7
def move(self):
""" Method to update position of bullet. """
# Remove object at borders
if self.pos_x <= 0 or self.pos_x >= H.WIDTH:
self.kill()
if self.pos_y <= 0 or self.pos_y >= H.HEIGHT:
self.kill()
# Use agent's cosine and sine for updating bullet position
self.pos_x += self.cos * self.speed
self.pos_y -= self.sin * self.speed
self.rect = self.img.get_rect(center=(self.pos_x, self.pos_y))