-
Notifications
You must be signed in to change notification settings - Fork 3
/
sprite.py
78 lines (68 loc) · 2.33 KB
/
sprite.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import random
import pygame
import spritesheet
NONE = -1
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
UP_ANIMATE = 4
DOWN_ANIMATE = 5
LEFT_ANIMATE = 6
RIGHT_ANIMATE = 7
def load_image(name):
fullname = os.path.join('images', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', name
raise SystemExit, message
image = image.convert_alpha()
return image
class Sprite(pygame.sprite.Sprite):
def __init__(self, image_url, x, y, width=32, height=32, animated=False, direction=NONE, image_urls = []):
pygame.sprite.Sprite.__init__(self)
self.image_url = image_url
self.image = load_image(image_url)
self.width = width
self.height = height
self.rect = self.image.get_rect()
self.rect.topleft = (x*32,y*32)
self.animated = animated
self.direction = direction
self.dir_images = []
self.image_urls = image_urls
self.animate = False
if direction != NONE:
for url in image_urls:
self.dir_images.append(load_image(url))
if animated:
self.images = []
sprites = spritesheet.Spritesheet(os.path.join('images', image_url))
for i in range(0,6):
self.images.append(sprites.image_at((i*32,i*32,32,32)))
self.image = self.images[0]
self.frame = 0
def do_animate(self):
if self.direction == UP:
self.direction = UP_ANIMATE
elif self.direction == DOWN:
self.direction = DOWN_ANIMATE
elif self.direction == LEFT:
self.direction = LEFT_ANIMATE
else:
self.direction = RIGHT_ANIMATE
self.animate = True
self.frame = 0
def set_direction(self, direction):
self.direction = direction
self.image = self.dir_images[self.direction]
def update(self, stats, viewport):
self.rect.topleft = (stats.x*32 - (viewport.x_offset*32), stats.y*32 - (viewport.y_offset*32))
if self.animate:
sheet = spritesheet.Spritesheet("images/" + self.image_urls[self.direction])
self.image = sheet.image_at((self.frame*32,0,32,32))
self.frame += 1
if self.frame > 6:
self.animate = False