CSci 157 - Lab 7
Purpose: the main objective is to continue to work with selection statements and the graphics classes in the context of a very simple game. Random number generators, in particular, are used extensively in game programming. Be creative and see what you can do with them.
Reminders and Warmup Questions:
Note: Warmup questions are discussion questions...you don't need to write anything down.
- Don't try to program everything at once. Set up the overall program first, then add/change things until things look right. One goal is to always have something that runs, even if it isn't complete, as you work on it.
- To get credit for completing lab during the session, be sure to demonstrate each program as requested in the directions, so the instructor can record completion of the lab.
- What module needs to be imported for the sqrt function? What module do you need to generate random numbers?
- How might you construct a bullseye target with three different rings? If we try layering three different objects, which object contains a point in all three?
- How can a program tell if a point is contained inside a circle?
I. A Dart game
Download the program darts.py - this program contains a basic framework for you to fill in. The final program will use random number generation to create your own "game" of darts.
- Setup:
- Set up a dart board with a bull's eye and at least two outer
rings of different colors (three concentric circles: one inside
the other, centered on the same point). The largest circle will
have radius 100, so make the window large enough to show the
entire thing. You can choose the sizes of the other two. Set a
different fill color for each circle. What order should the
circles be constructed in order that we can see all three?
- Label all parts of the game for the user: there should be a
"title" for the game somewhere in large
font. Somewhere on
the dartboard (for example, near the top of each circle) should be
a label with a point value. Add instructions near the
bottom of the window telling user what to do to throw a
dart: Click to
shoot! or something like that.
- Set up a dart board with a bull's eye and at least two outer
rings of different colors (three concentric circles: one inside
the other, centered on the same point). The largest circle will
have radius 100, so make the window large enough to show the
entire thing. You can choose the sizes of the other two. Set a
different fill color for each circle. What order should the
circles be constructed in order that we can see all three?
- On mouse click:
- Code for generating and recording a dart throw and updating the
score goes inside the for statement
- Clicking the mouse will cause a dart to be "thrown" at your
dartboard. A dart is a Circle with radius 3 or so
(experiment and use whatever looks right). Use the random module
to generate integers for the x- and y-coordinates where the dart
hits (you'll want to narrow the random number ranges to the area
your rings are, or at least within the confines of the window).
- When a dart lands on the board:
- Determine if the dart landed inside one of the circles. Start by checking the smallest circle, then each larger one. Add an appropriate amount to the score. If it lands outside all circles, it gets no points.
- If it lands inside a circle, change the color of the dart to yellow or another color different from the three rings (missed darts remain black)
- Display the score using large font in the upper right corner; it should increment appropriately each time a target is hit
- Display a message that says something like "good shot!" whenever a random shot hits a ring; this message should disappear when the user takes the next shot.
- Try to keep any and all messages from overlaying the circles or other messages. Keep the interface clean.
- Code for generating and recording a dart throw and updating the
score goes inside the for statement
-
After all throws, change the color of the player's score to red. Add some text such as "Game Over" somewhere that looks reasonable.
- Demonstrate your game to the instructor.
Reminder - always do this at the end of labs
Make sure both team members have a copy of the programs developed
in this lab.