gpt4 book ai didi

python - 我无法使用箭头键在 pygame 中移动图像

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:00 25 4
gpt4 key购买 nike

有人可以告诉我为什么向上向下箭头键不能移动我的图像吗?我正在使用 Python 3 和 Pygame 1.91。没有错误。只是失望。相关部分是第 65-115 行(Display 函数开始到 Move_Paddle 函数结束)。

import pygame
import time
import sys
from pygame.locals import *


pygame.init()

display_width = 800
display_height = 600

black = (0, 0, 0) #Colors
white = (255, 255, 255)

pygame.display.set_caption('Pong') #Title

clock = pygame.time.Clock() #Clock

gameDisplay = pygame.display.set_mode((display_width,display_height)) #Defines "window" and width/height variables


BallImg = pygame.image.load('C:\Python\Programs\Pong\Ball.png')
Paddle_PlayerImg = pygame.image.load('C:\Python\Programs\Pong\Paddle.png')
Paddle_CompImg = pygame.image.load('C:\Python\Programs\Pong\Paddle1.png')


def Ball(BLx,BLy):

gameDisplay.blit(BallImg,(BLx ,BLy ))

def Paddle_Player(PPx,PPy):

gameDisplay.blit(Paddle_PlayerImg, (display_width * 0.725 ,display_height * .166 ))

def Paddle_Comp(PCx,PCy):

gameDisplay.blit(Paddle_CompImg, (PCx,PCy))

pygame.display.flip()


gameDisplay.fill(black)

BLx = display_width * 0.475
BLy = display_height * .75

PPx = display_width * 0.725
PPy = display_height * .166

PCx = display_width * 0.125
PCy = display_height * .166

Ball(BLx,BLy)
Paddle_Player(PPx,PPy)
Paddle_Comp(PCx,PCy)

pygame.display.flip()

clock.tick(15)

def Display():
gameDisplay.fill(black)

BLx = display_width * 0.475
BLy = display_height * .75

PPx = display_width * 0.725
PPy = display_height * .166

PCx = display_width * 0.125
PCy = display_height * .166

Ball(BLx,BLy)
Paddle_Player(PPx,PPy)
Paddle_Comp(PCx,PCy)

pygame.display.update()



def Move_Paddle():
PPx = display_width * 0.725
PPy = display_height * .166
PPy_change = 0
gameExit = False

while not gameExit:

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

if event.type == KEYDOWN:
if event.key == K_DOWN:
PPy_change = -5
if event.key == K_UP:
PPy_change = 5

if event.type == pygame.KEYUP:
if event.key == K_UP or event.key == K_DOWN:
PPy_change = 0

PPy += PPy_change
pygame.display.update()
Paddle_Player(PPx,PPy)

clock.tick(60)

Move_Paddle()
Display()

quit()

最佳答案

我认为问题在于您的 Move_Paddle() 函数中,更新显示和移动 player 的代码,我猜 - Paddle_Player( ) 函数 - 位于 while 循环之外,因此只有在退出后才会执行该代码。您应该将它们移至 while 循环内。示例-

def Move_Paddle():    
PPx = display_width * 0.725
PPy = display_height * .166
PPy_change = 0
gameExit = False

while not gameExit:

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

if event.type == KEYDOWN:
if event.key == K_DOWN:
PPy_change = -5
if event.key == K_UP:
PPy_change = 5

if event.type == pygame.KEYUP:
if event.key == K_UP or event.key == K_DOWN:
PPy_change = 0

PPy += PPy_change
pygame.display.update()
Paddle_Player(PPx,PPy)

clock.tick(60)

关于python - 我无法使用箭头键在 pygame 中移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31860458/

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