Pygame

Grey overlay

Pink

Green

Blue

Cream

Liliac

Purple

Yellow

Objectives

Developing Programming and Development (learning strand) skills, specifically:

  • Understand that computers store and display shapes by using a drawing list of locations, dimensions and other properties.
  • Understand game animation is achieved by refresh/reload/update the screen with new data at a regular time intervals.

Introduction

Learn It

  • Pygame is a cross-platform set of Python modules designed for creating 2D games. The modules are designed to be simple, easy to use, and fun—a key part of Pygame's ideology.
  • You can read more over at http://www.pygame.org/
  • Pygame was first released in 2000 and has a large community of developers. Have a look at the Awesome section of the web site.
  • Pygame requires several line of code to get things up and running, this has been provided for you in the form of a template to use of all your pygame projects.

1-1.png

Code It: Template

  • Create a folder called pygameIntro.
  • Create a new project using pycharm called pygameCode in the pygameIntro folder.
  • Download the template pygame to your pygameIntro folder as basicShapes.py

Click here for the template

  • The template has two main sections:
    • Game Setup: runs once when the game loads
    • Game loop: runs again and again (loops) 60 times a second
  • We'll start by making some changes to the game loop, to get to grips with drawing basic graphics. Once we're happy with doing this, we can start to animate them.

Run It

  • Firstly, you'll need to set up pyCharm to work on your computer.
  • Secondly, you will need to download some software
  • With the template loaded in pyCharm, we need to test that everything is working.
  • Run the template file by either using Shift-F10 or clicking the green 'play' icon in the top right-hand corner.
  • After a few seconds, you should see the computer create a new window that is black, 1000 * 800 pixels and closes when you tell it to.

Learn It: Screen Geometry

  • Unlike Scratch, Pygame and most professional game engines have the origin x = 0, y = 0 at the top left hand corner.

1-2.png

  • In the image above, a pixel has been placed at x=30, y=40.

Program It

  • Pygame has several built-in shapes that you can draw using simple commands.
  • The .draw.rect() command lets you draw a rectangle on the display. It needs a few pieces of information to be able to do this first, which are placed in brackets. We call these parameters.
    • In theory you could have lots of graphics windows open. We've only got one, which we've called screen. We need to tell pyGame that this is where the shape is to be drawn.
    • We can draw lines and shapes in any of 16.7 million colours. Our code has a few set up for convenient access. In the example below, we're using a colour we've called green.
  • Next, there are four numbers. Let's look at that a little bit
    • We need to state exactly where on the screen windows we'd like to start drawing the rectangle from. The first two digits (50,200) tell pyGame that we want to start drawing 50 pixels from the left edge, and 200px from the top of the screen.
    • To draw a rectangle, we also need to state the width (40px in this case) and the height (20px) that we want our shape to be.

1-rect.png

  • We can draw several other basic shapes too, such as circles.
  • The idea is much the same, as you can see below.

1-circle.png

  • Lines are also possible…

1-line.png

Try It

  • In your pyCharm code, scroll down to line that says, your code starts here, located about two thirds of the way down the page.
  • Copy and paste the line below underneath this line
pygame.draw.rect(screen, green, (50, 200, 40, 20))
# pygame.draw.rect(screen, colour, (x, y, width, height), thickness)
pygame.draw.rect(screen, yellow, (50, 400, 40, 20), 4)
# thickness is optional filled shape if specified
  • When done, your screen should look like this:

1-3.PNG

  • Run the code with Shift-F10
  • What does the extra 4 at the end do?
  • Let's try something else. Remove the lines you wrote previously, and replace them with these and run the program. Make sure they're indented into the page.
pygame.draw.circle(screen, white, (300, 600), 20)
# pygame.draw.circle(screen, colour, (x, y), radius, thickness)
pygame.draw.circle(screen, yellow, (400, 700), 20, 4)
  • Your code should look like this:

1-4.PNG

  • Let's try one more. Replace your circle code with these lines.
  • Run them - what do you think the different parameters are doing?
pygame.draw.line(screen, green, (0, 0), (200, 200))
# pygame.draw.line(screen, colour, (startX, startY), (endX, endY), thickness)
pygame.draw.line(screen, green, (0, 0), (800, 200), 4)
  • Your code should look like this when you're done:

1-5.PNG

Badge It

  • For this lesson’s assessment you are marking your own work. This will allow you see your own progress. This process is often used for online training programmes and is called the ‘honour’ or ‘trust system’. Obviously the key to these systems is being honest. Your teacher will randomly check some students work to moderate their marking.
    • Silver: Can you draw a car or house using code made from rectangles, circles and lines?
    • Gold: Can you add comments below your code from the silver task, explaining in your own words how RGB colours work?
    • Platinum: Can you create two variables to store two new colours? Have you researched how to draw the shapes ellipse and polygon? Can you draw examples of them both shapes using your two new colours.
  • Once you have done above tasks and tested they are working as intended, click here for the self assessment.