Bullet
game/bullet.py defines the Bullet class, a pygame.sprite.Sprite representing a plasma bolt fired by the player. Bullets travel in a straight line, leave a short cyan trail, and are removed on wall collision or when they leave the screen.
Attributes
| Attribute | Type | Description |
|---|---|---|
image | pygame.Surface | 20×8 px plasma bolt, rotated to match direction. White ellipse core with a cyan outer glow. |
rect | pygame.Rect | Bounding rect used for collision detection. Centred on (x, y) after rotation. |
pos | pygame.Vector2 | Sub-pixel position, updated each frame. |
direction | pygame.Vector2 | Normalised unit vector representing the bullet’s travel direction. |
speed | int | 18 — pixels per frame. |
spawn_time | int | pygame.time.get_ticks() at construction. Available for lifetime-based expiry if needed. |
__init__(x, y, direction)
Horizontal spawn position. Set to
player.pos.x at the moment of firing.Vertical spawn position. Set to
player.pos.y at the moment of firing.Normalised direction vector. In the main game loop this is
last_direction, which tracks the most recent non-zero movement direction.(1, 0), so a bullet fired to the right is unrotated, upward bullets rotate +90°, and so on.
pygame.transform.rotate() enlarges the surface to fit the rotated content. The rect is re-derived from the rotated image and centred on (x, y), so the bullet origin stays accurate regardless of angle.update()
Advances the bullet along its direction vector each frame:
speed = 18, a bullet crosses a 1200 px screen in approximately 67 frames (~1.1 s).
update() is called by the bullets sprite group each frame:
draw(screen)
Renders the bullet’s kinetic trail and the plasma image.
The active display surface.
(0, 150, 255) is drawn 15 px behind the bullet centre, giving a short motion-blur streak effect. The rotated plasma image is then blitted on top.
The main game loop blits bullets a second time with the shake offset applied:This means the trail line drawn by
draw() is not shaken; only the image is offset. At low shake values this difference is imperceptible.Lifecycle
Spawned
Created on SPACE keypress (Levels 2–4) at the player’s current position, travelling in
last_direction.In flight
bullets.update() moves the bullet 18 px per frame. The group is iterated to check for wall and screen-boundary hits.Wall impact
If
bullet.rect.colliderect(wall) is true for any wall rect, 5 cyan (0, 255, 255) particles are spawned at bullet.pos and the bullet is removed from the group:Off-screen removal
If
pos.x or pos.y is outside [0, width] / [0, height], the bullet is removed with no particle effect.