0

I have the game come up and the rectangle rendered. When I press my KEYDOWN it doesn't move the rectangle, it just makes it longer. I have tried tons of stuff. I am new to Pygame.
Any help would be amazing.

Here is the code:

import pygame
import time
import random
import math
import sys


pygame.init()

display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)



gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Vertical Pong')
clock = pygame.time.Clock()


def pongBoard(x,y,):
    pygame.draw.rect(gameDisplay,white,(x,y,250,25))


def gameLoop():

    x = 325
    y = 750
    xChange = 0



    inGame = True

    while inGame:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = -5
                    print("Left")
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 5
                    print("Right")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = 0

        pongBoard(x,y)

        x += xChange






        pygame.display.update()
        clock.tick(60)




gameLoop()
pygame.quit()
quit()
red0ct
  • 4,840
  • 3
  • 17
  • 44
Pooce
  • 17
  • 2

2 Answers2

0

So the problem is this: The rectangle is being constantly redrawn at a different coord, but the screen is not being drawn over the rectangle to cover up the part that is not supposed to be there. In simpler terms, we need to constantly draw the background.

So now the code in the main game loop:

while inGame:
    #This code below draws the background
    pygame.draw.rect(gameDisplay, black, (0, 0, display_width, display_height)) 

That is it! The background will constantly cover up the pong ball, and the pong ball will be constantly blitted to a new position!

P.S, there is a better way to do arrow key movement here: How to get keyboard input in pygame?

Community
  • 1
  • 1
Demandooda
  • 122
  • 1
  • 14
  • That makes total sense! So when I am moving objects its not actually moving the location of the object? Its just drawing a new object and then updating the background to hide the last position? Thank you so much! – Pooce Jun 10 '16 at 20:20
  • Yup! That is how you do it. There are more complicated things to do, but that is the usual method in pygame. – Demandooda Jun 10 '16 at 20:21
  • Thank you so much for taking the time to answer – Pooce Jun 10 '16 at 20:24
  • It did, if I may, when I press the key down it does move it now. Am i missing something to keep it moving while the key is down? Im looking for how to mark this as answered – Pooce Jun 10 '16 at 20:28
  • If you looked at my link, it gives the answer to your new question. I thought it might come in handy. the check mark below my question's votes it the utton you have to press to accept the answer. – Demandooda Jun 10 '16 at 20:30
0

it actualy does move it, but the old one just stays there, making it look like it does not move but just grows. one way to change that would be to change the old ones color to the background color

try this code it works :-)

import pygame
import time
import random
import math
import sys


pygame.init()

display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
red = (123,11,45)



gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Vertical Pong')
clock = pygame.time.Clock()


def pongBoard(x,y,xold):
    pygame.draw.rect(gameDisplay,white,[x,y,250,25])
    pygame.draw.rect(gameDisplay,red,[xold,y,250,25])


def gameLoop():

    x = 325
    y = 750
    xChange = 0



    inGame = True

    while inGame:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = -50
                    pongBoard(x,y,xold)
                    print("Left")
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 50
                    pongBoard(x,y,xold)
                    print("Right")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = 0
        xold = x

        x += xChange
        xold = x-xChange






        pygame.display.update()
        clock.tick(60)




gameLoop()
pygame.quit()
quit()
Cid-El
  • 500
  • 7
  • 30