#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#define up 'w'
#define down 's'
#define left 'a'
#define right 'd'
HANDLE hOut;
void welcome(); //欢迎界面
void Finish(); //结束界面
void creattu(); //方格
void gotoxy(int x,int y);
void gotoprint(int x, int y);
void gotodelete(int x, int y);
void creatfood(); //食物小方格
int keyboard(); //输入 w s a d
int Judge(); //判断是否结束
void Movebody(); //身体移动
void Eat(); //吃后增加一个方格
void newbody(int x,int y); //打印新身体
int main() {
system("color 0A");
welcome();
creattu();
creatfood();
if (keyboard() == 0) return 0;
return 0;
}
void gotoxy(int x, int y) {
COORD pos;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOutput, pos);
}
char name[21];
int score = 0;
char click;
int speed;
void welcome() {
gotoxy(15, 10);
printf("/-------------------------------------------/");
gotoxy(15, 25);
printf("/-------------------------------------------/");
gotoxy(15, 13);
printf(" welcome to 贪吃蛇");
gotoxy(20, 18);
printf("Please enter your name:\n");
gotoxy(27, 21);
gets(name);
system("cls");
}
typedef struct Snakes {
int x;
int y;
struct Snakes* next;
}snake;
snake* head;
struct Food {
int x;
int y;
}food;
void gotoprint(int x, int y) {
gotoxy(x, y);
printf("■");
}
void gotodelete(int x, int y) {
gotoxy(x, y);
printf(" ");
}
void creattu() {
for (int i = 0; i < 58; i += 2) {
gotoprint(i, 0);
gotoprint(i, 26);
}
for (int j = 1; j < 26; j++) {
gotoprint(0, j);
gotoprint(56, j);
}
gotoxy(63, 10);
printf("Hello %s,欢迎来到贪吃蛇", name);
gotoxy(63, 15);
printf("你的分数是: %d ", score);
gotoxy(63, 20);
printf("This game was created by X,F,H");
head = (snake*)malloc(sizeof(snake));
snake* p = (snake*)malloc(sizeof(snake));
snake* q = (snake*)malloc(sizeof(snake));
head->x = 16;
head->y = 15;
p->x = 16;
p->y = 16;
q->x = 16;
q->y = 17;
head->next = p;
p->next = q;
q->next = NULL;
}
void creatfood() {
int flag = 0;
while (!flag) {
flag = 1;
srand((int)time(NULL));
food.y = rand() % 25+1;
food.x = rand() % 54+2;
if (food.x % 2 != 0) {
food.x = food.x + 1;
}
snake* judge = head;
while(1) {
if (judge->next == NULL) break;
if (food.x == judge->x && food.y == judge->y) flag = 0;
judge = judge->next;
}
}
gotoxy(food.x, food.y);
printf("■");
}
void Finish() {
system("cls");
gotoxy(15, 10);
printf("/------------------------------------------------/");
gotoxy(15, 20);
printf("/------------------------------------------------/");
gotoxy(18, 14);
printf("Game over!");
gotoxy(20, 16);
printf("你的分数是 : %d ", score);
gotoxy(18, 18);
printf("还不错哦!");
system("pause");
}
int Judge() { //判断是否结束
if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26) {
Finish();
return 0;
}
snake* p = head->next;
while (1) {
if (p == NULL) break;
if (head->x == p->x && head->y == p->y) {
Finish();
return 0;
}
p = p->next;
}
return 1;
}
void newbody(int x, int y) { //吃一个食物,更新身体
snake* p = head;
while (p->next->next != NULL) {
p = p->next;
}
free(p->next);
p->next = NULL;
snake* new_head = (snake*)malloc(sizeof(snake));
new_head->x = x;
new_head->y = y;
new_head->next = head;
head = new_head;
}
int keyboard() {
//char c;
while (1) {
if (Judge() == 0) return 0;
if (_kbhit()) { //_kbhit() ,_getch()
click = _getch();
}
Movebody();
Eat();
}
return 1;
}
void Movebody() {
int x = head->x;
int y = head->y;
snake* p = head;
while (p->next != NULL) {
p = p->next;
}
gotodelete(p->x, p->y);
switch (click) {
case up:
y = y - 1; break;
case down:
y = y + 1; break;
case left:
x = x - 2; break;
case right:
x = x + 2; break;
default:
break;
}
if (x != head->x || y != head->y) {
newbody(x, y);
}
p = head;
gotoprint(p->x, p->y);
int count = score / 10;
if (count <= 10) speed = 200;
else if (count > 10 && count <= 20) speed = 100;
else if (count > 20 && count <= 40) speed = 50;
else speed = 10;
Sleep(speed);
}
void Eat() {
if (head->x == food.x && head->y == food.y) {
creatfood();
snake* _new = (snake*)malloc(sizeof(snake));
snake* p;
p = head;
while (1) {
if (p->next == NULL)break;
p = p->next;
}
p->next = _new;
_new->next = NULL;
score += 10;
gotoxy(77, 15);
printf(" %d ", score);
}
}
贪吃蛇(C语言)
原创
©著作权归作者所有:来自51CTO博客作者别理我我很的原创作品,请联系作者获取转载授权,否则将追究法律责任
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
[C语言] 贪吃蛇
贪吃蛇小游戏
ide #include 初始化 i++ #define -
贪吃蛇程序代码python报告
50行Python代码实现经典游戏,不仅是划水神器,更是学习利器!前言:Free Python Games非常适合学生,它不仅具有高度的组织性和灵活性,而且能够激发人们探索和理解能力。Free Python Games在轻松的环境中把游戏和学习结合在一起,从而减轻了编程过程中的压力。贪吃蛇、迷宫、吃豆人、扫雷、Flappy Bird...这些游戏可以是非常经典,甚至伴随着很多人的童年回忆。那么,你
贪吃蛇程序代码python报告 bat贪吃蛇游戏代码 采用python类来实现五子棋 Python 贪吃蛇 -
java hbase 存储时间排序
【概述】几个相关类与HBase数据模型之间的对应关系java类 HBase数据模型 HBaseConfiguration、HBaseAdmin 数据库(DataBase) HTable 表(Table) HTableDescriptor 列族(
java hbase 存储时间排序 hbase 属性值 时间戳 修饰符