Part 5: Events- Change the colour of the circle on space key press.

5_changecolorofsinglecircle

We are going to recognize a space key press and are going to change the colour of the circle, whenever it is pressed.


Before the while loop starts we need the initial colour of the circle and the circle position. And the circle needs to be drawn once. Then it can be drawn again, only when the space key is pressed.

The colour of the circle will change on key press, hence we need to change it in the “if” condition of space key press, but the position will remain the same and need not be set.

COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
YPOS = 40
XPOS = 40
POS = (XPOS, YPOS)
pygame.draw.circle(SCREEN, COLOR, POS, RADIUS)
pygame.display.update()

In the while loop remove the line

pos = pygame.mouse.get_pos()

Add an “if” condition to recognize space key press. Any key is “down” and the key is “space” is the criteria.

if (events.type == pygame.KEYDOWN) and (events.key == pygame.K_SPACE):
            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
            pygame.draw.circle(SCREEN, COLOR, POS, RADIUS)
            pygame.display.update()

Now try drawing the circle and updating in the while and outside the “for” loop. Only a single tab space from while. Or in the “for”.

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):
            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
     pygame.draw.circle(SCREEN, COLOR, POS, RADIUS)
     pygame.display.update()

The result is the same, but we are updating the screen multiple times, which is unneccesary.
Another optimization (reducing the work for the CPU) would be to update just the portion where the circle has been drawn.

circlerect = pygame.draw.circle(SCREEN, COLOR, POS, RADIUS)
pygame.display.update(circlerect)

And the fifth parameter (input to the draw circle function) is optional, if not given it will take it as zero.

pygame.draw.circle(SCREEN, COLOR, POS, RADIUS, 2)

Now for the whole code.

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

#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

COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
YPOS = 40
XPOS = 40
POS = (XPOS, YPOS)
circlerect = pygame.draw.circle(SCREEN, COLOR, POS, RADIUS)
pygame.display.update(circlerect)

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):
            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
            pygame.draw.circle(SCREEN, COLOR, POS, 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

The next challenge- We shall combine drawing the circle on mouse movement and also change the colour on pressing the key to see a nice pattern.
Are you ready?

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