-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.py
109 lines (85 loc) · 2.93 KB
/
bullet.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
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
import pygame
from character import Enemy
from abc import ABC, abstractmethod
import random
from math import cos, sin
class Bullet(ABC, Enemy):
"""
A general bullet superclass that inherits ABC and Enemy class
Attributes:
pos_x: int representing location in x coordinate
pos_y: int representing location in y coordinate
speed: int representing change in location with respect to time
sprite: string representing source image path
"""
def __init__(self):
"""
Initializes a Bullet instance
"""
self.pos_x = None
self.pos_y = None
self.speed = None
self.sprite = None
@abstractmethod
def update(self):
"""
Update position of the instance
"""
pass
class ReimBullet(Bullet):
"""
User Bullet that inherits the Bullet class
Attributes:
pos_x: int representing location in x coordinate
pos_y: int representing location in y coordinate
speed: int representing change in location with respect to time
sprite: string representing source image path
"""
def __init__(self, pos_x, pos_y):
"""
Initialzies the ReimBullet instance
Args:
pos_x: an int representing the x coordinate
pos_y: an int representing the y coordinate
"""
self._pos_x = pos_x
self._pos_y = pos_y
self._speed = 8
self._sprite = pygame.image.load("bullet.png")
def update(self):
"""
Update position of the instance
"""
self.set_pos_y(self._pos_y - self._speed)
class EnemyBullet(ReimBullet):
"""
Enemy Bullet that inherits ReimBullet class
Attributes:
pos_x: int representing location in x coordinate
pos_y: int representing location in y coordinate
speed: int representing change in location with respect to time
sprite: string representing source image path
angle: int representing the angle that the bullet fires
"""
def __init__(self, pos_x, pos_y):
"""
Initialzies the ReimBullet instance
Args:
pos_x: an int representing the x coordinate
pos_y: an int representing the y coordinate
"""
self._pos_x = pos_x
self._pos_y = pos_y
self._speed = 2
self._sprite = pygame.image.load("enemy_bullet.png")
self._angle = random.randint(-30, 30)
def update(self):
"""
Update position of the instance with speed components in x and y
To shoot downwards and in a certain angle, each of the components
is calculated and then the y component is positive
"""
speed_y = self._speed * sin(self._angle)
speed_x = self._speed * cos(self._angle)
self.set_pos_y(self._pos_y + abs(speed_y))
self.set_pos_x(self._pos_x + speed_x)