Part 3: Draw shapes at random positions on the screen.

3_randompositions_colours

This is our next challenge! Are you ready?
We shall draw many shapes at random positions on the screen.


Since we would be positioning the shapes at random positions of the screen instead of positions specified by us, we shall use a library for random(a library of code and we can borrow pre-built code from it).

Add a line import random below the imports.

import pygame
import sys
import os
import random

Remove or comment (with a ‘#’) the draw commands and related code that we had earlier

LEFT = 100
TOP = 100
RECTCOORD = [LEFT, TOP, LENGTH, WIDTH]
rect1 = pygame.Rect(RECTCOORD)

RED = 255
YELLOW = 230
BLUE = 200
COLOR = (RED, YELLOW, BLUE)

pygame.draw.rect(SCREEN, COLOR, rect1, 2)

LEFT = 20
TOP = 20
RECTCOORD = [LEFT, TOP, LENGTH, WIDTH]
rect2 = pygame.Rect(RECTCOORD)

pygame.draw.rect(SCREEN, COLOR, rect2, 0)

LEFT = 200
TOP = 200
RADIUS = 20
pygame.draw.circle(SCREEN, COLOR, (LEFT,TOP), RADIUS,0)

Basically, all code except the LENGTH and the WIDTH variables shall be removed.

We shall use a loop and random. A loop is to follow commands repeatedly. To draw shapes repeatedly for a specific number of times we use

for i in range(10)
    print("hello")

the variable ‘i’ will change its value from 0 to 9, and follow the commands inside the loop 10 times(0-9).
All the commands that need to be repeated will be inside the for loop and they need to be at the same spacing, just below the ‘p’ of print(“hello”)

For more information about range, refer to for loop and range

And random.randint(0,255) will give a random number between 0 and 255. Every time we run the code, we can get any of the 256 values and it is by chance (random).

To get random positions and colors,

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

LENGTH = 20
WIDTH = 20
LEFT = random.randint(MINPOS,MAXPOS)
TOP = random.randint(MINPOS,MAXPOS)
rect1 = pygame.Rect(LEFT, TOP, LENGTH, WIDTH)

The new code altogether and the entire code.

LENGTH = 20
WIDTH = 20
RADIUS = 20

LOOPCOUNT = 100
MINPOS = 0
MAXPOS = 480

ZEROINTENSITY = 0
MAXINTENSITY = 255

SOLID = 0
for i in range(LOOPCOUNT):
    LEFT = random.randint(MINPOS,MAXPOS)
    TOP = random.randint(MINPOS,MAXPOS)
    rect1 = pygame.Rect(LEFT, TOP, LENGTH, WIDTH)

    COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY),random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
    
    pygame.draw.rect(SCREEN, COLOR, rect1, SOLID)

    CIRCLEPOS = (random.randint(MINPOS,MAXPOS), random.randint(MINPOS,MAXPOS)) 
    pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS, SOLID)

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()

#500, 500 = width and height of the square shaped window on which the game can be played
#putting [500,500] in square brackets means that it is a list.
#A list is a data structure. In layman terms, it is a bag in which you can
#store the width and height of the rectangle together.

#"screen", is the handle that you always use to draw on the window. Hence remember the name

#The width and height ideally should be variables and assigned values in the
#beginning of the code. If later you need to use the same [500,500] again or
#want to change it to [300, 550], you can do it by changing the value of the
#variable at one location, instead of changing the number 500 to 300 at multiple
#places.
#And ideally these are constants, whose values you dont change during in the code
#Hence the variables are written in capital letters
#There is no "const" keyword in python (keywords are special words recognised by the
#programming language like "for", "if", "while" etc)

#Not the right way
#screen = pygame.display.set_mode([500, 500])

#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")

#arc, circle, line, polygon etc
#The left top corner of the screen is considered to be the position for (0,0)
#With respect to that we give the left, top coordinates
LENGTH = 20
WIDTH = 20
RADIUS = 20

LOOPCOUNT = 100
MINPOS = 0
MAXPOS = 480

ZEROINTENSITY = 0
MAXINTENSITY = 255

SOLID = 0
for i in range(LOOPCOUNT):
    LEFT = random.randint(MINPOS,MAXPOS)
    TOP = random.randint(MINPOS,MAXPOS)
    rect1 = pygame.Rect(LEFT, TOP, LENGTH, WIDTH)

    COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
    
    pygame.draw.rect(SCREEN, COLOR, rect1, SOLID)

    
    CIRCLEPOS = (random.randint(MINPOS,MAXPOS), random.randint(MINPOS,MAXPOS)) 
    pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS, SOLID)
    
pygame.display.update()

#The while loop, which keeps running for ever (becaue we have "while True")
#In computer systems, there is always an event loop always checking for user inputs
#(It keeps checking for key presses on the key board and whenever for example, "caps lock"
#is preseed all the letters you trype are printed in upper case.

#Similarly, an event loop which runs the code over and over again for ever, till
#you choose to quit is required in pygame.
#In that loop, you check for all events in the bag of events, like key presses,quit etc
#and take the right action

#here we shall first handle the "quit" event, when the "X" button is pressed on the
#pygame screen
#when it is pressed we want to exit pygame and close the window (sys.exit())
while True:
    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  
    
    #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

WOW! This is colourful. What next? We shall move some code inside the event loop.
When we click the mouse at any position of the screen, it should draw a circle.

Rearing to go? Come on!

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