-
Notifications
You must be signed in to change notification settings - Fork 0
/
gun.py
30 lines (24 loc) · 819 Bytes
/
gun.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
#!/usr/bin/env python
import pygame as pg
import load_functions as lf
class Gun(pg.sprite.Sprite):
# moves a targeting reticle on the screen, following the mouse
def __init__(self, img, directory):
pg.sprite.Sprite.__init__(self) # call sprite initializer
self.image, self.rect = lf.load_image(img, directory, -1)
self.fist_offset = (0, 0)
self.shooting = False
def update(self):
# move the reticle based on the mouse position
pos = pg.mouse.get_pos()
self.rect.center = pos
self.rect.move_ip(self.fist_offset)
def shoot(self, target):
# returns true if the reticle collides with the target
if not self.shooting:
self.shooting = True
hitbox = self.rect.inflate(-5, -5)
return hitbox.colliderect(target.rect)
def unshoot(self):
# called to reset the gun
self.shooting = False