Grey overlay









Design It

  • Now we've learnt some individual coding techniques, we can try and bring them 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:


  • 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:


  • Using the ideas used in the Mouse lesson, we'll have the target graphic follow the mouse cursor. Add these lines into your game loop, around line 30:
  • NOTE: This code must be indented in order to work.
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))


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?


  • 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 these 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)


  • 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)


  • Now test your code. Look at the bottom of the pyCharm 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 form 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.