Pygame

Grey overlay

Pink

Green

Blue

Cream

Liliac

Purple

Yellow

Objectives

Developing Programming and Development (learning strand), specifically:

  • Understand the concept of Sprites and how they can used as game objects.
  • Understand and use variables to change to keep track of the X and Y coordinates of a sprite.
  • Understand the process of painting the screen with image(s) known as Bit Blitting or Blit.
  • Understand that Event Handlers can be used to perform an action detected by an Event Listener.

Pythagoras

Design It

  • Now that we've learnt some cool coding techniques, we can try and bring them all together to create a nice target shooting game like this:

Learn It

  • Pythagoras invented his theorem in ancient Greek times so we could make video games.
  • You may remember this from your maths classes:

8-1.png

  • Given the lengths of a and b we are able to calculate the length of c using the equation above.

Download It

Code It

  • First we'll need some graphics for our game. At the very least, we'll need to load a player and target image.
  • Save the two images below into your pyGame folder on your H: drive.
  • To keep things neat, make a sub-folder in your pyGame folder and call it img. Put both images in there. NOTE: If you don't place them into the sub-folder, the code examples used in this lesson won't work.

bird1.png crossHair.png

  • We'll start off by establishing the target's starting X and Y position on the screen. Add these lines just before your game loop, around line 18:
gameState = "running"  # controls which state the games is in
player1Image = pygame.image.load("img/crossHair.png")
targetImage = pygame.image.load("img/bird1.png")
targetX = 100
targetY = 100
  • It should look like this:

8-2.png

  • Using the ideas used in lesson 3 mouse events, we'll have the target graphic follow the mouse cursor around the screen. Add the following lines into your game loop, around line 30.
  • NOTE: This code must be indented in order to work:
screen.fill(white)
mousePos = pygame.mouse.get_pos()
player1X = pygame.mouse.get_pos()[0] - 50
player1Y = pygame.mouse.get_pos()[1] - 50

target = screen.blit(targetImage, (targetX, targetY))
player1 = screen.blit(player1Image, (player1X, player1Y))

8-3.png

Learn It

  • In our game, we'll need to know when the player has 'hit' the target bird.
  • We'll draw an imaginary circle around the bird's X, Y coordinates and say that when the player clicks the mouse, if the mouse is inside the circle, they've hit the target.
  • We could also use the idea of how close the player is to the middle to decide their accuracy.
  • You've almost certianly covered Pythagoras' theorem in maths, but how does it help us solve this problem?

8-4.png

  • Look at the diagram above for a moment. We can use the variables we already have to establish the X and Y distance from the bird to the target, then apply Pythagoras to see how far away it is.

Code It

  • In order to keep the game loop as tidy as possible we will create a function to calculate c.
  • The function will need to use the math module's square root method. This isn't built into Python by default, so some extra code in the form of the math module must be imported.
  • Add the following lines to the top of your program, around line 4:
import math

def pythag(pX, pY, tX, tY):
    a = pX - tX
    b = pY - tY
    c = math.sqrt(a ** 2 + b ** 2)
    print (c)

8-5.png

  • Finally, we must call the function and send it the player's and target's X and Y positions to perform the calculation with. Add this code inside the game loop, near line 44.
  • NOTE: This code must also be indented in order to work:
pythag(player1X, player1Y, targetX, targetY)

8-6.png

  • Now test your code. Look at the bottom of the pyGame shell window. What is this telling you?
  • If you're struggling with the concepts, this video tutorial will help with this lesson:

Badge It

  • Silver: Add comments explaining your code, then upload it for marking.
  • Gold: Use the function from the 'Text me' lesson to display how accurate the player was.
  • Platinum: Use Pygame.draw.line to draw the 3 sides of the triangle, like in the screenshot below.

8-pythanFinished.png

Validate