初级光能
#include <bits/stdc++.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
struct pos
{
int x;
int y;
} food, head;
const int mapX = 35;
const int mapY = 18;
int speed = 300;
int snake[mapX * mapY][2];
int length;
int direction;
void info();
void init();
void getInput();
void changeSnake();
bool alive();
void makeFood();
void gotoXY(short, short);
void drawMap();
void drawFood();
void drawSnake();
void clearSnake();
void upgrade();
bool getFood();
void run();
void gameOver();
int main()
{
run();
}
void run()
{
cout << "按任意键开始游戏..." << endl;
getch();
srand(int(time(NULL)));
info();
init();
while (true)
{
gotoXY(37, 3);
cout << "蛇的长度是:" << length;
getInput();
clearSnake();
changeSnake();
drawSnake();
if (!alive())
{
gameOver();
break;
}
if (getFood())
{
upgrade();
makeFood();
drawFood();
}
Sleep(speed-length/10*50);
}
}
void info()
{
gotoXY(37,9);
cout << "|--------------------|";
gotoXY(37,10);
cout << "|wasd控制上下左右移动|";
gotoXY(37,11);
cout << "|空格键暂停 |";
gotoXY(37,12);
cout << "|--------------------|";
}
void init()
{
length = 1;
head.x = snake[0][0] = 7;
head.y = snake[0][1] = 7;
makeFood();
direction = 3;
drawMap();
drawFood();
drawSnake();
}
void getInput()
{
char ch;
if (_kbhit())
{
ch = getch();
if ((ch == 'w' && direction == 3) ||
(ch == 'a' && direction == 4) ||
(ch == 's' && direction == 1) ||
(ch == 'd' && direction == 2))
{
return ;
}
switch (ch)
{
case 'w':
direction = 1;
break;
case 'a':
direction = 2;
break;
case 's':
direction = 3;
break;
case 'd':
direction = 4;
break;
case ' ':
{
gotoXY(37,13);
cout << "游戏暂停,按任意键继续游戏...";
getch();
}
break;
default:
break;
}
}
}
void changeSnake()
{
switch (direction)
{
case 1:
head.y--;
break;
case 2:
head.x--;
break;
case 3:
head.y++;
break;
case 4:
head.x++;
break;
default:
break;
}
for (int i = length; i > 0; i--)
{
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
snake[0][0] = head.x;
snake[0][1] = head.y;
}
bool getFood()
{
return (head.x == food.x && head.y == food.y) ? true : false;
}
bool alive()
{
if (head.x == 0 || head.x == (mapX + 1) || head.y == 0 || head.y == (mapY + 1))
{
return false;
}
for (int i = 1; i < length; i++)
{
if (head.x == snake[i][0] && head.y == snake[i][1])
{
return false;
}
}
return true;
}
void makeFood()
{
food.x = rand() % mapX + 1;
food.y = rand() % mapY + 1;
for (int i = 0; i < length; i++)
{
if (food.x == snake[i][0] && food.y == snake[i][1])
{
makeFood();
break;
}
}
}
void upgrade()
{
length++;
snake[length - 1][0] = food.x;
snake[length - 1][1] = food.y;
}
void gotoXY(short x, short y)
{
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void drawMap()
{
gotoXY(0, 0);
for (int i = 0; i < 37; i++) cout << "-";
gotoXY(0, 19);
for (int i = 0; i < 37; i++) cout << "-";
for (int i = 0; i < 20; i++)
{
gotoXY(0, i);
cout << "|";
gotoXY(36, i);
cout << "|";
}
}
void drawFood()
{
gotoXY(food.x, food.y);
cout << "$";
}
void drawSnake()
{
for (int i = 0; i < length; i++)
{
gotoXY(snake[i][0], snake[i][1]);
cout << "@";
}
}
void clearSnake()
{
gotoXY(snake[length - 1][0], snake[length - 1][1]);
cout << " ";
}
void gameOver()
{
system("cls");
cout << "Game Over";
}//放到DEV-C++里运行