Part 5 - Basic Graphics in PyGame

PyGame

PyGame is a graphics library made for Python that we are going to use to create programs with graphics.

Here is the documentation for all of the PyGame libraries: https://www.pygame.org/docs/. This page also includes links to tutorials and example code on how to use PyGame. There is a lot to learn that we will not get to in this course.

This section will give an introduction to drawing basic shapes using the PyGame graphical library.

What to do/hand in?

1. Download the Graphics Template.py file and use it as a starting point for each pygame program you make from now on.

Read through the Part 5: Graphics in PyGame tutorial.

2. Make sure that you read the corresponding portions of Chapter 5 - Programming Arcade Games as suggested in the Part 5 - Graphics tutorial.

3. Attempt to explain what each section of code does in the example program. Experiment with the graphics commands in #7.

4. Complete and hand in Chapter 5 Graphics Worksheet.

5. Create and hand in your own graphics assignment 'Still Life' as per the Part 5 - Graphics tutorial.

    • Make sure to use a loop to draw something. It must be a picture of something (a car, house, boar, kitten), nothing abstract (like random shapes).
    • Comment your code. If you have 5 lines of code to draw a car, indicate that with a comment above.

Submit:

  1. Hand in Chapter 5 - Graphics Worksheet
  2. Hand in the Still Life Python program.
      • Try to use at least one loop to draw your graphic.
Part 5 - Graphics.pdf

Still Life Examples

Still Life Examples.pdf

Hints and FAQ's

  1. Create or download a template file using the code in the file Part 5 - Graphics, and use it as a starting point whenever you are making a new program with the PyGame library. You should start with it when making graphics programs in the future.
  2. Importing Libraries. You only need to import a library once into your program. For example: the line 'import pygame' should only appear once, and at the top of your program. This is also true with any other library, such as when you import the random library.
  3. All draw functions in the PyGame library can all be found at this website: https://www.pygame.org/docs/ref/draw.html. Here you can find a description of how they work, and examples of code that use them,
  4. If you do not implement the code to exit your program, it will not close properly when you click on the 'x' on the upper right of the window. You will have to close the Python shell in order to close your program.
  5. If your program contains an error, the Python shell will provide you with information as to what caused it to crash.


Q: How on earth do I use a loop to draw multiple graphic objects?

A: Use a loop to increment an integer value. You can put one or more draw statements in the loop. Use your incrementing variable to either be, or modify a parameter for one of the shapes you wish to draw.

For example:

for x in range(0, 100, 5):
    pygame.draw.rect(screen, RED, [55 + x, 50, 5, 25], 0)