Part 8: Move the circle (up, down, left and right) on key movement

WASD-arrows

The WASD and arrow keys can be used to control the shapes we draw. How do we recognize these arrow keys? By recognizing and handling the events.


Add the line

from pygame.locals import *

below the imports. This will reduce typing, if you need it. Instead of writing pygame.K_DOWN you can write K_DOWN. Personally, I prefer, writing pygame.K_DOWN, since I get to type Ctrl-Space after the dot and see what all events are supported and choose one instead of having them in my head.

Draw the first rectangle before the while loop.

X = 40
Y = 40
CIRCLEPOS = (X, Y)
COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
pygame.display.update(circlerect)

Use the function

pygame.key.get_pressed()

to get the key pressed every time the while loop runs.
If the key is WASD or the arrow keys, reduce or increase the X,Y positions by 1.

    1. Up arrow key is pressed, y position should be reduced by 1.
    1. Down arrow key is pressed, y position should be increased by 1.
    1. Left arrow key is pressed, x position should be reduced by 1.
    Right arrow key is pressed, y position should be increased by 1.

And if it goes outside the SCREEN (Remember the LEFT, TOP corner of the SCREEN is (0,0)),

    1. Up arrow key is pressed, when Y position is less than zero, make the circle enter from below, set Y to SCREENHEIGHT.
    1. Down arrow key is pressed, when Y > SCREENHEIGHT, make the circle enter from above, set Y position to 0.
    Left arrow key is pressed, when X SCREENWIDTH, make the circle enter from the left, set X position to 0.

Here is the code, with changes in BOLD

X = 40
Y = 40
CIRCLEPOS = (X, Y)
COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
pygame.display.update(circlerect)

BLACK = (0,0,0)

while True:
#    pos = pygame.mouse.get_pos()
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        #Here for any commands inside the for loop
        #if (events.type == pygame.KEYDOWN) and (events.key == pygame.K_SPACE):
        if (events.type == pygame.MOUSEBUTTONDOWN):
            COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
            #note- we have skipped the last parameter and by default, 0 is taken
            circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
            pygame.display.update(circlerect)
    #beware of the positioning of this line. It should be inside the while
    #for all the commands that need to be executed inside the while

    user_input = pygame.key.get_pressed()
    
    if(user_input[pygame.K_UP]):
        Y=Y-1
        if(YSCREENHEIGHT):Y=0
    elif(user_input[pygame.K_LEFT]):
        X=X-1
        if(XSCREENWIDTH):X=0
    CIRCLEPOS = (X,Y)
    #exits the borders reposition

    SCREEN.fill(BLACK)
    circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
    pygame.display.update()

Now while updating change it to

pygame.display.update(circlerect)

and see the difference.
Also, comment out

SCREEN.fill(BLACK)

for both update with circlerect and the whole screen(with empty parameters) and see the difference.
Can you explain this?

Also, change the whole set in BOLD where we receive the arrow/WASD key press and change the movement of the circle in the “for” loop and see the difference.

while True:
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        #Here for any commands inside the for loop
        #if (events.type == pygame.KEYDOWN) and (events.key == pygame.K_SPACE):
        if (events.type == pygame.MOUSEBUTTONDOWN):
            COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
            #note- we have skipped the last parameter and by default, 0 is taken
            circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
            pygame.display.update(circlerect)
    #beware of the positioning of this line. It should be inside the while
    #for all the commands that need to be executed inside the while

        user_input = pygame.key.get_pressed()
        
        if(user_input[pygame.K_UP]):
            Y=Y-1
            if(YSCREENHEIGHT):Y=0
        elif(user_input[pygame.K_LEFT]):
            X=X-1
            if(XSCREENWIDTH):X=0
        CIRCLEPOS = (X,Y)
        #exits the borders reposition

        SCREEN.fill(BLACK)
        circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
        pygame.display.update()

What happens now? The movement is very slow? Can you explain this?

Now the entire code.

#Import statements are to enable the code to use the functions from the library
import pygame
import sys
import os
import random
from pygame.locals import *

#instructions to windows to center the game window in the center of
#the screen, which it might ignore
os.environ["SDL_VIDEO_CENTERED"] = "1"

#initialize pygame
pygame.init()

#Right way
SCREENWIDTH = 500
SCREENHEIGHT = 500
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = pygame.display.set_mode(SCREENSIZE)

#caption for the game
pygame.display.set_caption("My first game in pygame")

RADIUS = 20
ZEROINTENSITY = 0
MAXINTENSITY = 255

X = 40
Y = 40
CIRCLEPOS = (X, Y)
COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
pygame.display.update(circlerect)

BLACK = (0,0,0)

while True:
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        #Here for any commands inside the for loop
        #if (events.type == pygame.KEYDOWN) and (events.key == pygame.K_SPACE):
        if (events.type == pygame.MOUSEBUTTONDOWN):
            COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
            #note- we have skipped the last parameter and by default, 0 is taken
            circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
            pygame.display.update(circlerect)
    #beware of the positioning of this line. It should be inside the while
    #for all the commands that need to be executed inside the while

    user_input = pygame.key.get_pressed()
    
    if(user_input[pygame.K_UP]):
        Y=Y-1
        if(YSCREENHEIGHT):Y=0
    elif(user_input[pygame.K_LEFT]):
        X=X-1
        if(XSCREENWIDTH):X=0
    CIRCLEPOS = (X,Y)
    #exits the borders reposition

    SCREEN.fill(BLACK)
    circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
    pygame.display.update()

Next challenge, can you support for WASD keys too? We have only added support for arrow keys so far. Raring to go?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s