Part 7: Events- Draw circles of different colours at the mouse position on space key press

4-mouseclick

To run the code, press the space key, a circle is drawn where the mouse has been positioned. Move the mouse and press the space key and do the same.


The only change in code is to move the draw and the update inside the “if'” condition.



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

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
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):
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, 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



You can modify this to mouse key press too.

Just change the "if" condition to.

if (events.type == pygame.MOUSEBUTTONDOWN):

And if you want a similar pattern as Part 6, initialize COLOR once in the beginning and move th circle draw and update inside the "for" instead of inside the "if". Now try this code by moving the mouse and clicking the mouse to change the colour.



COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))

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.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, pos, RADIUS)
pygame.display.update(circlerect)

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

#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

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, pos, RADIUS)
pygame.display.update(circlerect)

The next challenge - move the circle on arrow key press or WASD keys (W for up, A for left, S for down and D for right. Whoo!! we have done a lot till now. Now for the final stuff to make a simple game. Are we in?

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