gpt4 book ai didi

python - Pygame 键盘动画无法正常工作

转载 作者:行者123 更新时间:2023-12-01 05:08:05 25 4
gpt4 key购买 nike

import pygame



class Sprite:

def __init__(self, x, y, curren_time):

self.rect = pygame.Rect(x, y, 100, 110)

self.images = []

#Tells pygame the image list that you want to stitch together
for x in range(10):
img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST6/Cernunnos" + str(x) +".PNG")
#Append the image to each other to create the animation effect
self.images.append( img )

self.current_image = 0

self.time_num = 100
self.time_target = curren_time + self.time_num

def update(self, curren_time):

if curren_time >= self.time_target:

self.time_target = curren_time + self.time_num

self.current_image += 1

if self.current_image == len(self.images):
self.current_image = 0

def render(self, window):

window.blit(self.images[self.current_image], self.rect)
#Colors
black = (0,0,0)
white = (255,255,255)




def main():

pygame.init()

window = pygame.display.set_mode((800,600))

pygame.display.set_caption("Sprites")

move_x, move_y = 0, 0

clock = pygame.time.Clock()
curren_time = pygame.time.get_ticks()

player = Sprite(110,100,curren_time)

font = pygame.font.SysFont(None, 150)
pause_text = font.render("PAUSE",1,white)
pause_rect = pause_text.get_rect( center = window.get_rect().center )

#Adding how many places the image moves when using the key function
state_game = True
state_pause = False

#So while True
while state_game:

curren_time = pygame.time.get_ticks()



for event in pygame.event.get():

if event.type == pygame.QUIT:
state_game = False

elif event.type == pygame.KEYDOWN:

if event.key ==pygame.K_ESCAPE:
state_game = False

elif event.key == pygame.K_SPACE:
state_pause = not state_pause

if event.key == pygame.K_LEFT:
move_x = -3

elif event.key == pygame.K_RIGHT:
move_y = 3

elif event.key == pygame.K_UP:
move_x = -3

elif event.key == pygame.K_DOWN:
move_y = 3

elif event.type == pygame.KEYUP:

if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
move_x = 0

elif event.key in (pygame.K_UP, pygame.K_DOWN):
move_y = 0



if not state_pause:
player.rect.x += move_x
player.rect.y += move_y
player.update(curren_time)

#Fills the window with a color

window.fill(black)

player.render(window)

if state_pause:
window.blit(pause_text,pause_rect)

pygame.display.flip()



clock.tick(50)



pygame.quit()



if __name__ == '__main__':
main()
#Code constructed by furas

所以问题是,每当我按下右键时,动画就会从左侧屏幕上滑落。当您将手指从琴键上移开时,动画不会停止。我自己搜索过问题,但找不到任何问题。如果您发现可能导致问题的原因,请告诉我。谢谢! (上键=下,下键=下,右键=左,左键=左)

最佳答案

您的问题是您将 move_xmove_y 交换为 K_RIGHTK_UP

            elif event.key == pygame.K_RIGHT:
move_y = 3

elif event.key == pygame.K_UP:
move_x = -3

应该是:

            elif event.key == pygame.K_RIGHT:
move_x = 3

elif event.key == pygame.K_UP:
move_y = -3

关于python - Pygame 键盘动画无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24719416/

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