最近整理下电脑,看到了自己在上个学期打的贪吃蛇游戏的c代码,觉得真的是略微有点冗长,但是实现起来应该也算是比较好理解,于是把自己的代码搬上来,网络上写贪吃蛇的c语言的文章很多,我这篇也仅是给大家作为一个参考而已。

我的代码是在Windows下运行的,因为需要用到windows.h这个库。

然后也做了一个简单的ai模式,这在没有障碍物的情况下前期还是蛮不错的,但是到了后期蛇变长了之后就会有bug了。

好了,直接上代码吧:

1)头文件和宏定义

#include

#include

#include

#include

#include

#define SNAKE_MAX_LENGTH 20

#define SNAKE_HEAD 'H'

#define SNAKE_BODY 'X'

#define BLANK_CELL ''

#define SNAKE_FOOD '$'

#define WALL_CELL '*'

2)各种实现函数的声明

/*snake stepping: dy = -1(up) 1(dowm) 0(no move); dx = -1(left), 1(right), 0(no move)*/

void snakemove(int, int);

//to write dowm the current location of the snake

void put_money(void);

void output(void);

// to put the current map on the screen

void initial_the_game(void);

void put_accelerate(void);

// @ is a special food which can speed up your snake.

int judge(int, int);

/* when it comes to ai, it is used to judge whether the next step is movable. */

int dis(int, int);

// when it coomes to ai, it is used to calculate the current distence //between the snake head and the food.

void welcome(void); // the game introduction.

void gameover(void);

void edition_handed(void);

// the edition in which you can play by yourself.

void edition_presentation(void);

// the edition in which the snake can go automatically.

3)各种全局变量

// define vars for snake,notice name of vars in c

int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};

int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};

int snakeLength = 5;

int gamestate = 1;

int current_speed = 600;

int score = 0;

char edition_choose; // for player to choose the edition.

int con = 1; // to judge the initial state of the game;

int energy = 0; // write down the condition to accelerate by eating $.

//the following part is to realize the simple ai .

const char movable[4] = {'a', 'd', 's', 'w'};

int distance[4] = {9999, 9999, 9999, 9999};

int fx = 6, fy = 6; // the coordinate of the food $

4)地图

char map[12][12] =

{"************",

"*XXXXH *",

"* *",

"* *",

"* *",

"* *",

"* $ *",

"* *",

"* @ *",

"* *",

"* *",

"************"};

5)主函数(可选模式)

int main() {

while (con) {

welcome();

int flag = 1;

while (flag) {

edition_choose = getch(); //choose the edition

if (edition_choose == 'h') {

edition_handed();

flag = 0;

}

else if (edition_choose == 'p') {

edition_presentation();

flag = 0;

}

else {

printf("Please press the correct bottom -,- ...\n");

}

}

gameover();

}

return 0;

}

6)手动模式实现

void edition_handed(void) {

system("cls");

output();

char ch = 'd';

while (gamestate) {

switch (ch) {

case 'a': // go left

while (1) {

snakemove(-1, 0);

Sleep(current_speed);

if (gamestate == 0) // to break the loop if the snake hit the wall or itself.

break;

if (kbhit() != 0) { // to change the direction

ch = getch();

if (ch == 's' || ch == 'w')

break;

else

ch = 'a';

}

}

break;

case 'd': // go right

while (1) {

snakemove(1, 0);

Sleep(current_speed);

if (gamestate == 0)

break;

if (kbhit() != 0)

ch = getch();

if (ch == 's' || ch == 'w')

break;

else

ch = 'd';

}

break;

case 's': // go down

while (1) {

snakemove(0, 1);

Sleep(current_speed);

if (gamestate == 0)

break;

if (kbhit() != 0)

ch = getch();

if (ch == 'a' || ch == 'd')

break;

else

ch = 's';

}

break;

case 'w': // go up

while (1) {

snakemove(0, -1);

Sleep(current_speed);

if (gamestate == 0)

break;

if (kbhit() != 0)

ch = getch();

if (ch == 'a' || ch == 'd')

break;

else

ch = 'w';

}

break;

}

if (gamestate == 0)

break;

}

return;

}

7)自动模式实现

void edition_presentation(void) { // for ai

system("cls");

int i, min = 10000;

output();

char ch;

char quit = 'o';

int k;

while (gamestate) { // find the shortest way;

min = 10000;

if (judge(-1, 0)) distance[0] = dis(-1, 0);

if (judge(1, 0)) distance[1] = dis(1, 0);

if (judge(0, 1)) distance[2] = dis(0, 1);

if (judge(0, -1)) distance[3] = dis(0, -1);

for (i = 0; i < 4; i++) {

if (min >= distance[i]) {

min = distance[i];

k = i;

}

}

Sleep(current_speed);

switch (movable[k]) {

case 'a': // go left

snakemove(-1, 0);

break;

case 'd': // go right

snakemove(1, 0);

break;

case 's': // go down

snakemove(0, 1);

break;

case 'w': // go up

snakemove(0, -1);

break;

}

if (gamestate == 0)

break;

system("cls");

output();

}

return;

}

8)其他辅助函数

欢迎界面

void welcome(void) { // just for some introduction

printf("WELCOME TO THE SNAKE'S WORLD !!!!\n");

printf("\n");

printf("\n");

printf("Please choose the edition you want.\n");

printf("\n");

printf("\n");

printf("The 'h' is for the hand-operated and the 'p' is for the simple presentation\n");

printf("\n");

printf("\n");

printf("Attention: the presentation still has a liitle bug, while it can be moving right for a period of time...\n ");

return;

}

游戏结束界面

void gameover(void) { // give you some introduction when you lose the game.

system("cls");

printf("Game over!!!\n");

printf("Do you want to continue? y or n\n");

char start; // in order to judge whether you still want to play the game.

while (1) {

start = getch();

if (start == 'y') {

system("cls");

initial_the_game();

break;

} else if (start == 'n') {

system("cls");

con = 0; // in order to let the game end.

printf("See you next time! ^-^\n");

break;

} else {

printf("Please press the correct buttom.\n");

}

}

}

图像实现方式

void output(void) { // put the cuttent game map.

printf("THE INTERESTING SNAKE GAME CREATED BY LONGJ =,=\n");

printf("use w~s~a~d to control the snake's movement\n");

printf("ATTENTION: the @ can speed up your lovely snake~~\n");

int i, j;

for (i = 0; i < 12; i++) {

for (j = 0; j < 12; j++) {

printf("%c", map[i][j]);

if (j == 11)

printf("\n");

}

}

printf("Your current_speed is %d\n", current_speed);

printf("The number of your food undigested is %d (when it comes to 5, your speed will be accelerated!) \n", energy);

printf("SCORE = %d\n", score);

return;

}

蛇的行走实现

void snakemove(int dx, int dy) { // all the conditions are comparing the head and the next position.

int i;

if (snakeY[snakeLength - 1] + dy == snakeY[snakeLength - 2] && snakeX[snakeLength - 1] + dx == snakeX[snakeLength - 2])

return; // to prevent it go to itslef.

if (map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == 'X') {

gamestate = 0;

return;

}

if (map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == '*') {

gamestate = 0;

return;

}

if (map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == ' '

|| map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == '@') {

map[snakeY[0]][snakeX[0]] = ' '; // clear the former_tail

if (map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == '@'

&& current_speed > 100) { // what will happen when your snake eats the @

current_speed -= 100;

put_accelerate();

}

for (i = 0; i < snakeLength - 1; ++i) {

snakeX[i] = snakeX[i + 1];

snakeY[i] = snakeY[i + 1];

}

snakeX[snakeLength - 1] += dx;

snakeY[snakeLength - 1] += dy;

for (i = 0; i < snakeLength - 1; i++) // write down the current snake location

map[snakeY[i]][snakeX[i]] = 'X';

map[snakeY[snakeLength - 1]][snakeX[snakeLength - 1]] = 'H';

}

if (map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == '$') {

map[snakeY[snakeLength - 1]][snakeX[snakeLength - 1]] = 'X';

snakeLength++;

snakeX[snakeLength - 1] = snakeX[snakeLength - 2] + dx;

snakeY[snakeLength - 1] = snakeY[snakeLength - 2] + dy;

map[snakeY[snakeLength - 1]][snakeX[snakeLength - 1]] = 'H';

score++;

energy++;

if (energy == 5 && current_speed > 50) {

current_speed -= 50;

energy = 0;

}

put_money();

}

system("cls");

output();

return;

}

食物放置的实现

void put_money(void) { /// ai will change the code

int flag = 1;

while (flag) {

srand(time(NULL));

fx = rand() % 12;

fy = rand() % 12;

if (map[fy][fx] == ' ') {

map[fy][fx] = '$';

flag = 0;

}

if (edition_choose == 'p') {

int i;

for (i = 0; i< 4; i++)

distance[i] = 9999;

}

}

return;

}

void put_accelerate(void) {

int x, y, flag = 1;

while (flag) {

srand(time(NULL));

x = rand() % 12;

y = rand() % 12;

if (map[x][y] == ' ') {

map[x][y] = '@';

flag = 0;

}

}

return;

}

ai辅助函数

int dis(int dx, int dy) {

return abs(fx - snakeX[snakeLength - 1] - dx) + abs(fy - snakeY[snakeLength - 1] - dy);

}

int judge(int dx, int dy) {

if (map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == ' ')

return 1;

else if (map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == '$')

return 1;

else if (map[snakeY[snakeLength - 1] + dy][snakeX[snakeLength - 1] + dx] == '@')

return 1;

else

return 0;

}

游戏over后的初始化函数:

void initial_the_game(void) {

int i, j, count = 1;

snakeLength = 5;

gamestate = 1;

fx = fy = 6;

current_speed = 600;

for (i = 0; i < 5; i++)

snakeY[i] = 1;

for (i = 0; i < 5; i++) {

snakeX[i] = count++;

}

for (j = 0; j < 12; j++) {

map[0][j] = '*';

map[11][j] = '*';

}

for (i = 1; i < 11; i++) {

map[i][0] = map[i][11] = '*';

for (j = 1; j < 11; j++)

map[i][j] = ' ';

}

map[fy][fx] ='$';

map[8][4] = '@';

for (i = 0; i< 4; i++) {

distance[i] = 9999;

}

for (i = 0; i < 4; i++)

map[snakeY[i]][snakeX[i]] = 'X';

map[snakeY[4]][snakeX[4]] = 'H';

return;

}

小结:

不难看出,c语言代码比较冗长,而且初始化的函数实现起来十分麻烦,稍有不慎就会全部出错,导致游戏无法持续玩下去,博主当初写的时候就是被坑了很久=_=

因而现在在学c++,希望以后把类的概念之类的东西都弄得更加熟练之后,可以去把这个冗长的c代码改成更加简洁,阅读性更强的c++代码。

(好的,已经更新了,写出了一个比较简单的C++贪吃蛇,的确是思路清晰很多,代码的可读性更高。)

这篇博客给那些想要用c来写贪吃蛇的同学一些参考,运行起来是没有问题的,可以选择性看:)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

C语言贪吃蛇大作业总结,C语言实现贪吃蛇游戏相关推荐

  1. c语言贪吃蛇大作业报告,C语言贪吃蛇实验报告

    C语言贪吃蛇实验报告 C 语言程序设计实训报告姓 名 专 业 班 级 指导教师 二 011 年 7 月 14 日I I目录1 实训目的和要求 11.1 实训目的和任务 11.2 实训要求 12 实训任 ...

  2. C语言贪吃蛇大作业总结,c语言贪吃蛇实训报告.doc

    c语言贪吃蛇实训报告 c语言贪吃蛇实训报告 C语言贪吃蛇实验报告 C语言程序设计实训报告 姓 名专 业班 级指导教师 二011年 7 月 14 日 1 1.1 1.2 目录 实训目的和要求 ..... ...

  3. c语言课程设计大作业模版,c语言课程设计报告模板下载

    c语言课程设计报告模板是一款专业的设计模板,对于在大学有需要些设计报告的朋友,可以下载这款模板作为参考,了解设计报告的需求,基本上毕业的论文设计也是在这个基础上严格要求的,欢迎下载使用. C语言介绍 ...

  4. c语言实验报告大作业答案,C语言实验报告摘要(共6篇)

    C语言实验报告摘要(共6篇) 第一部分: C语言实验经验 C语言实验经验 随着科学技术的飞速发展,计算机在人们中的作用越来越突出. C语言作为一种计算机语言,对其进行学习将有助于我们更好地理解计算机并 ...

  5. 0039c语言作业答案2020,西南大学2019年网络与继续教育[0039]《C语言程序设计》大作业试题(资料).doc...

    西南大学2019年网络与继续教育[0039]<C语言程序设计>大作业试题(资料).doc 文档编号:764150 文档页数:4 上传时间: 2019-10-12 文档级别: 文档类型:do ...

  6. c语言程序设计形成性作业3,C语言程序设计形成性作业3-4..doc

    C语言程序设计形成性作业3-4. 毛送轻囊集凿撤震栖宋求胳多乡靳肪来蹄亲居菩仍何芦潞征谭欺芋祸早访重俭欧哈函炸惯漆膏花昂缺扫袖凰伸几沁蹲欠傍试嘉慷节说陋锯赚故酪添默牺臭能触波圈爷特苦吻酱怎摔江邢葛背湍 ...

  7. unity3D期末大作业,酷炫喷气飞机汽车游戏

    unity3D期末大作业,酷炫喷气飞机汽车游戏 本游戏有酷炫的喷气式飞机飞跃障碍物飞行,汽车各种难度的行驶,鼠标控制人物运动,详细情况如下动态图所示(资源下载链接在文章末尾) 资源下载链接: http ...

  8. 贪吃蛇大作战:C语言代码

    代码: 代码有点长, #include<stdio.h> #include<Windows.h> #include<time.h> #include<stdl ...

  9. 有输入和输出的c语言大作业题目,C语言大作业题目2011.pdf

    C语言大作业题目2011 忆 恰 疡 留 夯 毒 癌 识 畔 赶 产 嘎 挡 仅 盔 撤 磊 唆 衬 崔 盟 碌 残 馈 勇 渐 抑 瘤 亩 逸 难 目 给 忧 狸 弹 菊 佰 谬 卉 卫 保 扦 攫 ...

  10. c语言程序设计0039大作业答案,2019西南大学0039C语言程序设计机考大作业答案.doc...

    - PAGE 1 - 西南大学网络与继续教育学院课程考试试题卷 类别: 网教 2019年 6月 课程名称[编号]: C语言程序设计 [0039] A卷 大作业 满分:100 分 一.大作业题目 1.简 ...

最新文章

  1. 甲骨文否认将收购IT咨询巨头埃森哲 称从未考虑过
  2. 用Kotlin写Android Gradle脚本
  3. ITK:查找图像的更高导数
  4. javascript 定义类(转载)
  5. C++ STL中的Hashmap
  6. db2 某个字段排序_MySQL、Oracle、DB2等数据库常规排序、自定义排序和按中文拼音字母排序...
  7. leetcode51. N皇后
  8. 链式延迟执行DOME
  9. 计算机日常英语,计算机英语的常用句子
  10. 关于background的一些知识
  11. 诺基亚:丑小鸭的重生
  12. 3dm java32位_3DM游戏运行库合集安装包v2.3
  13. java转盘抽奖_JAVA用户抽奖系统设计(幸运大转盘作业)
  14. winform打印html文件,c# 如何实现web打印插件
  15. 一个时代的印记:还记得那些年我们逃课去的网吧
  16. 【华为机试真题 C++】一种字符串压缩表示的解压-100
  17. 中药复方在治疗慢性盆腔炎上的应用
  18. 关于vue项目中的 日志管理功能
  19. 解决android repo (git出错的问题)
  20. 计算机ps特效教程,计算机一级photoshop给照片制作半素描效果教程

热门文章

  1. matlab进行下采样
  2. Windows如何查看.db数据库文件
  3. java 三大特性_java的三大特性是什么?
  4. 硬件基础知识(10)---元器件选型规范大全
  5. element-ui 下拉框实现拼音搜索
  6. 三角函数与反三角函数图像
  7. 转载-史密斯(smith)圆图讲解-基础内容
  8. 2016年统计用区划代码和城乡划分代码(截止2016年07月31日) 省市县镇+url
  9. 关于定时任务的时间表达式
  10. m语言常用函数和命令