-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.py
60 lines (42 loc) · 1.45 KB
/
unit.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
56
57
58
import math
import os
import pygame
wd = os.path.split(os.path.abspath(__file__))[0]
#functions to create our resources
def load_image(name, colorkey=None):
image = pygame.image.load(os.path.join(wd, name))
image = image.convert_alpha()
if colorkey != None:
if colorkey == -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey)
return image, image.get_rect()
class Unit(pygame.sprite.Sprite):
def __init__(self, entity, birdtype):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.entity = entity
init_position = self.entity.get_rect().center
png_name = ''
if birdtype == 'predator':
png_name = 'eagle.png'
else:
png_name = 'birdie.png'
self.image0, self.rect0 = load_image(png_name)
self.image = self.image0
self.rect = self.rect0.move(init_position)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
def update(self):
self.image, self.rect = self.do_yaw(self.rect, self.entity.velocity)
self.rect = self.rect.move(self.entity.velocity[0], self.entity.velocity[1])
def do_yaw(self, rect, velocity):
angle = math.atan2(velocity[1], velocity[0])
oldcent = rect.center
newsurf = pygame.transform.rotate(self.image0, 270-math.degrees(angle))
newrect = newsurf.get_rect()
newrect.center = oldcent
return newsurf, newrect
def calcnewpos(self, rect, vector):
(angle, z) = vector
(dx, dy) = (z*math.sin(-angle), z*math.cos(angle))
return rect.move(dx, dy)