-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathbullet.py
34 lines (26 loc) · 955 Bytes
/
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
import pygame
from settings import BULLET_SPEED, HEIGHT
class Bullet(pygame.sprite.Sprite):
def __init__(self, pos, size, side):
super().__init__()
self.x = pos[0]
self.y = pos[1]
# bullet info
img_path = f'assets/bullet/{side}-bullet.png'
self.image = pygame.image.load(img_path)
self.image = pygame.transform.scale(self.image, (size, size))
self.rect = self.image.get_rect(topleft = pos)
self.mask = pygame.mask.from_surface(self.image)
# different bullet movement direction for both player and enemy (alien)
if side == "enemy":
self.move_speed = BULLET_SPEED
elif side == "player":
self.move_speed = (- BULLET_SPEED)
def _move_bullet(self):
self.rect.y += self.move_speed
def update(self):
self._move_bullet()
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
# delete the bullet if it get through out of the screen
if self.rect.bottom <= 0 or self.rect.top >= HEIGHT:
self.kill()