-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.py
55 lines (48 loc) · 1.51 KB
/
target.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
#!/usr/bin/env python
import pygame as pg
import load_functions as lf
import random
class Target(pg.sprite.Sprite):
def __init__(self, img, directory):
pg.sprite.Sprite.__init__(self) # call Sprite initializer
random.seed()
self.image, self.rect = lf.load_image(img, directory, -1, 2)
screen = pg.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 10, 90
self.move = 18
self.hit = False
def update(self):
# move or break, depending on target's state
if self.hit:
self._break()
else:
self._appear()
def _walk(self):
# move the target across the screen, and turn at the ends
newpos = self.rect.move((self.move, 0))
if not self.area.contains(newpos):
if self.rect.left < self.area.left or self.rect.right > self.area.right:
self.move = -self.move
newpos = self.rect.move((self.move, 0))
self.image = pg.transform.flip(self.image, True, False)
self.rect = newpos
def _appear(self):
self.rect.x = random.randint(0, self.area.right - self.rect.width)
self.rect.y = random.randint(0, self.area.bottom - self.rect.height)
def _break(self):
# spin the target image
center = self.rect.center
self.hit = self.hit + 12
if self.hit >= 360:
self.hit = False
self.image = self.original
else:
rotate = pg.transform.rotate
self.image = rotate(self.original, self.hit)
self.rect = self.image.get_rect(center=center)
def shot(self):
# this will cause the target to start spinning
if not self.hit:
self.hit = True
self.original = self.image