c++ 让程序玩贪吃蛇游戏
貪吃蛇游戲的代碼
#include <iostream>
#include <string>
#include<windows.h>
#include <conio.h>
#include<thread>
#include "/Users/Administrator/source/repos/ConsoleApplication1/Test.h"
using namespace std;
string h1 = "*****************************************************************************\n";
string h2 = "*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*\n";
clock_t tstart, tfinish;
const unsigned char CTRL_KEY = 0XE0;
const unsigned char LEFT = 0X4B;
const unsigned char RIGHT = 0X4D;
const unsigned char DOWN = 0X50;
const unsigned char UP = 0X48;
typedef struct snbody
{
?? ?char z = '%';
?? ?short x, y;
}Body;
Body ?B[1678], A;
Body *Topbody = &B[1];
Body *Nxbody = &B[0];
Body *Midbody = &A;
char map[76][22] = { 0 };
short xpos = 10, ypos = 5;
short showchar();
short nxchar = 1;
int start = 1;
int out(short x, short y);
int stime = 200;
int num = 0;
short tar[2];
?
int onmap(short x, short y,char z)
{
?? ?if ( map[x][y] != '<' && map[x][y] != ?'>' && map[x][y] != 'A' && map[x][y] != 'V')
?? ?{
?? ??? ?
? ? ? ??
?? ??? ?if (Topbody - B > 1670)?
?? ??? ??? ?Topbody = &B[0];
?? ??? ??? ?
?? ??? ?
?? ??? ?if(Nxbody -B > 1670)
?? ??? ??? ?Nxbody = &B[0];
?? ??? ??? ?
?? ??? ?Topbody->z = map[x][y] = z;
?? ??? ?Topbody->x = x;
?? ??? ?Topbody->y = y;
?? ??? ?//Midbody->z = Topbody->z;
?? ??? ?if (x == xpos && y == ypos)
?? ??? ?{
? ? ? ? ? ?
?? ??? ??? ?++num;
?? ??? ??? ?xpos = (rand() % (76 - 1)) + 1;
?? ??? ??? ?ypos = (rand() % (22 - 1)) + 1;
?? ??? ??? ?tar[0] = xpos;
?? ??? ??? ?tar[1] = ypos;
?? ??? ??? ?if(stime>100)
?? ??? ??? ??? ?stime = stime-10;
? ? ? ??
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ??? ?out(Nxbody->x, Nxbody->y);
?? ??? ??? ??? ?map[Nxbody->x][Nxbody->y] = '0';
?? ??? ??? ??? ?Nxbody->z = '0';
?? ??? ??? ??? ?printf("%c",'\0');
?? ??? ??? ??? ?++Nxbody;
? ? ? ? ? ? ? ??
?? ??? ??? ?
?? ??? ?}
?? ??? ??? ?
?? ??? ??? ?out(x, y);
?? ??? ??? ?cout << Topbody->z;
?? ??? ??? ?++Topbody;
? ? ? ? ? ?
?? ??? ??? ?
?? ?}
?? ?else
?? ?{
?? ??? ?cout << "game over!!!";
?? ??? ?start = 0;
?? ??? ?system("cls");
?? ??? ?out(38,12);
?? ??? ?cout << "一共吃掉:" << num <<"個桃子"<< endl;
?? ??? ?Sleep(5000);
?? ??? ?return 1;
?? ?}
?? ?
?? ?
?? ?return 0;
}
int movefor(short *pos,char z ,int (*pt)(short *, char ))
{
?? ?while (TRUE)
?? ?{
?? ??? ?
?? ??? ?showchar();
?? ??? ?if(_kbhit())
?? ??? ??? ?return 0;
?? ??? ?Sleep(stime);?
?? ?//?? ?pt(pos, z);?這里的注釋,原本是調用方向鍵按下的對應函數,但現在用下面的Test函數來玩這個游戲。
?? ??? ?Test(pos, tar,map, z);
?? ?}
?? ??? ?
?? ?return 0;
?? ?
}
?? ?int out(short x,short y)
?? ?{
?? ??? ??
?? ??? ?HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//獲得屏幕的尺寸。
?? ??? ?COORD pos = { x,y };//設置坐標
?? ??? ?SetConsoleCursorPosition(hOut, pos);//設置位置
?? ??? ?
?? ??? ?return 0;
?? ?}
?? ?int move_left(short *pos, char z)
?? ?{
?? ??? ?if (pos[0] <= 1)
?? ??? ??? ?pos[0]=76;
?? ??? ?out(--pos[0],pos[1]);
?? ??? ?
?? ??? ?if (onmap(pos[0], pos[1],z='<'))
?? ??? ??? ?return 1;
?? ??? ?movefor(pos, z, move_left);
?? ??? ?return 0;
?? ?}
?? ?int move_right(short *pos, char z)
?? ?{
?? ??? ?if (pos[0] >= 75)
?? ??? ??? ?pos[0]=0;
?? ??? ?out(++pos[0], pos[1]);
?? ??? ?if (onmap(pos[0], pos[1],z='>'))
?? ??? ??? ?return 1;
?? ??? ?movefor(pos, z, move_right);
?? ??? ?return 0;
?? ?}
?? ?int move_up(short *pos, char z)
?? ?{
?? ??? ?if (pos[1] <= 1)
?? ??? ??? ?pos[1]=22;
?? ??? ?out(pos[0], --pos[1]);
?? ??? ?if (onmap(pos[0], pos[1],z='A'))
?? ??? ??? ?return 1;
?? ??? ?movefor(pos, z, move_up);
?? ??? ?return 0;
?? ?}
?? ?int move_down(short *pos,char z)
?? ?{
?? ??? ?if (pos[1] >= 21)
?? ??? ??? ?pos[1]=0;
?? ??? ?out(pos[0], ++pos[1]);
?? ??? ?if (onmap(pos[0], pos[1],z='V'))
?? ??? ??? ?return 1;
?? ??? ?movefor(pos, z, move_down);
?? ??? ?return 0;
?? ?}
?? ?
?? ?short showchar()
?? ?{
?? ??? ?//int x,y;
?? ??? ?
?? ??? ?out(xpos,ypos);
?? ??? ?switch(nxchar)
?? ??? ?{
?? ??? ?case 1:cout << '|' << '\b'; ?break;
?? ??? ?case 2:cout << '/' << '\b'; break;
?? ??? ?case 3:cout << '-' << '\b'; break;
?? ??? ?case 4:cout << '\\' << '\b'; break;
?? ??? ?default:nxchar = 1; break;
?? ??? ?}
?? ??? ?++nxchar;
?? ??? ?tfinish=clock();
?? ??? ?
?? ??? ??? ?out(86, 1);
?? ??? ??? ?printf("%u",(tfinish - tstart) / CLOCKS_PER_SEC );
?? ??? ??? ?out(92, 3);
?? ??? ??? ?cout << num;
?? ??? ?
?? ??? ?return 0;
?? ?}
int main(){
?? ?char z = '*';
?? ?cout << h1 ;
?? ?for (int i = 0; i <= 20; i++)
?? ??? ?cout << h2;
?? ?cout << h1;
?? ?out(80, 1);
?? ?cout << "時間:" ;
?? ?out(80, 3);
?? ?cout << "你已經吃了:"<<'\t'<< "個桃子";
?? ?short pos[2] = {38,11};
?? ?
?? ?int flag = 0;
?? ?out(pos[0], pos[1]);
?? ?xpos = (rand() % (76 - 1)) + 1;
?? ?ypos = (rand() % (22 - 1)) + 1;
?? ?tstart=clock();
?? ?srand(tstart);
?? ?tar[0] = xpos;
?? ?tar[1] = ypos;
?? ?while (start)
?? ?{
? ? ? ??
?? ??? ?Test(pos, tar,map, z);
?? ??? ?if (flag)
?? ??? ?{
?? ??? ??? ?switch (_getch())
?? ??? ??? ?{
?? ??? ??? ?case LEFT:move_left(pos,z); break;
?? ??? ??? ?case RIGHT:move_right(pos, z ); break;
?? ??? ??? ?case DOWN:move_down(pos, z ); break;
?? ??? ??? ?case UP:move_up(pos, z ); break;
?? ??? ??? ?default: ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?if (_getch() == CTRL_KEY)
?? ??? ??? ??? ?flag = 1;
?? ??? ?}
?? }
?
?? ??? ??? ?
下面是讓程序自己玩貪吃蛇
? ? ??
int Test(short *pos, short *tar, char(*map)[22], char z)
{
?? ?int x, y;
?? ?x = pos[0];
?? ?y = pos[1];
?? ?
?? ?
?? ?
?? ?char stopr = '0';
?? ?char stopl = '0';
?? ?char stopu = '0';
?? ?char stopd = '0';
?? ?
?? ??? ?
?? ??? ?if (map[x + 1][y] == 'A' || map[x + 1][y] == 'V')
?? ??? ??? ?stopr = map[x + 1][y];
?? ??? ?if (map[x - 1][y] == 'A' || map[x - 1][y] == 'V')
?? ??? ??? ?stopl = map[x - 1][y];
?? ??? ?if (map[x][y - 1] == '<' || map[x][y - 1] == '>')
?? ??? ??? ?stopu = map[x][y - 1];
?? ??? ?if (map[x][y + 1] == '<' || map[x][y + 1] == '>')
?? ??? ??? ?stopd = map[x][y + 1];
?? ?
? ? ? ??
?? ??? ?if (pos[0] < tar[0] && map[x + 1][y] != '<' && map[x + 1][y] != '>' && map[x + 1][y] != 'A' && map[x + 1][y] != 'V' )
?? ??? ?{
?? ??? ??? ?
?? ??? ??? ?move_right(pos, z);
?? ??? ?}
?? ??? ?else if (pos[0] > tar[0] && map[x - 1][y] != '<' && map[x - 1][y] != '>' && map[x - 1][y] != 'A' && map[x - 1][y] != 'V' )
?? ??? ?{
?? ??? ??? ?
?? ??? ??? ??? ??? ?move_left(pos, z);
?? ??? ?}
?? ??? ?else if (pos[1] < tar[1] && map[x][y + 1] != '<' && map[x][y + 1] != '>' && map[x][y + 1] != 'A' && map[x][y + 1] != 'V' )
?? ??? ?{
?? ??? ??? ?if (z == '<')
?? ??? ??? ?{
?? ??? ??? ??? ?if (stopl == 'V'&& map[x][y - 1] != '<' && map[x][y - 1] != '>' && map[x][y - 1] != 'A' && map[x][y - 1] != 'V')
?? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ?move_down(pos, z);
?? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ?else if (z == '>')
?? ??? ??? ?{
?? ??? ??? ??? ?if (stopr == 'V'&& map[x][y - 1] != '<' && map[x][y - 1] != '>' && map[x][y - 1] != 'A' && map[x][y - 1] != 'V')
?? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ?move_down(pos,z);
?? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ?else
?? ??? ??? ??? ?move_down(pos, z);
?? ??? ?}
?? ??? ?else if (pos[1] > tar[1] && map[x][y - 1] != '<' && map[x][y - 1] != '>' && map[x][y - 1] != 'A' && map[x][y - 1] != 'V')
?? ??? ?{
?? ??? ??? ?if (z == '<')
?? ??? ??? ?{
?? ??? ??? ??? ?if (stopl == 'A'&& map[x][y + 1] != '<' && map[x][y + 1] != '>' && map[x][y + 1] != 'A' && map[x][y + 1] != 'V')
?? ??? ??? ??? ??? ?move_down(pos, z);
?? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ??? ??
?? ??? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?else if (z == '>')
?? ??? ??? ?{
?? ??? ??? ??? ?if (stopr == 'A'&& map[x][y + 1] != '<' && map[x][y + 1] != '>' && map[x][y + 1] != 'A' && map[x][y + 1] != 'V')
?? ??? ??? ??? ??? ?move_down(pos, z);
?? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ??? ?
?? ??? ??? ??? ?move_up(pos, z);
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ??? ?if ( map[x - 1][y] != '<' && map[x - 1][y] != '>' && map[x - 1][y] != 'A' && map[x - 1][y] != 'V' )
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (z == 'V')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?if (stopd == '<'&& map[x + 1][y] != '>' && map[x + 1][y] != '<' && map[x + 1][y] != 'A' && map[x + 1][y] != 'V')
?? ??? ??? ??? ??? ??? ??? ?move_right(pos, z);
?? ??? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ??? ??? ?move_left(pos, z);
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?else if (z == 'A'&& map[x + 1][y] != '>' && map[x + 1][y] != '<' && map[x + 1][y] != 'A' && map[x + 1][y] != 'V')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?if (stopu == '<')
?? ??? ??? ??? ??? ??? ??? ?move_right(pos, z);
?? ??? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ??? ??? ?move_left(pos, z);
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ??? ?move_left(pos,z);
?? ??? ??? ??? ?
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if ( map[x + 1][y] != '<' && map[x + 1][y] != '>' && map[x + 1][y] != 'A' && map[x + 1][y] != 'V')
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (z == 'V')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?if (stopd == '>'&& map[x - 1][y] != '>' && map[x - 1][y] != '<'&& map[x - 1][y] != 'A' && map[x - 1][y] != 'V')
?? ??? ??? ??? ??? ??? ??? ?move_left(pos, z);
?? ??? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ??? ??? ?move_right(pos, z);
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else if (z == 'A')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?if (stopu == '>'&& map[x - 1][y] != '>' && map[x - 1][y] != '<' && map[x - 1][y] != 'A' && map[x - 1][y] != 'V')
?? ??? ??? ??? ??? ??? ??? ?move_left(pos, z);
?? ??? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ??? ??? ?move_right(pos, z);
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ??? ?move_right(pos, z);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if (map[x][y + 1] != '<' && map[x][y + 1] != '>' && map[x][y + 1] != 'A' && map[x][y + 1] != 'V' )
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (z == '>')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?if (stopr == 'V'&& map[x][y - 1] != '>'&& map[x][y - 1] != '<' && map[x][y - 1] != 'A' && map[x][y - 1] != 'V')
?? ??? ??? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ??? ??? ?move_down(pos, z);
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else if (z == '<')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?if (stopl == 'V'&& map[x][y - 1] != '>'&& map[x][y - 1] != '<' && map[x][y - 1] != 'A' && map[x][y - 1] != 'V')
?? ??? ??? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ??? ??? ?move_down(pos, z);
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ??? ?move_down(pos, z);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if ( map[x][y - 1] != '<' && map[x][y - 1] != '>' && map[x][y - 1] != 'A' && map[x][y - 1] != 'V')
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (z == '>')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?if (stopr == 'A'&& map[x][y + 1] != '>'&& map[x][y + 1] != '<' && map[x][y + 1] != 'A' && map[x][y + 1] != 'V')
?? ??? ??? ??? ??? ??? ??? ?move_down(pos, z);
?? ??? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else if (z == '<')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?if (stopl == 'A'&& map[x][y + 1] != '>' && map[x][y + 1] != '<' && map[x][y + 1] != 'A' && map[x][y + 1] != 'V')
?? ??? ??? ??? ??? ??? ??? ?move_down(pos, z);
?? ??? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ??? ?move_up(pos, z);
?? ??? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?
?? ??? ??? ?
?? ??? ?}
?? ?
?? ?return 0;
}
? ? ??
?? ??? ?
?? ?
?? ?
總結
以上是生活随笔為你收集整理的c++ 让程序玩贪吃蛇游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从服务器取文件的命令,ftp 服务器取文
- 下一篇: mysql报错You do not ha