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.
- Write
inside(x, y). It returnsTruewhen the point (x, y) is in the target, edges included, andFalseotherwise. It draws and prints nothing. - Write
clicked(x, y). It draws a circle of radius 8 at the click:"green"ifinside(x, y)is True,"red"if not. - Hand
clickedtoon_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 courseMore 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.