1. Technology

Game Programming in C Tutorial Six - Super Snake

Super snake Arises!

By

High Score of Snake

This is a modified version of Snake which adds the following features:

  • Demo Mode. It plays the game for a few seconds.
  • High score Table. This is shown in Demo Mode as well.
  • Fly away scores. After every fruit is eaten, a 50 score flies away up the screen.

If you just found this read about the Snake Tutorial first.

  • Download the Supersnake.c (txt)
  • Download a zip file of the compiled exe, all graphics plus needed SDL dlls.

Unzip the supersnake.zip file into a folder and just run the exe. This is for Windows only.

Changes from Snake

If you have saved the snake.c code already you might find it useful to download and run the SourceGear DiffMerge utility or use a diff utility of your own to compare the files.

SuperSnake was developed from Snake.c, so comparing the two file lets you see the differences quickly. There are about 30 changes and the new file is 959 lines long, compared to the original snake's 649 lines; almost 50% longer. There are two new graphics scroll.gif and 50.gif. Scroll provides the backdrop for the high score table and 50.gif is the letters 50 saved out. This is used as a sprite to display the fly away scores.

Restructuring the Game loop

To accomodate the demo mode and the high score table, the game loop structure had to be altered. Now there's an outer loop which switches from Demo Mode/High Score to game play when the game starts. I split InitGame into InitSetup which handles the general variables and InitGame which also initializes the game variables and calls InitSprites().

The fly away scores are implemented as sprites, a general term for free moving objects. In theory ten could be on screen at once but in poractice having more than one is unusual and would only happen if there were several fruits next to each other eaten by the snake.

The main function now has the outer loop which is also the game mode loop. This switches every 15 seconds from demo mode to high score mode then back. However the demo mode snake control logic is not very good and the snake usually kills itself within 1-5 seconds. That's done in the CalcSnakeDemoMoves() wjhich is passed a single int parameter, 0 to setup the snake at a random length and 1 for moving it. The global inDemo flag has three states:

  • 0 - Player is playing the game.
  • 1 - Demo Play of the game.
  • 2 - Show High score table.

The GameLoop function() function does either the demo mode play and high score table or the proper game play. In demo mode, the ExitDemoCheck() function is called to see if a key has been pressed and if so it exits the demo mode.

Showing High Scores

A new struct is added for this, the hiscore which holds the value and initials. To keep it simple, only three letters are stored. These are obtained in the function GetInput() which calls GetaChar() to read each character. This is very simple, and does not display the chars when typed but just captures the first three key presses.

struct hiscore hiscores[MAXHISCORES];>br/>

The ShowHiScores(int Counter) is called repeatedly until 15 seconds have passed. It blits the Scroll surface then prints the top ten initials. The counter parameter has a little ripple go slowly through the table blanking one line at a time, making it slightly more interesting. There is a slight bug in that I originally had the demo mode/high score time as ten seconds and the value of each second is passed in as the parameter to control the ripple. Having changed it to 15 seconds, there's no ripple for the last 5 seconds.

Moving the Snake in Demo Mode

As said, the snake is set to a random length (by just pretending it's eaten food and stretching it). It has a 2/3rds chance of carrying on in the same direction and 1/6th of turning left or right. When the direction changes, it is conditioned to limit it to the value 0-3.

Fly Away Scores and Sprites

I've seen arcade games where when a score winning action is done, displays the value of the score as a sprite, ie freely moving object and then slowly moves it away. That's what this does but to make it more useful, I implemented it via three functions.

  • AddSprite(int loc)
  • MoveSprites()
  • DrawSprites()

When a piece of fruit is eaten, snake[headindex] is passed in as the loc to AddSprite() so the first position the score appears is at the snake's head, where the fruit was. AddSprite() checks that there are spare places in the sprites[] array then sets a sprite there with a countdown of 300. Each time round the gameloop countdown is decremented and MoveSprites() moves the sprite every 15th times that countdown is decremented by the velocity specified in vx and vy. Finally DrawSprites() draws all sprites with countdown > 0.

Adding New Features

It's always fun to experiment. Here are some ideas you are free to add for yourself.

  1. Add special Fruit that might create new self moving snakes that only last for 30 seconds. These move randomly and destroy a trap (and kill themselves so doing). Annoyingly they can also eat your fruit!
  2. Have a mongoose appear and start chasing you. You have to lure it to hit a trap.
  3. Have a random Venus Fly Trap appear. If you move near it, it grabs and eats a segment of your snake so your snake shrinks. After a minute without eating it dies and vanishes.

Conclusion

I added these because I wanted the game to be more complete than the first version, but that's it and there are now no more Snake Tutorials in C. This will be the version implemented in the C++ and C# versions.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C Programming
  5. C Tutorials
  6. Game Programming in C Tutorial Six- Super Snake

©2014 About.com. All rights reserved.