我不能用箭头键在pygame中移动我的图像

时间:2015-08-06 15:57:14

标签: python image keyboard pygame

有人可以告诉我为什么上下箭头键不会移动我的图像?我正在使用Python 3和Pygame 1.91。没有错误。只是失望。相关部分是第65-115行(显示功能的开始到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()

3 个答案:

答案 0 :(得分:1)

我认为问题在于,在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)

答案 1 :(得分:0)

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)

cellSize = 11

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 += PPy_change
                    print(PPy_change)
                    print(K_DOWN)
                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
        Paddle_Player(PPx,PPy)
        pygame.display.update()


        clock.tick(60)               

Move_Paddle()
Display()

quit()

答案 2 :(得分:0)

我修好了!!!!

# I changed this function:

def Paddle_Player(PPx,PPy):

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

#...to this! That's all there was to it.

        def Paddle_Player(PPx,PPy):

gameDisplay.blit(Paddle_PlayerImg, (PPx,PPy))

#The variables were already defined below!
相关问题