Before we go further, we need to stop again and learn a few things. For the next program, we'll need some new functions that we haven't seen before. Let's see them one by one.
Using Pygame we can draw circles on the screen. This is the code we need to use:
pygame.draw.circle(screen, color, position, radius)
The words screen
, color
, position
and radius
are information that we'll have to provide. This is an example of use:
screen = pygame.display.set_mode([800, 600])
pygame.draw.circle(screen, [255, 255, 255], [100, 100], 10)
The code above will draw a white circle with a 10-pixel radius at the position x=100 and y=100. You will have to provide a screen
similar to how we have done before. Write a program to try it out. Then try these challenges:
In Python, there are several ways of generating random numbers. Regardless of how we do it, we'll need to import the random
library into our program. Then we can use a function such as the following:
random.randint(1, 10)
That code will generate a random number between 1 and 10. Here are some challenges:
OK, so now let's write a program that uses all the above. Write code for each one of the following steps, one by one:
random
library.Circle
.Circle
object and put it in a variable.x
coordinate, between 0 and 800.y
coordinate, between 0 and 600.radius
, between 5 and 50.