我用C++编了一个贪吃蛇,供大家参考,大神请无视。
主要代码如下:
Gluttonous-Snake.cpp
[C++] 纯文本查看 复制代码 #include "stdafx.h"
#include "tool.h"
int main()
{
cout << " --" << endl;
cout << " |贪|" << endl;
cout << " |吃|" << endl;
cout << " |蛇|" << endl;
cout << " --" << endl;
Sleep(3000);
//开始界面
char map[mapa][mapb] = {
" ------------------------------ ",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
" ------------------------------ "
};
queue<snk> snake;
print(map);
Sleep(700);
map[22][16] = ' ';
print(map);
Sleep(700);
map[22][16] = '#';
print(map);
Sleep(700);
map[21][16] = '#';
map[22][16] = '*';
print(map);
Sleep(700);
map[20][16] = '#';
map[21][16] = '*';
map[22][16] = '*';
print(map);
Sleep(700);
map[19][16] = '#';
map[20][16] = '*';
map[21][16] = '*';
map[22][16] = '*';
print(map);
Sleep(700);
map[18][16] = '#';
map[19][16] = '*';
map[20][16] = '*';
map[21][16] = '*';
map[22][16] = '*';
print(map);
Sleep(700);
map[17][16] = '#';
map[18][16] = '*';
map[19][16] = '*';
map[20][16] = '*';
map[21][16] = '*';
map[22][16] = '-';
print(map);
Sleep(700);
system("color 70");
Sleep(50);
system("color 07");
Sleep(50);
system("color 70");
Sleep(50);
system("color 07");
Sleep(50);
system("color 70");
Sleep(50);
system("color 07");
Sleep(50);
system("color 70");
Sleep(50);
system("color 07");
Sleep(50);
system("color 70");
Sleep(50);
system("color 07");
Sleep(100);
cout << "按任意键开始游戏......";
getch();
//蛇进场动画
//mstart = clock();//开始时间
int wait = 1000;
snk t;
direction dir = north;
t.x = 21; t.y = 16;
snake.push(t);
t.x = 20;
snake.push(t);
t.x = 19;
snake.push(t);
t.x = 18;
snake.push(t);
t.x = 17;
snake.push(t);
//增加蛇
int rx, ry;
srand(time(NULL));
rx = 1 + rand() % (mapa - 3);
ry = 1 + rand() % (mapb - 3);
map[rx][ry] = '0';
//生成食物
print(map);
while (true) {
int start;
char t;
bool getin;
snk snkt;
cout << "按 a d w s (小写)控制蛇活动......";
start = clock();
while (true) {
if (clock() - start > wait) {
getin = false;
break;
}
if (kbhit()) {
t = getch();
if (t != 'a'&&t != 'd'&&t != 's'&&t != 'w') {
print(map);
cout << "错误,请重新输入......";
}
else {
getin = true;
break;
}
}
}
if (t == 'w' || (dir == north&&getin == false)) {
if (snake.back().x - 1 == 0) end(snake.size()-5);
if (eatself(snake.back().x - 1, snake.back().y, snake))end(snake.size()-5);
snkt.x = snake.back().x - 1;
snkt.y = snake.back().y;
map[snkt.x][snkt.y] = '#';
map[snake.back().x][snake.back().y] = '*';
if (snkt.x != rx || snkt.y != ry) {
map[snake.front().x][snake.front().y] = ' ';
snake.pop();
}
else {
rx = 1 + rand() % (mapa - 3);
ry = 1 + rand() % (mapb - 3);
map[rx][ry] = '0';
if (wait > 100)wait -= 50;
}
snake.push(snkt);
dir = north;
}
else if (t == 'a' || (dir == west&&getin == false)) {
if (snake.back().y - 1 == 0) end(snake.size()-5);
if (eatself(snake.back().x, snake.back().y - 1, snake))end(snake.size()-5);
snkt.x = snake.back().x;
snkt.y = snake.back().y - 1;
map[snkt.x][snkt.y] = '#';
map[snake.back().x][snake.back().y] = '*';
if (snkt.x != rx || snkt.y != ry) {
map[snake.front().x][snake.front().y] = ' ';
snake.pop();
}
else {
rx = 1 + rand() % (mapa - 3);
ry = 1 + rand() % (mapb - 3);
map[rx][ry] = '0';
if (wait > 100)wait -= 50;
}
snake.push(snkt);
dir = west;
}
else if (t == 'd' || (dir == east&&getin == false)) {
if (snake.back().y + 1 == mapb - 2) end(snake.size()-5);
if (eatself(snake.back().x, snake.back().y + 1, snake))end(snake.size()-5);
snkt.x = snake.back().x;
snkt.y = snake.back().y + 1;
map[snkt.x][snkt.y] = '#';
map[snake.back().x][snake.back().y] = '*';
if (snkt.x != rx || snkt.y != ry) {
map[snake.front().x][snake.front().y] = ' ';
snake.pop();
}
else {
rx = 1 + rand() % (mapa - 3);
ry = 1 + rand() % (mapb - 3);
map[rx][ry] = '0';
if (wait > 100)wait -= 50;
}
snake.push(snkt);
dir = east;
}
else if (t == 's' || (dir == south&&getin == false)) {
if (snake.back().x + 1 == mapa - 1) end(snake.size()-5);
if (eatself(snake.back().x + 1, snake.back().y, snake))end(snake.size()-5);
snkt.x = snake.back().x + 1;
snkt.y = snake.back().y;
map[snkt.x][snkt.y] = '#';
map[snake.back().x][snake.back().y] = '*';
if (snkt.x != rx || snkt.y != ry) {
map[snake.front().x][snake.front().y] = ' ';
snake.pop();
}
else {
rx = 1 + rand() % (mapa - 3);
ry = 1 + rand() % (mapb - 3);
map[rx][ry] = '0';
if (wait > 100)wait -= 50;
}
snake.push(snkt);
dir = south;
}
print(map);
}
return 0;
}
stdafx.h
[C++] 纯文本查看 复制代码 #pragma once
const int mapa = 23, mapb = 33;
enum direction { north, west, east, south };
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <queue>
#include <time.h>
#include <Windows.h>
using namespace std;
tool.h
[C++] 纯文本查看 复制代码 #pragma once
#include "stdafx.h"
void print(char a[mapa][mapb]) {
system("cls");
int i, j;
for (i = 0; i < mapa; i++) {
for (j = 0; j < mapb; j++)
cout.put(a[i][j]);
cout << endl;
}
}
enum snkt { head, tail, body };
struct snk {
int x, y;
};
void end(int n) {
system("cls");
if (n > 0)cout << "你输了!" << endl << "你成功吃掉了" << n << "个食物!" << endl;
else cout << "你还能输得在彻底一点吗?" << endl;
cout << "按任意键退出......";
getch();
exit(0);
}
bool eatself(int x, int y, queue <snk> snake) {
while (snake.size() > 0) {
if (snake.front().x == x&&snake.front().y == y)return true;
snake.pop();
}
return false;
}
|