gpt4 book ai didi

python - 使用带有函数的pygame的角色动画问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:15 26 4
gpt4 key购买 nike

我想请你帮忙解决我的这个问题。我需要在 pygame 中仅使用函数(无类)开发游戏。

我已经设法运行游戏,但动画或 Sprite 没有按预期更新。图像应该在移动时发生变化,但始终保持不变。

我做错了什么?

代码如下:

import pygame               

WIDTH = 800
HEIGHT= 600
pygame.display.set_caption("Nico's worst adventure")
window = pygame.display.set_mode((WIDTH, HEIGHT))

#Color #R #G #B
White = (255, 255, 255)
Black = (0, 0, 0)
Red = (255, 0, 0)
Blue = (0, 0, 255)

#Position
x = 50
y = 425

imageWidth = 64

vel = 5 #Velocity of movement

isJumping = False
jumpCount = 10

left = False
right = False
walkCount = 0

#Image port
walkRight = [pygame.image.load('Nico right(1).png'), pygame.image.load('Nico right(2).png'), pygame.image.load('Nico right(3).png'), pygame.image.load('Nico right(4).png')]
walkLeft = [pygame.image.load('Nico left(1).png'), pygame.image.load('Nico left(2).png'), pygame.image.load('Nico left(3).png'), pygame.image.load('Nico left(4).png')]
still = pygame.image.load('Nico still.png')
backgorund = pygame.image.load('Fondo.png')



def drawCharacter():
global walkCount
window.blit(backgorund, (0,0))
if walkCount + 1 >= 12:
walkCount = 0

if left:
window.blit(walkLeft[walkCount//3], (x, y))
walkCount += 1
elif right:
window.blit(walkRight[walkCount//3], (x, y))
walkCount += 1
else:
window.blit(still, (x, y))

pygame.display.update()


def draw():
global imageWidth
global WIDTH
global x
global y
global vel
global jumpCount
global isJumping
clock = pygame.time.Clock()
play = True
#Main loop
while play:
clock.tick(27)
pygame.init()

for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False

key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False

elif key[pygame.K_RIGHT] and x < WIDTH - imageWidth - vel:
x += vel
right = True
left = False
else:
right = False
left = False
walkCount = 0

if not(isJumping):
if key[pygame.K_SPACE]:
isJumping = True
right = False
left = False
walkCount = 0

else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJumping = False
jumpCount = 10


drawCharacter()
pygame.display.flip()


pygame.quit()

draw()

我已经检查过它并与其他代码进行了比较,但我就是找不到真正的问题所在。

最佳答案

已经提到了简单的修复方法,但我更愿意建议完全摆脱全局变量,将它们移动到带有主循环的函数中,使它们成为局部变量。

此外,drawCharacter 函数不应该负责更新 walkCount 以及 blitting 图像。我只是删除它并更新 if key[pygame.K_LEFT]... 子句中的 walkCount ,然后使用它将当前玩家图像分配给一个变量(player_image),稍后您可以在绘图部分进行 blit。这也允许您摆脱 leftright 变量。

drawCharacter 函数将只包含这两行,

window.blit(background, (0, 0))
window.blit(player_image, (x, y))

因此您也可以简单地在主循环中调用它们,而不是在函数中调用它们(您必须将这些参数传递给函数)。

另外,加载图片时调用convertconvert_alpha方法可以提高性能。

这是更新后的代码:

import pygame


pygame.init()
WIDTH = 800
HEIGHT= 600
pygame.display.set_caption("Nico's worst adventure")
window = pygame.display.set_mode((WIDTH, HEIGHT))

White = (255, 255, 255)
Black = (0, 0, 0)
Red = (255, 0, 0)
Blue = (0, 0, 255)
# Images (I assume you're using pictures with a transparent background
# so I call the `convert_alpha` method).
walkRight = [
pygame.image.load('Nico right(1).png').convert_alpha(),
pygame.image.load('Nico right(2).png').convert_alpha(),
pygame.image.load('Nico right(3).png').convert_alpha(),
pygame.image.load('Nico right(4).png').convert_alpha(),
]
walkLeft = [
pygame.image.load('Nico left(1).png').convert_alpha(),
pygame.image.load('Nico left(2).png').convert_alpha(),
pygame.image.load('Nico left(3).png').convert_alpha(),
pygame.image.load('Nico left(4).png').convert_alpha(),
]
still = pygame.image.load('Nico still.png').convert_alpha()
background = pygame.image.load('Fondo.png').convert()



def game():
clock = pygame.time.Clock()
# Player related variables.
x = 50
y = 425
imageWidth = 64
vel = 5
isJumping = False
jumpCount = 10
walkCount = 0
player_image = still # Assign the current image to a variable.

play = True
while play:
clock.tick(27)

for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False

key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and x > vel:
x -= vel
# Update the walkCount and image here.
walkCount += 1
player_image = walkRight[walkCount//3]
elif key[pygame.K_RIGHT] and x < WIDTH - imageWidth - vel:
x += vel
walkCount += 1
player_image = walkLeft[walkCount//3]
else:
player_image = still
walkCount = 0

if walkCount + 1 >= 12:
walkCount = 0

if not isJumping:
if key[pygame.K_SPACE]:
isJumping = True
walkCount = 0
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJumping = False
jumpCount = 10

window.blit(background, (0, 0))
window.blit(player_image, (x, y)) # Just blit the current image.
pygame.display.flip() # Only one `flip()` or `update()` call per frame.


game()
pygame.quit()

关于python - 使用带有函数的pygame的角色动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53262926/

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