博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
贪吃蛇
阅读量:6406 次
发布时间:2019-06-23

本文共 4613 字,大约阅读时间需要 15 分钟。

// ChangePartitionNumber.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include 
#include
#include
//即standard library标志库头文件,里面定义了一些宏和通用工具函数#include
//接收键盘输入输出#include
//用于获得随机数#include
//MFC#include
#include
POINT meat;//蛇爱吃的肉wchar_t *str = _T("头学习计算机技术\0");int nextChar = 1;int width = 66;int height = 38;POINT preHead;enum DIRECTION{ UP, DOWN, LEFT, RIGHT};typedef struct Snake{ wchar_t ch; DIRECTION dir; POINT point; Snake * next;}S;void drawWall();void printfSnake(S s);void followHead(S * s);BOOL isDoublication(S s);void gotoxy(POINT head){ COORD c; c.X = head.x; c.Y = head.y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);}void ControlCursorPosition(){ drawWall(); int second; second = GetTickCount(); meat.x = second % width; meat.y = second % height; //判断奇偶数,因为奇数坐标,蛇吃不到实物 if (meat.x & 1) { meat.x++; } if (meat.y & 1) { meat.y++; } S s;//?头 s.point.x = 30; s.point.y = 30; s.ch = str[0]; s.dir = UP; s.next = NULL; S ** tail = (S**)malloc(sizeof(S)); tail = &(s.next);//空的 S *tailBody = &s;//保存末尾的身体 gotoxy(s.point); wprintf(L"%c", s.ch); preHead = s.point; gotoxy(meat); wprintf(L"%c", str[nextChar]); while (true) { //产生移动了 if (preHead.x != s.point.x || preHead.y != s.point.y) { //吃到食物 if (s.point.x == meat.x && s.point.y == meat.y) { //找到最后一个NULL指针 while (*tail != NULL) { tailBody = *tail; tail = &((*tail)->next); } *tail = (S*)malloc(sizeof(S));//为最后一个空指针分配内存,准备添加吃到的食物 (*tail)->ch = str[nextChar]; //添加到尾部的身体,要添加到前一个的后面,根据最后一个身体的坐标,方向 if ((*tailBody).dir == UP) { (*tail)->point.y = (*tailBody).point.y + 1; (*tail)->point.x = (*tailBody).point.x; (*tail)->dir = (*tailBody).dir; } else if ((*tailBody).dir == DOWN) { (*tail)->point.y = (*tailBody).point.y - 1; (*tail)->point.x = (*tailBody).point.x; (*tail)->dir = (*tailBody).dir; } else if ((*tailBody).dir == LEFT) { (*tail)->point.y = (*tailBody).point.y; (*tail)->point.x = (*tailBody).point.x + 2; (*tail)->dir = (*tailBody).dir; } else if ((*tailBody).dir == RIGHT) { (*tail)->point.y = (*tailBody).point.y; (*tail)->point.x = (*tailBody).point.x - 2; (*tail)->dir = (*tailBody).dir; } (*tail)->next = NULL; nextChar++; if (nextChar == (wcslen(str))) { nextChar = 0; } } system("cls"); drawWall(); //画蛇 printfSnake(s); //逆序跟随蛇头 followHead(&s);//这一行如果放在画蛇前面会吞掉一个字 //画食物 gotoxy(meat); wprintf(L"%c", str[nextChar]); preHead = s.point; } Sleep(10); //printf("a"); DIRECTION preDir = s.dir; if (GetAsyncKeyState(VK_UP) & 0x8000 && s.dir != DOWN){ s.point.y--;//这里只是蛇头动了,所以食物不能跟随蛇头 s.dir = UP; if (isDoublication(s)) { s.point.y++; s.dir = preDir; } } else if (GetAsyncKeyState(VK_DOWN) & 0x8000 && s.dir != UP){ s.point.y++; s.dir = DOWN; if (isDoublication(s)) { s.point.y--; s.dir = preDir; } } else if (GetAsyncKeyState(VK_LEFT) & 0x8000 && s.dir != RIGHT){ s.point.x -= 2; s.dir = LEFT; if (isDoublication(s)) { s.point.x += 2; s.dir = preDir; } } else if (GetAsyncKeyState(VK_RIGHT) & 0x8000 && s.dir != LEFT){ s.point.x += 2; s.dir = RIGHT; if (isDoublication(s)) { s.point.x -= 2; s.dir = preDir; } } }}//撞到自己BOOL isDoublication(S s){ POINT headPoint = s.point; S * next = s.next; while (next != NULL) { if (headPoint.x == next->point.x && headPoint.y == next->point.y) { return TRUE; } next = next->next; } return FALSE;}//跟随蛇头, 把所有的节点向前移动//递归逆序遍历链表void followHead(S * s){ S * next = s->next; S first = *s; if (next != NULL) { followHead(next); } else { return; } next->point = first.point; next->dir = first.dir;}void printfSnake(S s){ gotoxy(s.point); wprintf(L"%c", s.ch); S * next = s.next; while (next != NULL) { gotoxy((*next).point); wprintf(L"%c", (*next).ch); next = next->next; }}void drawWall(){ COORD c; for (int i = 0; i < width; i += 2){ c.X = i; c.Y = 0; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); printf("■"); } for (int i = 0; i < width; i += 2){ c.Y = height; c.X = i; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); printf("■"); } for (int i = 0; i < height + 1; i++){ c.Y = i; c.X = 0; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); printf("■"); } for (int i = 0; i < height + 1; i++){ c.Y = i; c.X = width; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); printf("■"); }}int _tmain(int argc, _TCHAR* argv[]){ //隐藏光标 HANDLE fd = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cinfo; cinfo.bVisible = 0; cinfo.dwSize = 1; SetConsoleCursorInfo(fd, &cinfo); //设置区域 _wsetlocale(LC_ALL, L"chs"); ControlCursorPosition(); getchar(); return 0;}

  //内存没有释放

转载于:https://www.cnblogs.com/wangxingzhou/p/10493369.html

你可能感兴趣的文章
【webpack4】基本配置和入门api
查看>>
Mac使用ssh公钥登录Linux
查看>>
【366天】跃迁之路——程序员高效学习方法论探索系列(实验阶段124-2018.02.06)...
查看>>
POJ3070-Fibonacci(矩阵快速幂)
查看>>
[vue插件]基于vue2.x的电商图片放大镜插件
查看>>
标准的组件结构
查看>>
vue——一个页面实现音乐播放器
查看>>
SVG 扬帆起航
查看>>
NET Core-学习笔记(二)
查看>>
职业生涯上的点点滴滴
查看>>
Linux下添加新硬盘,分区及挂载
查看>>
一起来将vscode变成私人定制笔记本
查看>>
Flutter 云音乐
查看>>
RecyclerView实现多type页面
查看>>
个人的web商城网站
查看>>
debian fcitx
查看>>
排中律与实无穷问题的性质分析
查看>>
08/23 学习总结
查看>>
物理层
查看>>
linux多网卡路由设置
查看>>