贪吃蛇游戏——C语言双向链表实现
采用了双向链表结点来模拟蛇身结点;
通过C语言光标控制函数来打印地图、蛇身和食物;
/**************************
***************************
贪吃蛇游戏
C语言数据结构
作者:Dew
时间:2019年3月23日
版本:1.0
***************************
**************************/ #include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h> #define N 30
#define TRUE 1
#define FALSE 0
#define TIME 300 #define LEFT 37
#define UP 38
#define RIGHT 39
#define DOWN 40 void initMap();
void showGame();
void initSnake();
void control(int key);
void updateSnake(int next_head_x, int next_head_y);
void gotoxy(int x, int y); //光标控制
void gameRun();
void moveUp();
void moveDown();
void moveLeft();
void moveRight();
void createFood();
void addSnakeNode(int cur_tail_x, int cur_tail_y);
void crashTest(int head_x, int head_y); struct snakeNode {
int pos_x;
int pos_y;
snakeNode* pre;
snakeNode* next;
}; struct foodNode {
int pos_x;
int pos_y;
}; int map[N][N];
int keyPress;
int key; int next_head_x, next_head_y;
int cur_tail_x, cur_tail_y; snakeNode* snake_head = (snakeNode *)malloc(sizeof(snakeNode));
snakeNode* snake_tail = (snakeNode *)malloc(sizeof(snakeNode)); foodNode* food = (foodNode *)malloc(sizeof(foodNode)); int main()
{
initSnake(); initMap(); gameRun(); return 0;
} void initMap()
{
int i, j;
for(i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
if (i == 0 || i == N - 1)
map[i][j] = 1;
else if (j == 0 || j == N - 1)
map[i][j] = 1;
else
map[i][j] = 0;
}
} void showGame()
{
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (map[i][j] == 1) {
gotoxy(j*2, i);
printf("□");
}
else if (map[i][j] == 0) {
gotoxy(j*2, i);
printf(" ");
}
}
printf("\n");
}
gotoxy(snake_head->pos_y * 2, snake_head->pos_x);
printf("●");
gotoxy(snake_tail->pos_y * 2, snake_tail->pos_x);
printf("●");
gotoxy(food->pos_y * 2, food->pos_x);
printf("◇");
} void initSnake()
{
snake_head->pos_x = 15;
snake_head->pos_y = 15;
snake_head->pre = NULL;
snake_head->next = snake_tail; snake_tail->pre = snake_head;
snake_tail->pos_x = snake_tail->pre->pos_x + 1;
snake_tail->pos_y = snake_tail->pre->pos_y;
snake_tail->next = NULL; food->pos_x = rand() % 28 + 1;
food->pos_y = rand() % 28 + 1;
} void control(int direction)
{
if (direction == UP)
{
moveUp();
}
else if (direction == DOWN)
{
moveDown();
}
else if (direction == LEFT)
{
moveLeft();
}
else if (direction == RIGHT)
{
moveRight();
} } void updateSnake(int next_head_x, int next_head_y)
{
snakeNode* p = snake_tail;
while (p != snake_head)
{
p->pos_x = p->pre->pos_x;
p->pos_y = p->pre->pos_y;
p = p->pre;
}
p->pos_x = next_head_x;
p->pos_y = next_head_y;
} void gotoxy(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
} void gameRun()
{
showGame(); control(UP); while (TRUE)
{
if (GetAsyncKeyState(VK_RIGHT))
{
control(RIGHT);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_LEFT))
{
control(LEFT);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_UP))
{
control(UP);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_DOWN))
{
control(DOWN);
Sleep(TIME);
}
}
} void moveUp()
{
while (TRUE)
{
next_head_x = snake_head->pos_x - 1;
next_head_y = snake_head->pos_y;
cur_tail_x = snake_tail->pos_x;
cur_tail_y = snake_tail->pos_y;
updateSnake(next_head_x, next_head_y);
gotoxy(next_head_y * 2, next_head_x);
printf("●");
crashTest(next_head_x, next_head_y);
if (next_head_x == food->pos_x && next_head_y == food->pos_y)
{
addSnakeNode(cur_tail_x, cur_tail_y);
createFood();
}
else
{
gotoxy(cur_tail_y * 2, cur_tail_x);
printf(" ");
} Sleep(TIME); if (GetAsyncKeyState(VK_RIGHT))
{
control(RIGHT);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_LEFT))
{
control(LEFT);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_UP))
{
control(UP);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_DOWN))
{
continue;
} }
} void moveDown()
{
while (TRUE)
{
next_head_x = snake_head->pos_x + 1;
next_head_y = snake_head->pos_y;
cur_tail_x = snake_tail->pos_x;
cur_tail_y = snake_tail->pos_y;
updateSnake(next_head_x, next_head_y);
gotoxy(next_head_y * 2, next_head_x);
printf("●");
crashTest(next_head_x, next_head_y);
if (next_head_x == food->pos_x && next_head_y == food->pos_y)
{
addSnakeNode(cur_tail_x, cur_tail_y);
createFood();
}
else
{
gotoxy(cur_tail_y * 2, cur_tail_x);
printf(" ");
} Sleep(TIME);
if (GetAsyncKeyState(VK_RIGHT))
{
control(RIGHT);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_LEFT))
{
control(LEFT);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_UP))
{
continue;
}
else if (GetAsyncKeyState(VK_DOWN))
{
control(DOWN);
Sleep(TIME);
} }
} void moveLeft()
{
while (TRUE)
{
next_head_x = snake_head->pos_x;
next_head_y = snake_head->pos_y - 1;
cur_tail_x = snake_tail->pos_x;
cur_tail_y = snake_tail->pos_y;
updateSnake(next_head_x, next_head_y);
gotoxy(next_head_y * 2, next_head_x);
printf("●");
crashTest(next_head_x, next_head_y);
if (next_head_x == food->pos_x && next_head_y == food->pos_y)
{
addSnakeNode(cur_tail_x, cur_tail_y);
createFood();
}
else
{
gotoxy(cur_tail_y * 2, cur_tail_x);
printf(" ");
} Sleep(TIME);
if (GetAsyncKeyState(VK_RIGHT))
{
continue;
}
else if (GetAsyncKeyState(VK_LEFT))
{
control(LEFT);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_UP))
{
control(UP);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_DOWN))
{
control(DOWN);
Sleep(TIME);
} }
} void moveRight()
{
while (TRUE)
{
next_head_x = snake_head->pos_x;
next_head_y = snake_head->pos_y + 1;
cur_tail_x = snake_tail->pos_x;
cur_tail_y = snake_tail->pos_y;
updateSnake(next_head_x, next_head_y);
gotoxy(next_head_y * 2, next_head_x);
printf("●");
crashTest(next_head_x, next_head_y);
if (next_head_x == food->pos_x && next_head_y == food->pos_y)
{
addSnakeNode(cur_tail_x, cur_tail_y);
createFood();
}
else
{
gotoxy(cur_tail_y * 2, cur_tail_x);
printf(" ");
} Sleep(TIME);
if (GetAsyncKeyState(VK_RIGHT))
{
control(RIGHT);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_LEFT))
{
continue;
}
else if (GetAsyncKeyState(VK_UP))
{
control(UP);
Sleep(TIME);
}
else if (GetAsyncKeyState(VK_DOWN))
{
control(DOWN);
Sleep(TIME);
} }
} void createFood()
{
food->pos_x = rand() % 28 + 1;
food->pos_y = rand() % 28 + 1;
gotoxy(food->pos_y * 2, food->pos_x);
printf("◇");
} void addSnakeNode(int cur_tail_x, int cur_tail_y)
{
snakeNode* tempNode = (snakeNode *)malloc(sizeof(snakeNode));
tempNode->pos_x = cur_tail_x;
tempNode->pos_y = cur_tail_y;
tempNode->next = NULL;
snake_tail->next = tempNode;
tempNode->pre = snake_tail;
snake_tail = tempNode;
} void crashTest(int head_x, int head_y)
{
snakeNode* p = snake_head->next;
while (p != NULL)
{
if (p->pos_x == head_x && p->pos_y == head_y)
{
gotoxy(32 * 2, 15);
printf("...发生撞击!......游戏结束...");
getchar();
}
if (p->pos_x == 1 || p->pos_x == N - 2 || p->pos_y == 1 || p->pos_y == N - 2)
{
gotoxy(32 * 2, 15);
printf("...发生撞击!......游戏结束...");
getchar();
}
p = p->next;
}
}
贪吃蛇游戏——C语言双向链表实现的更多相关文章
- 基于EasyX库的贪吃蛇游戏——C语言实现
接触编程有段时间了,一直想学习怎么去写个游戏来练练手.在看了B站上的教学终于可以自己试试怎么实现贪吃蛇这个游戏了.好了,废话不多说,我们来看看如何用EasyX库来实现贪吃蛇. 一.准备 工具vc++6 ...
- 贪吃蛇游戏C语言源代码学习
源代码下载地址为:www.clang.cc 阅读学习了源代码,并做了简单的注释和修改,里面只用了链表数据结构,非常适合C语言入门者学习阅读. 程序可在VS2013下编译运行. #include< ...
- 【C语言项目】贪吃蛇游戏(上)
目录 00. 目录 01. 开发背景 02. 功能介绍 03. 欢迎界面设计 3.1 常用终端控制函数 3.2 设置文本颜色函数 3.3 设置光标位置函数 3.4 绘制字符画(蛇) 3.5 欢迎界面函 ...
- 贪吃蛇游戏(printf输出C语言版本)
这一次我们应用printf输出实现一个经典的小游戏—贪吃蛇,主要难点是小蛇数据如何存储.如何实现转弯的效果.吃到食物后如何增加长度. 1 构造小蛇 首先,在画面中显示一条静止的小蛇.二维数组canva ...
- 使用Love2D引擎开发贪吃蛇游戏
今天来介绍博主近期捣腾的一个小游戏[贪吃蛇],贪吃蛇这个游戏相信大家都不会感到陌生吧.今天博主将通过Love2D这款游戏引擎来为大家实现一个简单的贪吃蛇游戏,在本篇文章其中我们将会涉及到贪吃蛇的基本算 ...
- Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录
一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...
- 用C++实现的贪吃蛇游戏
我是一个C++初学者,控制台实现了一个贪吃蛇游戏. 代码如下: //"贪吃蛇游戏"V1.0 //李国良于2016年12月29日编写完成 #include <iostream& ...
- WebGL实现HTML5的3D贪吃蛇游戏
js1k.com收集了小于1k的javascript小例子,里面有很多很炫很酷的游戏和特效,今年规则又增加了新花样,传统的classic类型基础上又增加了WebGL类型,以及允许增加到2K的++类型, ...
- 100行JS实现HTML5的3D贪吃蛇游戏
js1k.com收集了小于1k的javascript小例子,里面有很多很炫很酷的游戏和特效,今年规则又增加了新花样,传统的classic类型基础上又增加了WebGL类型,以及允许增加到2K的++类型, ...
随机推荐
- Python:time模块/random模块/os模块/sys模块
time 模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 python中时间日期格式化符号: %y 两位数的 ...
- Python学习案例之Web版语音合成播报
前言 语音合成技术能将用户输入的文字,转换成流畅自然的语音输出,并且可以支持语速.音调.音量设置,打破传统文字式人机交互的方式,让人机沟通更自然. 应用场景 将游戏场景中的公告.任务或派单信息通过语音 ...
- 序言 - PHP零基础快速入门
我为什么要写<PHP零基础快速入门>? 原因: PHP 真心简单,适合零基础的人快速入门掌握,身边的人学习一两周上手开发的比比皆是: 市面上的文章或书籍对初学者并不友好,多半枯燥乏味,我相 ...
- java易混淆知识小结
1.java的基本数据类型,及所占字节和范围 byte: 字节型,占1个字节,8位,范围是 -2^7 ~ 2^7-1 short:短整型,占2个字节,16位,范围是 -2^15 ~ 2^15 ...
- MySQL数据库8.0.15 安装教程
第一步:安装MySQL服务 这里下载完成的是一个压缩文件,直接将里面的‘mysql-8.0.15-winx64'文件夹解压到你想要安装的路径即可,我是直接安装在C盘的. 解压完后的文件路径如下图: 在 ...
- [洛谷P1650] 田忌赛马
贪心难题:总结贪心问题的一般思路 传送门:$>here<$ 题意 田忌和齐王各有n匹马,赛马时一一对应.赢+200,输-200,平+0. 问最多多少钱? 数据范围:$n \leq 2000 ...
- Educational Codeforces Round 62 (Rated for Div. 2)C
题目链接 :C. Playlist #include<bits/stdc++.h> using namespace std; #define maxn 300005 #define LL ...
- termux
使用http服务,链接原服务器要挂vpn. apt edit-sources 如果提示 $ apt edit-sources e: Sub-process editor returned a n er ...
- 如何隐藏overflow: scroll的滚动条
css3有一个直接调用的css,保证隐藏滚动条的同时还可以继续通过滚轮向下翻 ::-webkit-scrollbar { /*隐藏滚轮*/ display: none; } 但是仅限于支持css3的浏 ...
- mysql函数调用过程
1.conn = mysql_init(NULL);//初始化 MYSQL *conn; 2.mysql_real_connect(conn, "localhost", &quo ...