Snake Game-贪吃蛇

来源:互联网 发布:数据挖掘神经网络算法 编辑:程序博客网 时间:2024/04/30 00:38

原文网址:http://www.simonhuggins.com/courses/cbasics/course_notes/snake.htm

 

Introduction

The following is an example game written in C based on the game called 'snake' which has been around since the earliest days of home computing (I can remember writing a version of it for my ZX81), and has re-emerged in recent years on mobile phones.

It isn't the world's greatest game, but it does give you an idea of what you can achieve with a relatively simple C program, and perhaps the basis by which to extend the principles and create more interesting games of your own.

Playing the game

You can download the executable to try out the game without having to compile it if you wish. Download it by clicking here - it is about 123k in length.

Note that on faster PCs, it will be unplayably fast, so you will need to re-compile it  with the pause_length constant set to a higher value. My PC is about 350MHz and it is OK.

To move the snake, use 'a' for up, 'z' for down, 'o' for left and 'p' for right. Again, there are constants you can change if you want to alter these settings. Press 'x' to exit the game at any time.

The aim of the game is to collect the dots (food) and avoid the obstacles (crosses, borders, and the snake itself). As you collect food, the snake gets longer, so increasing your likelihood of crashing into yourself. When you have collected enough food, you progress onto the next level, where your snake gets longer, and the amount of food to collect to progress through the level gets larger.

You get scored according to the length of the snake and the number of 'x' obstacles on the screen.

The speed increases every 5 levels.

You get a bonus when you complete the level of 1000, increasing by 1000 each level (e.g. complete level 5, you get a 5000 bonus).

There is no concept of lives. Once you hit an obstacle, that's it, game over.

Make sure you do not have the caps lock on, otherwise the keys will fail to respond.

The Listing

This is the C program for the game. it is reasonably well structured, but by no means perfect.  Note that it uses library functions that are not available on all C compilers - it was designed using Borland C++ Builder / Turbo C.  Also it uses a few C++ concepts not available in standard C - e.g. const for a constant (you would use #define in normal C). You may also have to change your compiler settings if you have them set to ANSI C to C++ instead - the for loops define a local instance of i as a loop counter, a concept to available in standard C.

 

原创粉丝点击