Computer Programming I

Pacman by Michael

Michael M. decided to make a maze version of Pacman. The object is to consume as many energy dots as possible while avoiding a collision with the ghosts who are chasing you. You have to keep moving because your energy will drop continuously and you have to find new dots to replenish it. This code shows how Michael managed the energy level:

	energy = energy - .1
	IF energy > 100 THEN
		energy = 100
	END IF
	IF energy < 0 OR energy = 0 THEN
		lose
		EXIT DO
	END IF

	
Do you see why the energy level has a maximum of 100?

You can download the file for this game and play it yourself, if you like. It works best if you hit Alt-Enter after you double click on the file name. The game runs only on a PC, unfortunately. But don't worry-- in Computer Programming III and Computer Programming IV, students learn to make programs that run on any platform.

Frogger by Tani

Tani G. worked very hard to make this game of Frogger and he learned a lot about programming. In this classic game, you are the frog--trying to dodge cars as you hop across the road. If you make it that far, you get to the river, where you try to hop onto logs to make it across the river. Accomplishing that will promote you to the next level, where the cars and the logs speed up.

This is the code that Tani used to check if the frog had safely crossed the river:

	
	IF frogger.y > 270 THEN
		level = nextLevel(level)
		carSpeed = level * 2
		logSpeed = level * 1.5
	END IF
	
Do you see why the logs don't move quite as fast as the cars?

You can download the file for this game and play it yourself, if you like. As with Pacman above, the game works only on a PC. Double-click on the file name and hit Alt-Enter to maximize the window.

Snake by Jesse

Jesse B. developed a program for playing the game of Snake. That zig zag thing in the screen shot at left is the snake, and in this game it is trying to eat food that appears in a random location on the screen. Each time the player successfully manuevers the snake to the food's location, the snake becomes one block longer. However, if the player runs the snake into a wall, it's curtains for that round! This is some of the code that Jesse used to determine if the snake had hit a wall:

IF snakeHead.x <= 9 OR snakeHead.x >= 381 THEN
	deadSnake = 1
	EXIT SUB
END IF
	

This code statement checks to see if the snake hits a wall on the left or right side of the screen. Can you guess what the code might look like to determine if the snake had hit a wall on the top or bottom of the screen?

One of the most challenging aspects of writing this program was figuring out how to store all of the necessary information about the snake. If the head of the snake makes a left turn at a certain location, then each block of the snake will also turn left when it reaches that same location. That's how you end up with a zigzag shape--by making lots of turns. But how does the computer know where all of those turns are supposed to happen? Figuring out how to structure data is one of the most important tasks in writing a computer program.

You can download the file for this game and play it yourself, if you like. It's not perfect but it's still fun to play. I have seen some students achieve very long snakes before they ever hit a wall. As in the case of Frogger and Pacman above, the game runs only on a PC.