-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGun.py
More file actions
51 lines (40 loc) · 1.87 KB
/
Gun.py
File metadata and controls
51 lines (40 loc) · 1.87 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
#!/usr/bin/env python
"""Gun module: ranged weapon entity."""
import pygame
from Bullets import Bullets
__author__ = "Joshua Sonnenberg and Ethan Richardson"
class Gun(pygame.Rect):
"""Represents a gun weapon attached to an entity."""
def __init__(self, entity, surface: pygame.Surface) -> None:
"""Initialize the gun.
Args:
entity: The entity (player) that owns this gun.
surface: The pygame surface to draw on.
"""
self.entity = entity
super(Gun, self).__init__(entity.x+20, entity.y+10, 10, 5)
self.ammoSideX = entity.x+30
self.ammoSideY = entity.y+10
self.surface = surface
self.entity = entity
def draw(self) -> None:
"""Update gun position and render it on screen."""
if self.entity.facing == 'RIGHT' or self.entity.facing == 'RSTOP':
self.x = self.entity.x+20
elif self.entity.facing == 'LEFT' or self.entity.facing == 'LSTOP':
self.x = self.entity.x-10
self.y = self.entity.y+10
pygame.draw.rect(self.surface, (55, 55, 59), self, 0)
def shoot(self) -> None:
"""Fire a bullet if ammo is available, otherwise switch to sword."""
if self.entity.ammo > 0:
if self.entity.facing == 'RIGHT' or self.entity.facing == 'RSTOP':
self.ammoSideX = self.entity.x+30
elif self.entity.facing == 'LEFT' or self.entity.facing == 'LSTOP':
self.ammoSideX = self.entity.x-10
self.ammoSideY = self.entity.y+10
# Fires rounds and adds bullet objects to list
self.entity.bullet_manager.bullets.append(Bullets(self.ammoSideX, self.ammoSideY, self.entity.facing, self.surface))
self.entity.ammo -= 1
else:
self.entity.current_weapon = 'SWORD'