Games · Guided

Build Hit the Target in Python

A first click game: every click lands a dot, green inside the target and red outside.

You write: A function that says whether a point is inside a rectangle, and the function that answers each click.

Loading the game…

What to build

The gold rectangle is a target. Its left edge is at x = 200 and its right edge at x = 400; its top is at y = 120 and its bottom at y = 280.

  1. Write inside(x, y). It returns True when the point (x, y) is in the target, edges included, and False otherwise. It draws and prints nothing.
  2. Write clicked(x, y). It draws a circle of radius 8 at the click: "green" if inside(x, y) is True, "red" if not.
  3. Hand clicked to on_click.

Then play: click inside and outside the target.

Not sure where to start?

This game is part of “Functions”, a lesson of Build with Python. The lesson builds up to it, and the course before it starts from zero. Both are free.

Read the lessonSee the course

More games

  • Tic-Tac-Toe: A two-player Tic-Tac-Toe you can play in the page: turns, a win in any line, and a draw.
  • Lights: A puzzle on a five-by-five grid: a click switches a light and its neighbours, and the goal is to turn every light off.
  • Four in a Row: A game for two: counters drop down the columns of a board, and four in a line wins.
  • Pairs: A memory game: sixteen face-down cards hide eight pairs, and a turn flips two.