How do i move the square up and down in pygame?

Question:

i need to allow the square to move up and down. From my following code the square moves left and right but i need to make sure it moves up and down as well.

import pygame, sys
from pygame.locals import QUIT


def draw_rectangle(surface, rectangle):
    pygame.draw.rect(surface, (0, 255, 0), rectangle)


def main():
    pygame.init()

    #Intalise
    DISPLAYSURF = pygame.display.set_mode((400, 300))
    pygame.display.set_caption('Rectangles and Squares')

    game_clock = pygame.time.Clock()

    #Variables
    x_position = 100

    #Game Loop
    running = True
    while running:
        #Update Section

        #Update delta_time
        delta_time = game_clock.tick(60)/100

        #Handle events
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        #Process keyboard inputs
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            x_position -= 10 * delta_time
        if keys[pygame.K_RIGHT]:
            x_position += 10 * delta_time

        #Draw Section
        DISPLAYSURF.fill((128, 128, 128))

        draw_rectangle(DISPLAYSURF, pygame.Rect(x_position, 100, 50, 50))

        pygame.display.flip()

if __name__ == "__main__":
    main()
Asked By: hal man

||

Answers:

A good piece of code with a good effort. There are some hints in the comments to the original question, but since there are multiple changes i have added them.

here are the 3 basic modifications that are made:

  1. introduce an initial y_position.
  2. enable up / down moves (remember y=0 at the top of screen).
  3. add y_position into draw_rectangle function.

so this code works:

import pygame, sys
from pygame.locals import QUIT


def draw_rectangle(surface, rectangle):
    pygame.draw.rect(surface, (0, 255, 0), rectangle)


def main():
    pygame.init()

    #Intalise
    DISPLAYSURF = pygame.display.set_mode((400, 300))
    pygame.display.set_caption('Rectangles and Squares')

    game_clock = pygame.time.Clock()

    #Variables
    x_position = 100
    y_position = 100    # <---  this

    #Game Loop
    running = True
    while running:
        #Update Section

        #Update delta_time
        delta_time = game_clock.tick(60)/100

        #Handle events
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        #Process keyboard inputs
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            x_position -= 10 * delta_time
        if keys[pygame.K_RIGHT]:
            x_position += 10 * delta_time

        # handles the y positions (note y is from top to bottom)    
        if keys[pygame.K_UP]:
            y_position -= 10 * delta_time
        if keys[pygame.K_DOWN]:
            y_position += 10 * delta_time
        #Draw Section
        DISPLAYSURF.fill((128, 128, 128))

        draw_rectangle(DISPLAYSURF, pygame.Rect(x_position, y_position, 50, 50))    # <-- this

        pygame.display.flip()

if __name__ == "__main__":
    main()
Answered By: D.L
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.