|
C++编了一个贪吃蛇,供大家参考,大神请无视。
主要代码如下:
Gluttonous-Snake.cpp
[mw_shl_code=cpp,true]// Gluttonous-Snake.cpp : 定义控制台应用程序的入口点。
//
#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;
}[/mw_shl_code]
stdafx.h
[mw_shl_code=cpp,true]// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#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;
// TODO: 在此处引用程序需要的其他头文件
[/mw_shl_code]
tool.h
[mw_shl_code=cpp,true]#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[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;
}[/mw_shl_code]
Gluttonous-Snake源码.7z
(1.69 KB, 下载次数: 343)
|
|