python动画精灵_游戏精灵片精灵动画

我正在尝试为我的精灵(4个方向)使用4个不同的精灵表(移动上,下,左,右)行走动画。除此之外,我还想用图像替换彩色方块,但不确定我该怎么做。下面是主.py游戏文件:import pygame as pg

import sys

from settings import *

from os import path

from sprites import *

from tilemap import *

class Spritesheet:

def __init__(self, filename, cols, rows):

self.sheet = pg.image.load(filename)

self.cols = cols #no. of columns in spritesheet

self.rows = rows #no. of rows

self.totalCellCount = cols*rows

self.rect=self.sheet.get_rect()

w = self.cellWidth = self.rect.width / cols

h = self.cellHeight = self.rect.height / rows

hw, hh = self.cellCenter = (w / 2, h / 2)

self.cells = list([(index % cols * w, index // cols * h, w, h) for index in range(self.totalCellCount)])

self.handle = list([

(0, 0), (-hw, 0), (-w, 0),

(0, -hh), (-hw, -hh), (-w, -hh),

(0, -h), (-hw, -h), (-w, -h),])

def draw(self, surface, cellIndex, x, y, handle = 0):

surface.blit(self.sheet, (x + self.handle[handle][0], y + self.handle[handle][1]), self.cells[cellIndex])

CENTER_HANDLE = 4

index = 0

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()

pg.key.set_repeat(500, 100)

self.load_data()

def load_data(self):

game_folder = path.dirname(__file__)

img_folder = path.join(game_folder, 'img')

self.map= Map(path.join(game_folder, 'map.txt'))

def new(self):

# initialize all variables and do all the setup for a new game

self.all_sprites = pg.sprite.Group()

self.walls = pg.sprite.Group()

self.player1group = pg.sprite.Group()

self.player2group = pg.sprite.Group()

for row, tiles in enumerate(self.map.data):

for col, tile in enumerate(tiles):

if tile == '1':

Wall(self, col, row)

if tile =='P':

self.player = Civilian(self, col, row)

if tile =='T':

self.player2 = Thief(self, col, row)

def run(self):

# game loop - set self.playing = False to end the game

self.playing = True

while self.playing:

self.dt = self.clock.tick(FPS) / 1000

self.events()

self.update()

self.draw()

def quit(self):

pg.quit() #Calls the quit function, game window closes

sys.exit()

def update(self):

# update portion of the game loop

self.all_sprites.update() #All sprite attributes, position etc are updated

def draw_grid(self):

for x in range(0, WIDTH, TILESIZE):

pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))

for y in range(0, HEIGHT, TILESIZE):

pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))

def draw(self):

self.screen.fill(BGCOLOR)

self.draw_grid()

self.all_sprites.draw(self.screen)

pg.display.flip()

def events(self):

# catch all events here

for event in pg.event.get():

if event.type == pg.QUIT:

self.quit()

if event.type == pg.KEYDOWN:

if event.key == pg.K_ESCAPE:

self.quit()

def show_start_screen(self):

pass

def show_go_screen(self):

pass

# create the game object

g = Game()

g.show_start_screen()

while True:

g.new()

g.run()

g.show_go_screen()

这是精灵.py文件,续精灵类

^{pr2}$

希望根据精灵移动的方向在精灵表中的一行单元格中运行,并将其设置为精灵图像(使玩家产生精灵动画的错觉)

任何帮助将不胜感激,抱歉,如果问题是困惑,第一次使用网站,并不是太擅长编码目前阶段。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值