-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteExample1.py
More file actions
65 lines (50 loc) · 2.13 KB
/
SpriteExample1.py
File metadata and controls
65 lines (50 loc) · 2.13 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
white = [255, 255, 255]
#FROM Joust example: https://github.com/StevePaget/PythonJoust ###############
def load_sliced_sprites(w, h, filename):
#returns a list of image frames sliced from file
images = []
master_image = pygame.image.load( filename )
master_image = master_image.convert_alpha()
master_width, master_height = master_image.get_size()
for i in range(int(master_width/w)):
images.append(master_image.subsurface((i*w, 0, w, h)))
return images
class JoustBird(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
#load in the images (will do it for every instance, which isn't efficient if you have many)
self.images = load_sliced_sprites(60, 60, "playerMounted.png")
self.frameNum = 0
self.image = self.images[self.frameNum]
self.x = 415
self.y = 350
self.rect = self.image.get_rect() #sets the image size
self.rect.topleft = (self.x,self.y) #sets the image location
self.nextFrameCounter = 0 #only going to redraw sometimes
def update(self):
#count how many times has been called
self.nextFrameCounter += 1
#if this is the third time we will change the bird's image
if self.nextFrameCounter == 3:
self.nextFrameCounter = 0
self.frameNum += 1
if self.frameNum == 4: #just use the walking images, not the flying ones
self.frameNum = 0
self.image = self.images[self.frameNum]
##############################################################################
bird = JoustBird()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.time.delay(20) #pause for 20 milliseconds
screen.fill(white) #make the screen completely white
bird.update()
screen.blit(bird.image, bird.rect)
#update the entire display
pygame.display.update()
pygame.quit()