-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspritesheet.py
103 lines (91 loc) · 3.95 KB
/
spritesheet.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# This class handles sprite sheets
# This was taken from www.scriptefun.com/transcript-2-using
# sprite-sheets-and-drawing-the-background
# I've added some code to fail if the file wasn't found..
# Note: When calling images_at the rect is the format:
# (x, y, x + offset, y + offset)
# Additional notes
# - Further adaptations from https://www.pygame.org/wiki/Spritesheet
# - Cleaned up overall formatting.
# - Updated from Python 2 -> Python 3.
from pygameInit import *
from pygame.math import Vector2 as vector
from MyColors import *
from MyFunctions import *
import os
width, height = startPygame(hypo = 1000, ratioa = 21, ratiob = 10, caption = "AAAAAAAAA")
screen = pygame.display.set_mode((width, height), pygame.SRCALPHA)
class SpriteSheet:
def __init__(self, filename):
"""Load the sheet."""
try:
self.sheet = pygame.image.load(filename).convert_alpha()
except pygame.error as e:
print(f"Unable to load spritesheet image: {filename}")
raise SystemExit(e)
def image_at(self, rectangle, colorkey = None):
"""Load a specific image from a specific rectangle."""
rect = pygame.Rect(rectangle)
image = pygame.Surface(rect.size, pygame.SRCALPHA) # Create surface with per-pixel alpha
image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image
def images_at(self, rects, colorkey = None):
"""Load a whole bunch of images and return them as a list."""
return [self.image_at(rect, colorkey) for rect in rects]
def load_strip(self, rect, image_count, colorkey = None):
"""Load a whole strip of images, and return them as a list."""
tups = [(rect[0]+rect[2]*x, rect[1], rect[2], rect[3])
for x in range(image_count)]
return self.images_at(tups, colorkey)
# (8, 31),(40,63) 32
def load_grid_images(self, num_rows, num_cols, x_margin=0, x_padding=0,
y_margin=0, y_padding=0):
"""Load a grid of images.
x_margin is space between top of sheet and top of first row.
x_padding is space between rows.
Assumes symmetrical padding on left and right.
Same reasoning for y.
Calls self.images_at() to get list of images.
"""
sheet_rect = self.sheet.get_rect()
sheet_width, sheet_height = sheet_rect.size
self.rows = num_rows
self.cols = num_cols
# To calculate the size of each sprite, subtract the two margins,
# and the padding between each row, then divide by num_cols.
# Same reasoning for y.
x_sprite_size = ( sheet_width - 2 * x_margin
- (num_cols - 1) * x_padding ) / num_cols
y_sprite_size = ( sheet_height - 2 * y_margin
- (num_rows - 1) * y_padding ) / num_rows
sprite_rects = []
for row_num in range(num_rows):
for col_num in range(num_cols):
# Position of sprite rect is margin + one sprite size
# and one padding size for each row. Same for y.
x = x_margin + col_num * (x_sprite_size + x_padding)
y = y_margin + row_num * (y_sprite_size + y_padding)
sprite_rect = (x, y, x_sprite_size, y_sprite_size)
sprite_rects.append(sprite_rect)
grid_images = self.images_at(sprite_rects)
# print(f"Loaded {len(grid_images)} grid images.")
return grid_images
#
# ss = SpriteSheet("resources/cat1.png")
# a=ss.load_grid_images(1,6)
# b = ss.image_at(pygame.Rect((0,0,12,12)))
# i = 0
# clock = pygame.time.Clock()
#
# while True:
# screen.fill(white)
# checkQuit()
# screen.blit(pygame.transform.scale(a[i],(80,80)),(width/2-40,height/2-40))
# # print(b.get_at((0,0)))
# i = i+ 1 if 0 <= i <= len(a) - 2 else 0
# pygame.display.flip()
# clock.tick(20)