Skip to content

Instantly share code, notes, and snippets.

@crushedhat
Created Jan 14, 2018
Embed
What would you like to do?
Working with sprite sheet
#!/usr/bin/python
import pygame as pg
import random
from settings import *
from sprites import *
from os import path
class Game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
self.running = True
self.load_data()
def load_data(self):
self.dir = path.dirname(__file__)
img_dir = path.join(self.dir, 'img')
self.spritesheet = Spritesheet(path.join(img_dir, SPRITESHEET))
def new(self):
self.all_sprites = pg.sprite.Group()
self.player = Player(self)
self.all_sprites.add(self.player)
self.run()
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
self.all_sprites.update()
def events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
if self.playing:
self.playing = False
self.running = False
def draw(self):
self.screen.fill(BLACK)
self.all_sprites.draw(self.screen)
pg.display.flip()
def show_start_screen(self):
pass
def show_go_screen(self):
pass
g = Game()
g.show_start_screen
while g.running:
g.new()
g.show_go_screen()
pg.quit()
TITLE = "My Game"
WIDTH = 480
HEIGHT = 600
FPS = 60
SPRITESHEET = "sprite2.png"
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
#!/usr/bin/python
import pygame as pg
from settings import *
class Spritesheet:
def __init__(self, filename):
self.spritesheet = pg.image.load(filename).convert()
def get_image(self, x, y, width, height):
image = pg.Surface((width, height)) #tried resizing this to specifc w & h, but made image skip around
image.blit(self.spritesheet, (0,0), (x, y, width, height))
image = pg.transform.scale(image, (150,150))
return image
class Player(pg.sprite.Sprite):
def __init__(self, game):
pg.sprite.Sprite.__init__(self)
self.game = game
self.walking = False
self.standing = False
self.current_frame = 0
self.last_update = 0
self.image = self.game.spritesheet.get_image(9, 108, 18, 23)
self.load_images()
#self.image.set_colorkey(BLACK)
#self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.x = 0
self.y = 0
def update(self):
self.animate()
self.x = 0
self.y = 0
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.x = -3
if keys[pg.K_RIGHT]:
self.x = 3
if keys[pg.K_UP]:
self.y = -3
if keys[pg.K_DOWN]:
self.y = 3
self.rect.x += self.x
self.rect.y += self.y
def load_images(self):
self.standing = {'front': self.game.spritesheet.get_image(9, 108, 18, 23),
'back': self.game.spritesheet.get_image(79, 10, 17, 21),
'right': self.game.spritesheet.get_image(8, 190, 18, 21)}
self.standing_left = []
self.walking_bck = [self.game.spritesheet.get_image(106, 108, 18, 23),
self.game.spritesheet.get_image(138, 109, 18, 22),
self.game.spritesheet.get_image(170, 107, 18, 24),
self.game.spritesheet.get_image(200, 107, 18, 24),
self.game.spritesheet.get_image(232, 107, 18, 24),
self.game.spritesheet.get_image(263, 108, 18, 23),
self.game.spritesheet.get_image(296, 109, 18, 22),
self.game.spritesheet.get_image(328, 107, 18, 24),
self.game.spritesheet.get_image(360, 107, 18, 24),
self.game.spritesheet.get_image(391, 107, 18, 24)]
self.walking_fwd = [self.game.spritesheet.get_image(103, 189, 18, 22),
self.game.spritesheet.get_image(136, 189, 18, 22),
self.game.spritesheet.get_image(168, 187, 18, 24),
self.game.spritesheet.get_image(200, 186, 18, 25),
self.game.spritesheet.get_image(230, 187, 18, 24),
self.game.spritesheet.get_image(262, 189, 18, 22),
self.game.spritesheet.get_image(293, 189, 18, 22),
self.game.spritesheet.get_image(326, 187, 18, 24),
self.game.spritesheet.get_image(360, 186, 18, 25),
self.game.spritesheet.get_image(390, 187, 18, 24)]
self.walking_right = [self.game.spritesheet.get_image(100, 151, 23, 22),
self.game.spritesheet.get_image(134, 152, 20, 21),
self.game.spritesheet.get_image(168, 152, 20, 21),
self.game.spritesheet.get_image(199, 151, 20, 22),
self.game.spritesheet.get_image(229, 151, 20, 22),
self.game.spritesheet.get_image(258, 151, 23, 22),
self.game.spritesheet.get_image(292, 151, 19, 22),
self.game.spritesheet.get_image(327, 151, 19, 22),
self.game.spritesheet.get_image(360, 151, 19, 22),
self.game.spritesheet.get_image(389, 151, 20, 22),]
self.walking_left = []
for frame in self.walking_right:
self.walking_left.append(pg.transform.flip(frame, True, False))
self.attack_front = [self.game.spritesheet.get_image(0, 229, 28, 22),
self.game.spritesheet.get_image(38, 228, 25, 25),
self.game.spritesheet.get_image(80, 227, 19, 33),
self.game.spritesheet.get_image(114, 227, 19, 34),
self.game.spritesheet.get_image(151, 228, 25, 33),
self.game.spritesheet.get_image(193, 227, 29, 32),
self.game.spritesheet.get_image(238, 227, 31, 24),
self.game.spritesheet.get_image(285, 228, 32, 23),]
def animate(self):
now = pg.time.get_ticks()
keys = pg.key.get_pressed()
if self.x or self.y != 0:
self.walking = True
else:
self.walking = False
if self.walking:
if now - self.last_update > 100:
self.last_update = now
#bottom = self.rect.bottom
if keys[pg.K_DOWN]:
self.current_frame = (self.current_frame + 1) % len(self.walking_bck)
self.image = self.walking_bck[self.current_frame]
if keys[pg.K_UP]:
self.current_frame = (self.current_frame + 1) % len(self.walking_fwd)
self.image = self.walking_fwd[self.current_frame]
if keys[pg.K_LEFT]:
self.current_frame = (self.current_frame + 1) % len(self.walking_left)
self.image = self.walking_left[self.current_frame]
if keys[pg.K_RIGHT]:
self.current_frame = (self.current_frame + 1) % len(self.walking_right)
self.image = self.walking_right[self.current_frame]
#self.rect = self.image.get_rect()
#self.rect.bottom = bottom
if keys[pg.K_SPACE]:
self.current_frame = (self.current_frame + 1) % len(self.attack_front)
self.image = self.attack_front[self.current_frame]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment