Showing how to use spritesheets with python with codergophers Heather and Martian Marty. Coding in Saskatchewan Canada with Python and Pygame.
Here's the spritemapper code. Be sure to save the python document as anyname.py.
#Imports
import pygame
from pygame import *
#Screen Dimensions
WIN_WIDTH = 1400
WIN_HEIGHT = 1000
#Screen Defaults
DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
DEPTH = 32
FLAGS = 0
#Spritesheet
spritesheet = pygame.image.load(" spritesheet.png")
#Main Function
def main():
pygame.init()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
pygame.display.set_caption("Sprite Mapper")
x = 0
y = 0
w = 100
h = 50
pygame.key.set_repeat(1,30)
f = open("test.txt","w")
f.close()
while 1:
for e in pygame.event.get():
if e.type == QUIT: raise SystemExit("QUIT")
if e.type == KEYDOWN and e.key == K_ESCAPE:
raise SystemExit("ESCAPE")
if e.type == KEYDOWN and e.key == K_DOWN:
y = y - 1
if e.type == KEYDOWN and e.key == K_UP:
y = y + 1
if e.type == KEYDOWN and e.key == K_RIGHT:
x = x - 1
if e.type == KEYDOWN and e.key == K_LEFT:
x = x + 1
if e.type == KEYDOWN and e.key == K_g:
y = y - 15
if e.type == KEYDOWN and e.key == K_t:
y = y + 15
if e.type == KEYDOWN and e.key == K_h:
x = x - 15
if e.type == KEYDOWN and e.key == K_f:
x = x + 15
if e.type == KEYDOWN and e.key == K_w:
h = h - 1
if e.type == KEYDOWN and e.key == K_s:
h = h + 1
if e.type == KEYDOWN and e.key == K_a:
w = w - 1
if e.type == KEYDOWN and e.key == K_d:
w = w + 1
if e.type == KEYDOWN and e.key == K_SPACE:
name = input("Name of Sprite?")
f = open("test.txt","a")
savedcode = ("character = Surface((") + str(w) + (",") + str(h) + ("),pygame.SRCALPHA)") + ("\n")
f.write(savedcode)
savedcode = ("character.blit(spritesheet,(") + str(x) + (",") + str(y) + ("))") + ("\n")
f.write(savedcode)
savedcode = ("character = pygame.transform.scale(character, (") + str(w) + (",") + str(h) + ("))") + ("\n")
f.write(savedcode)
savedcode = name + (" = character") + ("\n") + ("\n")
f.write(savedcode)
f.close()
screen.fill(Color("#2E722A"))
character = Surface((w,h))
character.fill(Color("#4CCCFF"))
character.blit(spritesheet,(x,y))
character = pygame.transform.scale(character,(w*8,h*8))
screen.blit(character,(0,0))
pygame.display.update()
if _name_ == "__main__":
main()
On this page of the site you can watch the video online Using Spritesheets With Python for Games with a duration of hours minute second in good quality, which was uploaded by the user codergopher 08 May 2017, share the link with friends and acquaintances, this video has already been watched 1,158 times on youtube and it was liked by 20 viewers. Enjoy your viewing!