Creating movable objects

Creating non-movable objects

To start creating movable objects, you need to start with non-movable objects. We'll start of with a square. To create of square of length y and width x (ill use numbers) use the following line

# this will create a rectangle of RGB value (0, 128, 255) at position (30, 30) and of scale (60, 60) 
pygame.draw.rect(gameDisplay, (0, 128, 255), pygame.Rect(30, 30, 60, 60))

Game display refers to the blank screen variable created earlier

Adding keyboard input

Now we can get inputs from the user to make the square move. Add this code portion into the events loop. We'll add what action does later

if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
    #action

Changing the position of the square

Now that we can get keyboard input, let's add the code that changes the position of the square.

To change the position of the square now, you'll meed to change the values of two constantly changing variables. Lets define this variable before the loop that checks if the game is done

    #setting initial position variables for the rectangle
    rectX = 10
    rectY = 10

Now we set the position of the rectangle to those variables. Change the line defining the rectangle to the following

pygame.draw.rect(gameDisplay, (0, 128, 255), pygame.Rect(rectX, rectY, 60, 60))

Now we have to change the variable values. I'll want to map moving to the arrow keys.

Because of the internal workings of pygame, the KEYDOWN event is defined by actually being pressed and not held. So to get the square moving we'll change the code to the following

keys = pygame.key.get_pressed()  #checking pressed keys
            if keys[pygame.K_UP]:
                rectY = rectY - .25
            if keys[pygame.K_DOWN]:
                rectY = rectY + .25
            if keys[pygame.K_LEFT]:
                rectX = rectX - .25
            if keys[pygame.K_RIGHT]:
                rectX = rectX + .25

Lastly, since the square is constantly being created we need to erase the square each frame. To do this, put this before the definition of then square

#resets the screen
gameDisplay.fill((0,0,0))

The final code should look like this

#Library that the project is based on
import pygame

#This is the main function which is where I'll write the majority of the code
def main():
    #this code initializes the code within the library
    pygame.init()
    #this bit of code sets the black screen display
    gameDisplay = pygame.display.set_mode((800,600))
    #setting initial position variables for the rectangle
    rectX = 10
    rectY = 10
    #This is the code that exitrs the game if the x in the corner is hit
    done = False
    while not done:
            for event in pygame.event.get():
                #pygame.QUIT is an event defined by the pygame lib
                if event.type == pygame.QUIT:
                        done = True
                        #system function
                        exit()
            keys = pygame.key.get_pressed()  #checking pressed keys
            if keys[pygame.K_UP]:
                rectY = rectY - .25
            if keys[pygame.K_DOWN]:
                rectY = rectY + .25
            if keys[pygame.K_LEFT]:
                rectX = rectX - .25
            if keys[pygame.K_RIGHT]:
                rectX = rectX + .25
            #resets the screen    
            gameDisplay.fill((0,0,0))
            # this will create a rectangle of RGB value (0, 128, 255) at position (30, 30) and of scale (60, 60) 
            pygame.draw.rect(gameDisplay, (0, 128, 255), pygame.Rect(rectX, rectY, 60, 60))

            #dont worry about this, it just buffers the screen
            pygame.display.flip()

#this is where the code enters and decides where to go
if __name__== "__main__":
  #the function that you want to start the game on
  main()

Last updated

Was this helpful?