嘻嘻,这是我自己写的第一个C++小游戏,还记得当初有一个软件学院的同学说他们的一个暑期作业就是用C实现贪吃蛇,哇,当时觉得好高大上!!因为光想一想,就觉得其中的逻辑好复杂╮(╯▽╰)╭
今天我自己实现后,觉得只要把逻辑线搞懂,就可以实现一个基本功能的贪吃蛇了!废话不多说,上思路。
1:需要四个基本的函数,分别为setup()[初始化], draw()[显示], input()[键盘输入上下左右], logic()[吃水果,得分,加尾巴],声明一个游戏状态的标识符gameover, 下面这段代码则展示了整个程序的框架,接下来就是要在框架里添东西。
void draw()
void setup()
void input()
void logic()
bool gameover = false;
int main()
{
setup();
while(!gameover)
{
draw();
input();
logic();
}
return 0;
}
2: 在draw()函数里创建一个画板,大小为20*20(具体略过)
在setup()函数里初始化蛇头的位置(x,y),和水果的位置(fruitx, fruity)[水果位置随即变]
for (int i = 0; i < width+2; i++)
{
cout<< "#";
}
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ( j == 0)
cout << "#";
if (i == y && j == x)
cout << "0";
else if (i == fruitY && j == fruitX)
cout << "F";
else
{
cout<< " ";
}
if ( j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width+2; i++)
{
cout<< "#" ;
}
cout << endl;
3: 键盘输入函数使用标准库#incldue
void Input()
{
if (_kbhit())
{
switch(_getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
4: 逻辑控制,先考虑移动的问题
switch(dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
defult:
break;
}
5: 程序的难点在于尾巴增加和移动
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
const int height = 20;
const int width = 20;
bool gameOver = false;
int x, y, fruitX, fruitY;
int score;
enum eDirection{UP, DOWN, LEFT, RIGHT, STOP};
eDirection dir;
int tailx[100], taily[100];
int ntail = 1;
void canvas()
{
for (int i = 0; i < width+2; i++)
{
cout<< "#";
}
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if ( j == 0)
cout << "#";
if (i == y && j == x)
cout << "0";
else if (i == fruitY && j == fruitX)
cout << "F";
else
{
bool printtail = false;
for ( int k = 1; k < ntail; k++)
{
if(i == taily[k] && j == tailx[k])
{
cout<<"o";
printtail = true;
}
}
if(!printtail) //这里需要添加标志符,来判断是否为尾巴
cout<< " ";
}
if ( j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width+2; i++)
{
cout<< "#" ;
}
cout << endl;
cout << "Score = " << score << endl;
}
void setUp()
{
gameOver = false;
dir = STOP;
x = width /2;
y = height /2;
fruitX = rand() % width;
fruitY = rand() % height;
}
void draw()
{
system("cls");
canvas();
}
void Input()
{
if (_kbhit())
{
switch(_getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
void logic()
{
tailx[0] = x;
taily[0] = y;
int prex = tailx[0], prey = taily[0];
int prex2,prey2;
for (int i = 1; i < ntail; i++) //这里的尾巴位置坐标是后一个的位置变为前一个位置
{ //这里需要理解
prex2 = tailx[i]; //
prey2 = taily[i]; //
tailx[i] = prex; //
taily[i] = prey;
prex = prex2;
prey = prey2;
}
switch(dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
defult:
break;
}
if (x > width || x < 0)
{
x = abs(x - width);
}
if (y > height || y < 0)
{
y = abs(y - height);
}
/*if (x > width || x < 0 || y > height || y < 0)
gameOver = true;*/
if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
ntail ++;
}
for( int i = 1; i < ntail; i++)
{
if (tailx[i] == x && taily[i] == y)
gameOver = true;
}
}
int main()
{
setUp();
while(!gameOver)
{
draw();
Input();
logic();
Sleep(100);
}
return 0;
}
代码思路是参考这个视频
https://www.youtube.com/channel/UCqXrJmvJAvFnGyLGSBnk45g
编程真有意思(^o^)/~