-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.py
65 lines (49 loc) · 2.28 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
import os
import pygame
import GameSettings
import GameObjects
import Direction
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, direction, is_bullet_friendly, bullet_level):
pygame.sprite.Sprite.__init__(self)
self.bullet_speed = GameSettings.change_for_fps(bullet_level * 10)
self.level = bullet_level
self.move_x = 0
self.move_y = 0
self.previous_x, self.previous_y = 0, 0
self.direction = direction
self.collision_type = 2
image = pygame.image.load(os.path.join('sprites/Bullet', f'bullet-{direction.name}.png')).convert()
self.size = image.get_size()
if self.direction == Direction.Direction.right or self.direction == Direction.Direction.left:
self.image = pygame.transform.scale(image, (GameSettings.change_for_screen_width(15),
GameSettings.change_for_screen_height(14)))
else:
self.image = pygame.transform.scale(image, (GameSettings.change_for_screen_width(14),
GameSettings.change_for_screen_height(15)))
self.image.set_colorkey((0, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.is_bullet_friendly = is_bullet_friendly
GameObjects.GameObjects.instance.add_bullet(self)
def move_bullet(self):
if self.direction == Direction.Direction.up:
self.rect.y -= self.bullet_speed
if self.direction == Direction.Direction.right:
self.rect.x += self.bullet_speed
if self.direction == Direction.Direction.down:
self.rect.y += self.bullet_speed
if self.direction == Direction.Direction.left:
self.rect.x -= self.bullet_speed
def is_bullet_behind_screen(self):
if self.rect.x < 0 - self.rect.size[0] \
or self.rect.x > GameSettings.GameSettings.screen_width \
or self.rect.y < 0 - self.rect.size[1] \
or self.rect.y > GameSettings.GameSettings.screen_height:
self.destroy_bullet()
return True
return False
def destroy_bullet(self):
GameObjects.GameObjects.instance.bullets.remove(self)
self.kill()