-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspriteTools.py
More file actions
65 lines (47 loc) · 2.23 KB
/
Copy pathspriteTools.py
File metadata and controls
65 lines (47 loc) · 2.23 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
"""
spriteTools.py - module to define tools for manipulating sprites
---------------------------------------------------------------------------
Copyright 2014 Alexandre Lopes <aalopes@ovi.com>
This file is part of CometZ.
CometZ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CometZ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CometZ. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
"""
import pygame
class SpriteSheet():
"""
Class to get sprites from a sprite sheet
"""
# Constructor
def __init__(self,file):
self.imageSheet = pygame.image.load(file).convert()
def getImagePixel(self,x,y,width,height):
"""
Grab a sprite from a sprite sheet based on the top left x,y position
of the sprite, its width and its height
"""
# blank image
sprite = pygame.Surface([width,height]).convert()
# copy sprite from sprite sheet
sprite.blit(self.imageSheet,(0,0),(x,y,width,height))
return sprite
def getImageGrid(self,row,column,width,height):
"""
Grab a sprite from a sprite sheet assuming it is based on a grid.
One needs the width and height of each sprite, and the row and column
of the sprite one wants to extract. The top left one is row=0, column=0.
"""
# blank image
sprite = pygame.Surface([width,height]).convert()
# copy sprite from sprite sheet
sprite.blit(self.imageSheet,(0,0),(width*column,height*row,width*(column+1),
height*(row+1)))
return sprite