Draw Multiple Circles on One Screen Android
This is our next challenge! Are you ready?
We shall depict many shapes at random positions on the screen.
Since nosotros 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 annotate (with a '#') the draw commands and related lawmaking that we had earlier
LEFT = 100 TOP = 100 RECTCOORD = [LEFT, TOP, LENGTH, WIDTH] rect1 = pygame.Rect(RECTCOORD) Carmine = 255 YELLOW = 230 BLUE = 200 COLOR = (Crimson, YELLOW, BLUE) pygame.describe.rect(SCREEN, Color, rect1, 2) LEFT = 20 TOP = 20 RECTCOORD = [LEFT, Elevation, LENGTH, WIDTH] rect2 = pygame.Rect(RECTCOORD) pygame.draw.rect(SCREEN, Color, rect2, 0) LEFT = 200 TOP = 200 RADIUS = 20 pygame.draw.circumvolve(SCREEN, COLOR, (LEFT,TOP), RADIUS,0)
Basically, all lawmaking except the LENGTH and the WIDTH variables shall be removed.
We shall employ a loop and random. A loop is to follow commands repeatedly. To describe shapes repeatedly for a specific number of times we use
for i in range(ten) print("hello") the variable 'i' volition change its value from 0 to 9, and follow the commands within the loop 10 times(0-ix).
All the commands that need to be repeated will be inside the for loop and they need to be at the same spacing, but below the 'p' of print("hello")
For more than information about range, refer to for loop and range
And random.randint(0,255) will requite a random number between 0 and 255. Every time we run the lawmaking, nosotros tin can get whatever of the 256 values and it is by take a chance (random).
To become 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 unabridged code.
LENGTH = 20 WIDTH = 20 RADIUS = xx 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.depict.circle(SCREEN, Colour, CIRCLEPOS, RADIUS, SOLID)
The entire lawmaking.
#Import statements are to enable the code to employ the functions from the library import pygame import sys import bone import random #instructions to windows to centre the game window in the centre of #the screen, which it might ignore bone.environ["SDL_VIDEO_CENTERED"] = "1" #initialize pygame pygame.init() #500, 500 = width and pinnacle of the foursquare shaped window on which the game tin be played #putting [500,500] in square brackets means that information technology is a list. #A list is a data construction. In layman terms, it is a pocketbook in which you can #shop the width and height of the rectangle together. #"screen", is the handle that you always use to draw on the window. Hence recollect the name #The width and tiptop ideally should exist variables and assigned values in the #kickoff of the code. If later you need to apply the same [500,500] once more or #want to modify it to [300, 550], y'all tin can do it by irresolute the value of the #variable at one location, instead of irresolute the number 500 to 300 at multiple #places. #And ideally these are constants, whose values you lot 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 style #screen = pygame.display.set_mode([500, 500]) #Correct way SCREENWIDTH = 500 SCREENHEIGHT = 500 SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT] SCREEN = pygame.display.set_mode(SCREENSIZE) #caption for the game pygame.brandish.set_caption("My first game in pygame") #arc, circumvolve, 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 = xx WIDTH = xx 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) Colour = (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 Truthful") #In computer systems, at that place is always an result loop always checking for user inputs #(It keeps checking for primal presses on the key board and whenever for instance, "caps lock" #is preseed all the messages 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, similar key presses,quit etc #and take the right action #here we shall first handle the "quit" consequence, when the "X" button is pressed on the #pygame screen #when it is pressed we want to exit pygame and shut the window (sys.go out()) while True: for events in pygame.outcome.get(): if events.type == pygame.QUIT: pygame.quit() #till sys.go out() is not mentioned, it will non stop the programme #only pygame has quit, hence the for loop to get events from pygame #volition fail and hence will go an mistake if program does not leave sys.get out() #Here for any commands within 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 within the while WOW! This is colourful. What side by side? We shall move some code within the event loop.
When nosotros click the mouse at any position of the screen, it should draw a circumvolve.
Rearing to go? Come on!
Source: https://diycomputerscienceandelectronics.wordpress.com/2017/04/28/pygame-and-python-continued-draw-shapes-randomly-at-random-positions-on-the-screen20170428orderasc/
0 Response to "Draw Multiple Circles on One Screen Android"
Post a Comment