gpt4 book ai didi

python - pygame.transform.flip 不翻转也不给我一个错误?

转载 作者:行者123 更新时间:2023-12-01 07:58:20 24 4
gpt4 key购买 nike

所以我们接到了制作一个小型视频游戏的任务,到目前为止我已经有了玩家,背景,我可以跳跃和行走。我的问题是,无论我朝哪个方向走,我都会得到相同的动画,我也尝试用 pygame.transform.flip 翻转动画,但无法让它正常工作,我没有收到错误或什么也没有。但我的玩家角色仍然向后走..

在将数据传输到屏幕上之前,我也尝试过使用翻转命令,现在又尝试将整个动画移动到一个函数中,两者都给了我相同的结果。

import  pygame as pg
pg.init()
winHeight=600
winWidth = 1020
x=10
y=560
size = 37
bg = pg.image.load("scenes/Background/bg.png")
walkList = [pg.image.load("player/Run/run-00.png"),pg.image.load("player/Run/run-01.png"),pg.image.load("player/Run/run-02.png"),pg.image.load("player/Run/run-03.png"),pg.image.load("player/Run/run-04.png"),pg.image.load("player/Run/run-05.png")]
char = pg.image.load("player/idle/idle-00.png")
vel = 3
win = pg.display.set_mode((winWidth,winHeight))
pg.display.set_caption("SMAS 'EM By IKEA KID")
run = True
jumCount = 7
isJump = False
AnimationCounter = 0

def Animation():
direction = win.blit(walkList[AnimationCounter], (x, y))
if pg.event == pg.K_LEFT:
return direction
elif pg.event == pg.K_RIGHT:
direction=pg.transform.flip(direction,False,True)
return direction
def RedrawWindow():
global AnimationCounter
win.blit(bg, (0,0))

if AnimationCounter +1 >= 6:
AnimationCounter = 0
if walk:
Animation()
AnimationCounter += 1
elif not walk:
win.blit(char,(x,y))

pg.display.update()

while run:
pg.time.delay(15)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
keys = pg.key.get_pressed()

if keys[pg.K_LEFT] and x > vel:
x -= vel
walk = True
elif keys[pg.K_RIGHT] and x+size < winWidth-vel:
x += vel
walk = True
flip = True
else:
AnimationCounter = 0
walk = False
if not(isJump):
if keys[pg.K_SPACE]:
isJump = True
else:

if jumCount >= -7:
neg = 1
if jumCount < 0:
neg = -1
y -= (jumCount**2)*0.5*neg
jumCount-=0.5
else:
isJump = False
jumCount = 7

RedrawWindow()

预计我的玩家角色也会转身,以便我可以双向行走(我可以双向行走,但我正在朝一个方向行走)

最佳答案

函数pg.transform.flip从未被调用过。
pygame.event是一个类而不是事件类型。除此之外,pg.transform.flip 的第一个参数必须是 pygame.Surface对象而不是pygame.Rect对象。

根据变量flip的状态翻转表面(在全局范围内):

def Animation():
surf = walkList[AnimationCounter]
if flip:
surf = pg.transform.flip(surf,False,True)
direction = win.blit(surf, (x, y))

根据按键事件更改 flip 的状态:

flip = False
while run:
pg.time.delay(15)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
keys = pg.key.get_pressed()

if keys[pg.K_LEFT] and x > vel:
x -= vel
walk = True
flip = False
elif keys[pg.K_RIGHT] and x+size < winWidth-vel:
x += vel
walk = True
flip = True
else:
AnimationCounter = 0
walk = False

# [...]

RedrawWindow()

关于python - pygame.transform.flip 不翻转也不给我一个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com