赞
踩
晚自习摸鱼写个代码
蛇是单链表数据结构
预览如图:
代码如下:
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
#include<conio.h>
int flag, food_flag, food_x, food_y, level=300;
void Game_start();
void Welcome_game();
void Create_map();
void gotoxy(int x, int y);
void Run_game();
void Product_food();
void Eat_food();
void Move_snake(int direction);
void Good_bye();
enum {UP=2, DOWN=80, LEFT=75, RIGHT=77};
typedef struct _SNAKE {
int x, y;
struct _SNAKE* next;
}Snake;
Snake* head = NULL;
void Init_snake();
// 主函数
int main() {
Game_start();
Run_game();
Good_bye();
return 0;
}
void Init_snake() {
Snake* t = (Snake*)malloc(sizeof(Snake));
t->x = 20;
t->y = 15;
t->next = NULL;
head = t;
for (int i = 1; i <= 3; i++)
{
Snake* p = (Snake*)malloc(sizeof(Snake));
p->x = 20 + 2 * i;
p->y = 15;
p->next = NULL;
t->next = p;
t = p;
}
t = head;
while (t != NULL)
{
gotoxy(t->x, t->y);
printf("■");
t = t->next;
}
}
void Move_snake(int direction) {
Snake* p = head;
//找尾巴
while (p != NULL && p->next != NULL)
{
p = p->next;
}
//加一个节点
Snake* t = (Snake*)malloc(sizeof(Snake));
t->next = NULL;
if (direction == RIGHT)
{
t->x = p->x + 2;
t->y = p->y;
}
else if (direction == LEFT)
{
t->x = p->x - 2;
t->y = p->y;
}
else if (direction == UP)
{
t->x = p->x;
t->y = p->y - 1;
}
else if (direction == DOWN)
{
t->x = p->x;
t->y = p->y + 1;
}
p->next = t;
//打印
gotoxy(t->x, t->y);
printf("■");
gotoxy(head->x, head->y);
printf(" ");
if ((t->x)==0||(t->x)==58||(t->y)==0||(t->y)==29)
{
flag = 0;
}
if (t->x==food_x&&t->y==food_y)
{
food_flag = 0;
}
//释放头
t = head->next;
delete head;
head = t;
}
void Run_game() {
start:
flag = 1;
Create_map();
Init_snake();
Product_food();
int direction = RIGHT;
while (1)
{
if (food_flag==0)
{
gotoxy(food_x, food_y);
printf(" ");
Product_food();
Eat_food();
}
//判断有没有输入
if (_kbhit())//该函数监听输入
{
char ch = _getch();//输入键值 72:上 75:左 77:右 80:下
if (direction==RIGHT)
{
switch (ch)
{
case 72:
direction = UP;
break;
case 80:
direction = DOWN;
break;
default:
break;
}
}
else if (direction==LEFT)
{
switch (ch)
{
case 72:
direction = UP;
break;
case 80:
direction = DOWN;
break;
default:
break;
}
}
else if (direction==UP)
{
switch (ch)
{
case 75:
direction = LEFT;
break;
case 77:
direction = RIGHT;
break;
default:
break;
}
}
else {
switch (ch)
{
case 75:
direction = LEFT;
break;
case 77:
direction = RIGHT;
break;
default:
break;
}
}
}
// 移动蛇
Move_snake(direction);
if (flag==0)
{
break;
}
Sleep(level);
}
system("cls");
printf("\t\t你个菜鸡\t\t\n");
printf("想继续打吗请输入1否则退出\n");
char h = getchar();
system("cls");
if (h=='1')
{
goto start;
}
}
void Game_start() {
system("mode con cols=100 lines=34");
Welcome_game();
}
void Create_map() {
for (int i = 0; i < 60; i+=2)
{
gotoxy(i, 0);
printf("■");
gotoxy(i, 29);
printf("■");
}
for (int i = 0; i < 30; i++)
{
gotoxy(0, i);
printf("■");
gotoxy(58, i);
printf("■");
}
}
void gotoxy(int x, int y) {
COORD pos = {x, y};
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, pos);
}
void Welcome_game() {
printf("\n\n\n\n\n\n");
printf_s("\t\t===========================================================\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t欢迎进入\t贪吃蛇世界\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t========================开发者:LYM==========================\n");
system("pause");
system("cls");
}
void Good_bye() {
printf("\n\n\n\n\n\n");
printf_s("\t\t===========================================================\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\tcsdn:https://blog.csdn.net/qq_60952169\t\t =\n");
printf_s("\t\t=\t\t正在离开\t贪吃蛇世界\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t=\t\t\t\t\t\t\t =\n");
printf_s("\t\t========================开发者:LYM==========================\n");
system("pause");
system("cls");
}
void Product_food() {
food_flag = 1;
food_x = (rand() % 28+1)*2;
food_y = rand() % 28+1;
gotoxy(food_x, food_y);
printf("□");
}
void Eat_food() {
Snake* p = head;
//找尾巴
Snake* new_node = (Snake*)malloc(sizeof(Snake));
new_node->x = 2 * (head->x) - (head->next->x);
new_node->y = 2 * (head->y) - (head->next->y);
head = new_node;
head->next = p;
if(level >= 100)level -= 30;//吃到食物后加速
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。