Today's lab looks at nested loops and indefinite loops from Chapter 8.

Mystery Point Game

Today, we are going to write a simple game where the user tries to find our mystery point in the graphics window. Lets first go through the program section by section:

• Opening comments and importing modules: In addition to math and graphics, we will also need to generate random numbers (to put our point at a random location), so, we have also included that module:
# Mystery point game for Lab 9
# Lehman College, CUNY, Fall 2012

from graphics import *
from math import *
from random import *

• We will reuse the distance function from the previous lab:
# Reuse the distance function we wrote for Lab 8:
def dist(p1,p2):
dist = sqrt((p1.getX()-p2.getX())**2 + (p1.getY() - p2.getY())**2)
return dist

• Set up the graphics window:
def main():
w = GraphWin("Target Practice",500,500)
w.setCoords(-250,-250,250,250)

• We will use the built-in random number generator to give coordinates for our mystery points. See Chapter 9 for more discussion on random numbers:
#Generate a mystery point (at a random location):
x = randrange(-200,200)
y = randrange(-200,200)
print("x and y are:",x,y)
mysteryPoint = Point(x,y)

• Set up a Text object to display messages to the user. We changed the font size, since the default one is small and hard to read.
#Text to tell the user what's happening:
t = Text(Point(0,-210), "Click on the mystery point")
t.setSize(16)
t.draw(w)

• Save the point that the user clicked in the variable p. We display its distance to our mystery point in the IDLE shell:
#Get the point:
p = w.getMouse()
p.draw(w)
d = dist(mysteryPoint, p)
print("distance from mystery point is:", d)

• This loop repeats as long as the condition d > 20 holds. So, once the point clicked is within 20 of the mystery point, the loop ends. You can make the game harder (or easier) by decreasing (or increasing) 20. For example, if you change the 20 to 10, then the user has to click even closer to the mystery point to win. If you change the 20 to 100, then the game becomes much easier since they only have to be within 100 to win.
#Keep going until they click close to the point:
while d > 20:
p = w.getMouse()
p.draw(w)
d = dist(mysteryPoint, p)
print("distance from mystery point is:", d)

• Congratulate the user for finding the point and end the program nicely:
t.setText("Congratulations!  You found the mystery point!")

#keep the window up until the user clicks
w.getMouse()
w.close()

main()

Try running the program. How easy is it to win?

Giving Hints

Could you find the point without peeking at the IDLE shell? To make the game a bit easier, we will add hints. The first hint will be to tell the user if they need to click more to the left (or right) to find the point. Here is the pseudocode:

if point clicked is to the left of mystery point
give message "Too far left"
else
give message "Too far right"
How can you tell if the point clicked is to the left of the mystery point? The x-coordinate tells how far left or right a point is, so, we compare the x-coordinates of the two points:
if p.getX() < mysteryPoint.getX():
t.setText("Click again! You were too far left")
else:
t.setText("Click again! You were too far right")

Now, add in the code that will give a hint to move up (or down) depending on where they clicked with respect to the mystery point. Your completed program should have a "left/right" hint and an "low/high" hint to make the game possible to do without looking at our diagonostics on the python shell.

Hint: Before the loop, set up another Text object, t2 to display the message about being too low or too high:

t2 = Text(Point(0,-230), "")
t2.setSize(16)
t2.draw(w)