嘿嘿嘿嘿嘿嘿。。。。。。
好久没更了,今天更一篇。
做一个游戏。嘿嘿嘿嘿嘿[奸笑ing]
在上代码之前先给个提醒:
这个游戏可能会有亿点点虐人
这不重要。
重要的是:以下代码我是用Dev-C++写的,VS上可能会有些小错误
游戏:
- 欢迎界面
- 游戏部分
- 结束界面
- 重新开始
- 。。。。。。。后台服务人员。。。
先看代码,后讲重点
#include "resource.h"
#include <iostream>
#include <cstdlib>
//播放音乐的头文件/
#include <windows.h>
#pragma comment(lib, "winmm.lib")
#include <mmsystem.h>
#include <time.h>
#include <conio.h>
#include <fstream>
#define WNDLONG 81
using namespace std;
HANDLE handle;
COORD crd;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取实例输出句柄
void Game();
void draw();
void die_show();
void record();
void plan();
//定义地图//
char map[17][17] = { 9,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,10,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,1,
11,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,12, };
///绘制地图///
void map_draw() {
int i, j;
cout << "\t\t\b\b";
for (i = 0;i < 17;i++)
{
cout << "\t\t\t";
for (j = 0;j < 17;j++)
{
switch (map[i][j])
{
case 0:cout << " ";break;//空地
case 1:cout << "║";break;//边框
case 2:cout << "人";break;//玩家
case 3:cout << "◎";break;//金币
case 4:cout << "★";break;//奖励
case 5:cout << "♂";break;//魔药
case 6:cout << "×";break;//地雷
case 7:cout << "■";break;//墙
case 8:cout << "§";break;//柱子
边框
case 9:cout << "╔";break;
case 10:cout << "╗";break;
case 11:cout << "╚";break;
case 12:cout << "╝";break;
case 13:cout << "══";break;
}
}
cout << endl;
}
}
/定义玩家结构体并赋值///
struct Player {
int x; //玩家x坐标
int y; //玩家y坐标
double num; //玩家得分
int f; //玩家生命数
Player(); //结构中的“构造函数”,用于初始化
}p;
Player::Player() {
x = 15;
y = 8;
num = 0.0;
f = 3;
}
做清屏函数
void gotoxy() {
COORD pos;
pos.X = 0;
pos.Y = 0;
SetConsoleCursorPosition(hOut, pos);
}
///定义鼠标隐藏和显示函数/
void HSCursor(bool IS_HIDE) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = IS_HIDE;
SetConsoleCursorInfo(handle, &CursorInfo);
}
/欢迎函数/
void welcome() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
handle = GetStdHandle(STD_OUTPUT_HANDLE);
crd.Y = 13;
SetConsoleCursorPosition(handle, crd);
mciSendString(TEXT("open music\\bgm2.mp3 alias s1"), NULL, 0, NULL);
mciSendString(TEXT("play s1 repeat"), NULL, 0, NULL);
cout << "\n";
for (int i = 1;i <= 40;i++) cout << "▁▁";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED| FOREGROUND_GREEN);
cout << "\t\t\t\t\t\b\b\b\b\b\b\b疯狂捡金币小游戏\n\n";
cout << "\t\t\t【 按任意键开始 】\n\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
for (int of = 1;of <= 40;of++) cout << "▁▁";
cout << "\n";
for (int f = 1;f <= 40;f++) cout << "▁▁";
cout << "\n" << endl;
cout << "\t\b\b*金币+1分\t*奖励+4分\t*踩到地雷得分清零\t*血量为零时,游戏结束" << endl;
for (int i = 1;i <= 40;i++) cout << "▁▁";
char sh = _getch();
mciSendString(TEXT("pause s1"), NULL, 0, NULL);
mciSendString(TEXT("close s1"), NULL, 0, NULL);
system("cls");
}
//随机画出墙/
void Wall() {
int diyz, diys;
srand(time(0));
for (int diyynum = 0;diyynum <= 89;diyynum++) {
diyz = rand() % 13 + 1;
diys = rand() % 14 + 1;
map[diyz][diys] = 0;
map[diyz][diys] = 7;
}
}
随机放置奖励 ///
void reward() {
int rz, rs, renum;
srand(time(0));
renum = rand() % 4 + 1;
for (int er = 0;er <= renum;er++) {
rz = rand() % 13 + 1;
rs = rand() % 14 + 1;
map[rz][rs] = 0;
map[rz][rs] = 4;
}
}
///随机埋地雷//
void mines() {
int mz, ms, minum;
srand(time(0));
minum = rand() % 2 + 1;
for (int mi = 1;mi <= minum;mi++) {
mz = rand() % 13 + 1;
ms = rand() % 14 + 1;
map[mz][ms] = 0;
map[mz][ms] = 6;
}
}
//随机生成魔药
void potions() {
srand(time(0));
for (int j = 1;j <= 2;j++) {
int we = rand() % 15 + 1;
int wz = rand() % 14 + 1;
map[we][wz] = 5;
}
}
/随机生成金币
void coin() {
int wx, wy, zs;
srand(time(0));
wx = rand() % 15 + 1;
wy = rand() % 14 + 1;
zs = rand() % 5 + 1;
for (int i = 0;i <= zs;i++) {
if (map[wx][wy] == 0 || map[wx][wy] == 8) {
map[wx][wy] = 0;
map[wx][wy] = 3;
}
}
}
//重来函数///
void Play_Again() {
handle = GetStdHandle(STD_OUTPUT_HANDLE);
crd.X = 14; crd.Y = 18; SetConsoleCursorPosition(handle, crd);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
HSCursor(true);
///遍历地图,清除所有内容,均设置为空地(然后在开始游戏时重新绘制)///
for (int i = 0;i <= 17;i++) {
for (int j = 0;j <= 16;j++) {
if (map[i][j] == 7 || map[i][j] == 3 || map[i][j] == 4 || map[i][j] == 6 || map[i][j] == 8 || map[i][j] == 5 || map[i][j] == 2) {
map[i][j] = 0;
}
}
}
///玩家属性初始化
p.num = 0;
p.f = 3;
p.x = 15;
p.y = 8;
map[p.x][p.y] = 2;
/打印提示并判断玩家输入的值//
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "是 否 重 新 开 始 ( Y / N ):";
char ch;
cin >> ch;
if (ch == 'Y' || ch == 'y') {
SetConsoleTitle(L"疯狂捡金币小游戏");
system("cls");
Game();
}
else if (ch == 'N' || ch == 'n') {
return;
}
else {
MessageBox(NULL, L"不要乱选!", L" ", MB_OK | MB_SETFOREGROUND);
return Play_Again();
}
}
/游戏部分(包括绘制内容,人物移动,音乐播放,死亡判断和分数记录//
void Game() {
///绘制内容
reward();
Wall();
mines();
potions();
draw();
/音乐播放/
mciSendString(TEXT("open music\\bgm.mp3 alias s3"), NULL, 0, NULL);
mciSendString(TEXT("play s3 repeat"), NULL, 0, NULL);
HSCursor(false); //隐藏光标
while (1) {
coin(); //绘制金币(必须放在循环内,不然只绘制一次)
/人物移动
int ch = _getch();
if (ch == 72 || ch == 119 || ch == 87) {
if (map[p.x - 1][p.y] == 0) {
map[p.x][p.y] = 0;
p.x--;
map[p.x][p.y] = 2;
}
if (map[p.x - 1][p.y] == 3) {
map[p.x][p.y] = 8;
p.x--;
map[p.x][p.y] = 2;
p.num++;
}
if (map[p.x - 1][p.y] == 4) {
map[p.x][p.y] = 0;
p.x--;
map[p.x][p.y] = 2;
p.num += 4;
}
if (map[p.x - 1][p.y] == 6) {
map[p.x][p.y] = 0;
p.x--;
map[p.x][p.y] = 2;
p.num = 0;
MessageBox(NULL, L"得分清零", L" ", MB_OK | MB_SETFOREGROUND);
}
if (map[p.x - 1][p.y] == 7) {
MessageBox(NULL, L"你撞到了墙", L" ", MB_OK | MB_SETFOREGROUND);
p.f--;
}
if (map[p.x - 1][p.y] == 5) {
MessageBox(NULL, L"魔药!!!!!!!!!!!!!!!!!!!\n喝魔药!!!!!!!", L"你捡到了一瓶魔药", MB_OK | MB_SETFOREGROUND);
MessageBox(NULL, L"分数翻倍", L" ", MB_OK | MB_SETFOREGROUND);
p.num = p.num * 2;
map[p.x - 1][p.y] = 0;
}
if (map[p.x - 1][p.y] == 8 && map[p.x - 2][p.y] == 0) {
map[p.x][p.y] = 0;
p.x -= 1;
map[p.x][p.y] = 2;
map[p.x - 1][p.y] = 8;
}
}
if (ch == 80 || ch == 115 || ch == 83) {
if (map[p.x + 1][p.y] == 0) {
map[p.x][p.y] = 0;
p.x++;
map[p.x][p.y] = 2;
}
if (map[p.x + 1][p.y] == 3) {
map[p.x][p.y] = 8;
p.x++;
map[p.x][p.y] = 2;
p.num++;
}
if (map[p.x + 1][p.y] == 4) {
map[p.x][p.y] = 0;
p.x++;
map[p.x][p.y] = 2;
p.num += 4;
}
if (map[p.x + 1][p.y] == 6) {
map[p.x][p.y] = 0;
p.x++;
map[p.x][p.y] = 2;
p.num = 0;
MessageBox(NULL, L"得分清零", L" ", MB_OK | MB_SETFOREGROUND);
}
if (map[p.x + 1][p.y] == 7) {
MessageBox(NULL, L"你撞到了墙", L" ", MB_OK | MB_SETFOREGROUND);
p.f--;
}
if (map[p.x + 1][p.y] == 5) {
MessageBox(NULL, L"魔药!!!!!!!!!!!!!!!!!!!\n喝魔药!!!!!!!", L"你捡到了一瓶魔药", MB_OK | MB_SETFOREGROUND);
MessageBox(NULL, L"分数减半", L" ", MB_OK | MB_SETFOREGROUND);
p.num = p.num / 2;
map[p.x + 1][p.y] = 0;
}
if (map[p.x + 1][p.y] == 8 && map[p.x + 2][p.y] == 0) {
map[p.x][p.y] = 0;
p.x += 1;
map[p.x][p.y] = 2;
map[p.x + 1][p.y] = 8;
}
}
if (ch == 75 || ch == 97 || ch == 65) {
if (map[p.x][p.y - 1] == 0) {
map[p.x][p.y] = 0;
p.y--;
map[p.x][p.y] = 2;
}
if (map[p.x][p.y - 1] == 3) {
map[p.x][p.y] = 8;
p.y--;
map[p.x][p.y] = 2;
p.num++;
}
if (map[p.x][p.y - 1] == 4) {
map[p.x][p.y] = 0;
p.y--;
map[p.x][p.y] = 2;
p.num += 4;
}
if (map[p.x][p.y - 1] == 6) {
map[p.x][p.y] = 0;
p.y--;
map[p.x][p.y] = 2;
p.num = 0;
MessageBox(NULL, L"得分清零", L" ", MB_OK | MB_SETFOREGROUND);
}
if (map[p.x][p.y - 1] == 7) {
MessageBox(NULL, L"你撞到了墙", L" ", MB_OK | MB_SETFOREGROUND);
p.f--;
}
if (map[p.x][p.y - 1] == 5) {
MessageBox(NULL, L"魔药!!!!!!!!!!!!!!!!!!!\n喝魔药!!!!!!!", L"你捡到了一瓶魔药", MB_OK | MB_SETFOREGROUND);
MessageBox(NULL, L"分数减半", L" ", MB_OK | MB_SETFOREGROUND);
p.num = p.num / 2;
map[p.x][p.y - 1] = 0;
}
if (map[p.x][p.y - 1] == 8 && map[p.x][p.y - 2] == 0) {
map[p.x][p.y] = 0;
p.y -= 1;
map[p.x][p.y] = 2;
map[p.x][p.y - 1] = 8;
}
}
if (ch == 77 || ch == 100 || ch == 68) {
if (map[p.x][p.y + 1] == 0) {
map[p.x][p.y] = 0;
p.y++;
map[p.x][p.y] = 2;
}
if (map[p.x][p.y + 1] == 3) {
map[p.x][p.y] = 8;
p.y++;
map[p.x][p.y] = 2;
p.num++;
}
if (map[p.x][p.y + 1] == 4) {
map[p.x][p.y] = 0;
p.y++;
map[p.x][p.y] = 2;
p.num += 4;
}
if (map[p.x][p.y + 1] == 6) {
map[p.x][p.y] = 0;
p.y++;
map[p.x][p.y] = 2;
p.num = 0;
MessageBox(NULL, L"得分清零", L" ", MB_OK | MB_SETFOREGROUND);
}
if (map[p.x][p.y + 1] == 7) {
MessageBox(NULL, L"你撞到了墙", L" ", MB_OK | MB_SETFOREGROUND);
p.f--;
}
if (map[p.x][p.y + 1] == 5) {
MessageBox(NULL, L"魔药!!!!!!!!!!!!!!!!!!!\n喝魔药!!!!!!!", L"你捡到了一瓶魔药", MB_OK | MB_SETFOREGROUND);
MessageBox(NULL, L"分数减半", L" ", MB_OK | MB_SETFOREGROUND);
p.num = p.num / 2;
map[p.x][p.y + 1] = 0;
}
if (map[p.x][p.y + 1] == 8 && map[p.x][p.y + 2] == 0) {
map[p.x][p.y] = 0;
p.y += 1;
map[p.x][p.y] = 2;
map[p.x][p.y + 1] = 8;
}
}
//是否按下认输键/
if (ch == 112) {
mciSendString(TEXT("pause s3"), NULL, 0, NULL);
mciSendString(TEXT("close s3"), NULL, 0, NULL);
MessageBox(NULL, L"你认输了", L"!!!", MB_OK | MB_SETFOREGROUND);
break;
}
/血量是否为零(判断死亡)///
if (p.f == 0) {
mciSendString(TEXT("pause s3"), NULL, 0, NULL);
mciSendString(TEXT("close s3"), NULL, 0, NULL);
MessageBox(NULL, L"你死了!!", L"你死了", MB_OK | MB_SETFOREGROUND);
break;//退出循环
}
gotoxy();
draw();
}
/“逝”后工作 /
die_show(); //显示死亡界面
record(); //记录分数
Play_Again(); //调用重来函数
}
int main() {
system("mode con cols=81 lines=39");
SetConsoleTitle(L"疯狂捡金币小游戏");
SetWindowLong(FindWindow(L"ConsoleWindowClass", NULL), GWL_STYLE, GetWindowLong(FindWindow(L"ConsoleWindowClass", NULL), GWL_STYLE) & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
HSCursor(false);
welcome();
Game();
return 0;
}
/ 游戏界面其他提示显示/
void draw() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
for (int of = 1;of <= WNDLONG;of++) cout << "▁";
cout << "\n\t\t\t墙:■\t 药:♂\t地雷:×\n\t\t\t金币:◎\t\t奖励:★\n";
for (int of = 1;of <= WNDLONG;of++) cout << "▁";
map_draw();
for (int of = 1;of <= WNDLONG;of++) cout << "▁";
cout << endl;
cout << endl;
cout << "\t血量:";//画血条
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
for (int i = 1;i <= p.f;i++)
{
cout << "〓";
}
cout << " \n\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
plan();
for (int ef = 1;ef <= WNDLONG;ef++) cout << "▁";
cout << endl;
}
//显示死亡界面函数
void die_show() {
system("cls");
system("title 你死了!!!!!!!!!!!!!!!!!");
mciSendString(TEXT("open music\\over.mp3 alias s2"), NULL, 0, NULL);
mciSendString(TEXT("play s2"), NULL, 0, NULL);
handle = GetStdHandle(STD_OUTPUT_HANDLE);//显示结束界面
crd.X = 17;crd.Y = 13; SetConsoleCursorPosition(handle, crd);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "▁▁▁▁▁▁▁▁▁GAME(×_×)OVER▁▁▁▁";
crd.X = 17;crd.Y = 15; SetConsoleCursorPosition(handle, crd);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "\t\t\t\b\b\b\b\b\b\b\b\b\b\b你的得分:" << p.num << "\n" << endl;
Sleep(1000);
mciSendString(TEXT("close s2"), NULL, 0, NULL);
}
//记录分数函数
void record() {
ofstream out("./疯狂捡金币得分记录.txt", ios::app);//记录得分
if (out.fail()) {
cout << "出错了\n";
}
if (p.num > 0) {
out << "得分:" << p.num << endl;
out << endl;
out.close();
}
}
/规则打印///
void plan() {
cout << "\t【操作】W键(上) A键(左) S键(下) D键(右)\t或使用方向键\tP键认输\n";
cout << "\t【目标】拿到金币\n";
cout << "\t【拿到奖励会得到更多金币】\n";
cout << "\t【撞到墙会扣血】\n";
}
重点来了:
1.Dev-C++中编写时,请在项目属性-参数-链接中加入-lwinmm
2.音乐文件或存有音乐的文件夹要和cpp(exe)文件放在同一目录下
3.音乐文件的路径要写正确
4.写路径时要用“//”