Module 4 - Section 2 - Graphics
- identify the commands inside the Pygame basic template
- experiment with various graphics commands
- draw shapes using Python commands
- draw text using Python commands
4.2.7 - Graphics
Open the Basic Pygame Template file (click here if you haven't saved it).
Around line 43 there is a command to fill the background with WHITE color. Change this line so the background is BLACK and run the code to see the difference. (you should change the line to screen.fill(BLACK) )
Now between in lines 45 and 47 we will write code to create a WHITE rectangle.
In line 46 write
pygame.draw.rect(screen, WHITE, [50, 50, 50, 50])
Do not forget to correct the identation if needed.
Pygame is the library, draw is a method, rect (for rectangle) is a type, screen is where to draw.
The first two numbers are the top left coordinates and the second two numbers are the width and height of the rectangle. You can experiment changing these numbers (consider that your screen is 700 x 500) and the color to one of the previously defined. In the image below the command is pygame.draw.rect(screen, WHITE, [150, 80, 70, 90])
You can view what other shapes you can draw here
Try to draw another shape. This time draw a circle writing the code under the line that creates the rectangle.
If you didn't succeed in finding the command on your own, try the following line :
pygame.draw.circle(screen, RED, [200, 140], 30)
[200, 140] is the center of the circle and 30 its radious.
Change the code adding another number before closing the parenthesis like this :
pygame.draw.rect(screen, WHITE, [50, 50, 50, 50], 7)
pygame.draw.circle(screen, RED, [200, 140], 30, 5)
What do you think the last numbers do?