我无法使用箭头键在pygame中移动图片 [英] I cant move my image in pygame with arrow keys

查看:164
本文介绍了我无法使用箭头键在pygame中移动图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么向上箭头键不能移动我的图像吗?我正在使用Python 3和Pygame 1.91.没有错误.只是令人失望.相关的部分是第65-115行(显示功能到Move_Paddle功能的结尾).

Can someone show me why the up down arrow keys won't move my image? I'm using Python 3 and Pygame 1.91. No errors. Just disappointment. The pertinent section are lines 65-115 (Beginning of Display function to end of Move_Paddle function).

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循环内.示例-

I think the issue is that in your Move_Paddle() function , the code to update the display and move the player , I am guessing - Paddle_Player() function - is outside the while loop, so that code is only hit after you exit. You should move them inside the while loop. Example -

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)

这篇关于我无法使用箭头键在pygame中移动图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆