Part 4: Events – Draw the circle wherever the mouse is clicked.

4-mouseclick

We recognize mouse clicks and draw a circle wherever the mouse is clicked.


We remove the loop code and most of the variables that we don’t need.
We just need the code.

RADIUS = 20
ZEROINTENSITY = 0
MAXINTENSITY = 255

Now next steps, we need to recognize whenever the mouse is clicked, recognize the position where the mouse is clicked and draw a circle at that position.
And move the update inside the loop. The changed code is in BOLD.

while True:
    pos = pygame.mouse.get_pos()
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            #till sys.exit() is not mentioned, it will not end the program
            #but pygame has quit, hence the for loop to get events from pygame
            #will fail and hence will get an error if program does not exit
            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
         pygame.draw.circle(SCREEN, COLOR, pos, RADIUS)



    #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
    
pygame.display.update()

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

    #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
    pygame.display.update()

What is the next challenge? Change the colour of the circle on space key press.
Ready?Whooo!

Advertisements

2 thoughts on “Part 4: Events – Draw the circle wherever the mouse is clicked.

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