Snake Game-贪吃蛇

原文网址: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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值