-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbullet.py
More file actions
55 lines (47 loc) · 1.9 KB
/
bullet.py
File metadata and controls
55 lines (47 loc) · 1.9 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
import pygame
import math
from config import SCREEN_WIDTH, SCREEN_HEIGHT, BULLET_SIZE, BULLET_SPEED
class Bullet:
def __init__(self, x, y, target_x, target_y, color=(0, 0, 0), shooter=None, image=None):
self.shooter = shooter
self.color = color
self.original_image = image # Original, unrotated image
self.image = None
self.image_rect = None
# Calculate movement vector
angle = math.atan2(target_y - y, target_x - x)
self.angle_deg = math.degrees(angle)
self.dx = math.cos(angle) * BULLET_SPEED
self.dy = math.sin(angle) * BULLET_SPEED
# Positioning
self.rect = pygame.Rect(0, 0, BULLET_SIZE, BULLET_SIZE)
self.rect.center = (x, y)
# Apply rotation if image provided
if self.original_image:
try:
rotated = pygame.transform.rotate(self.original_image, -self.angle_deg + 180)
self.image = rotated
self.image_rect = self.image.get_rect(center=self.rect.center)
#print("[Bullet] Rotated image created successfully")
except Exception as e:
print("[Bullet ERROR] Rotation failed:", e)
self.image = None
self.image_rect = None
else:
pass
#print("[Bullet] No image provided, using color:", self.color)
def update(self):
self.rect.x += self.dx
self.rect.y += self.dy
if self.image_rect:
self.image_rect.center = self.rect.center
def draw(self, screen):
if self.image and self.image_rect:
screen.blit(self.image, self.image_rect)
else:
pygame.draw.rect(screen, self.color, self.rect)
def is_off_screen(self):
return (
self.rect.right < 0 or self.rect.left > SCREEN_WIDTH or
self.rect.bottom < 0 or self.rect.top > SCREEN_HEIGHT
)