0

I want to make a program where I move a rectangle via the keyboard, but it doesn't move like it doesn't understand the event commands. I can't find what's wrong. I think the problem is the sequence of commands, but as a beginner, I can't find it. Can anyone help me? Thanks!

import pygame
import sys
from pygame.locals import *

fps = 30
fpsclock = pygame.time.Clock()
w = 640
h = 420
blue = (0, 0, 255)
white = (255, 255, 255)
x = w / 3
y = 350
boxa = 20
movex = 0


def drawwindow():
    global screen
    pygame.init()
    screen = pygame.display.set_mode((w, h))
    screen.fill(blue)


def drawbox(box):
    if box.right > (w - boxa):
        box.right = (w - boxa)
    if box.left < 0:
        box.left = 0
    pygame.draw.rect(screen, white, box)


def main():
    global x
    global movex
    drawwindow()
    box1 = pygame.Rect(x, y, boxa, boxa)
    drawbox(box1)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_RIGHT:
                    movex = +4
                if event.key == K_LEFT:
                    movex = -4
            if event.type == KEYUP:
                if event.key == K_RIGHT:
                    movex = 0
                if event.key == K_LEFT:
                    movex = 0
        x += movex
        pygame.display.update()
        fpsclock.tick(fps)

if __name__ == '__main__':
    main()

1 Answer 1

2

Keyboard events are being accepted properly. That can be verified by sticking a print statement inside one of the if event.key == ... blocks.

One of the problems is you are never redrawing the box after initially drawing it. Every iteration of the game loop should redraw the background (ideally only the area that changes, but that's for later) and the box in its new position. Something like this:

while True:
    # [event handling code omitted for brevity]

    x += movex
    drawwindow()
    drawbox(box1)
    pygame.display.update()
    fpsclock.tick(fps)

However there's another problem. Changing x or movex has no effect on anything, because they are not used anywhere once the main loop is entered. Rather than x += movex, the box would move if its x attribute was changed, as in the following code:

while True:
    # [event handling code omitted for brevity]

    box1.x += movex # this line changed
    drawwindow()    # this line added
    drawbox(box1)   # this line added
    pygame.display.update()
    fpsclock.tick(fps)

Running your code with the changes above, the box now moves.

Sign up to request clarification or add additional context in comments.

4 Comments

i did it but it desnt move again
It isn't moving at all, or it moves once then not any more? It will only move once every keypress.
it isnt moving at all
If I copy-paste the code in your question and add the 3 lines from the bottom of my answer, the box moves a small amount every time a left/right arrow key is pressed. Sorry, but I can't replicate whatever problem you're having with the code I've been given.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.