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()
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)
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()
I fixed It!!!!
# 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!