Join Mark Niemann-Ross for an in-depth discussion in this video Challenge: Create a treasure map, part of Teach Kids Programming with PHP.
- View Offline
Here's a challenge, I'm creating an adventure game and I need your help. In my game characters travel through the winderness collecting gold coins. The game has a map, and on the map are scattered a random number of gold coins. Here's what it looks like with just one gold coin. I need a program that will show the map with the random coins. Each time I refresh the view of the map, it also needs to show a different number of coins and place them in new and random positions.
Let's take a look at the challenge code. It's really simple, in fact, it doesn't have any php code at all. Line six includes the map in the background of the body. You can see it here, background image url bluemap.jpg. Lines eight, nine, and ten are HTML that places a picture of goldcoin.png on the map. Note that the position of the gold coin is controlled by left followed by a number, and top followed by a number. In this case, the gold coin is placed left 100 pixels and top 100 pixels.
So, if left is equal to zero, and top is equal to zero, the coin will appear in the top left corner of the map. If left is equal to 650 and top is equal to zero The coin will appear in the upper right hand corner of the map. If left is zero and top is 400, the coin is in the lower left hand corner. And if left is 650 and top is 400, the coin will appear in the lower right hand corner of the map. Here's your challenge. First, place a random number of coins in random places on the map and a hint, left, should be between 0 and 650 so the coin appears on the map.
And top should be between 0 and 400. Finally you'll need to know about a php function called rand. If you give rand a minimum and maximum number it will return a random number between those two values, and I'm pretty sure you're going to have to do a loop of some sort. Perhaps a while loop. This challenge should take you about 15 minutes to complete, good luck let's get cracking.
- Teacher's Guide: Introduces the concepts in the chapter and preps the adult on places where the student might get stuck
- Try It: Immediately engages the student, showing working code in action
- Check It Out: Encourages the adult and student to engage with and edit some existing code
- The Facts: A lecture for the student to watch
- Extend It: An exploration of expanded and extended concepts
- Challenge: A hands-on coding challenge for the student
- Solution: A step-by-step solution presented by the author
This course, in essence, acts as a lesson plan to help you teach PHP to a beginner. Its structured curriculum supports those who have a solid understanding of PHP, but don't necessarily know how to teach PHP effectively to kids.