目录
第一种
第二种
第三种
第四种
第五种
好玩小游戏参考!
代码运行时先把under find删除
因为DEV C++的编译环境较小,所以大部分的游戏代码都无法在此上运行,我收集了一部分摸鱼小游戏的源码,在此呈现,如果有能在DEV C++上运行的我会先作声明:
第一种
经典的简单扫雷小游戏:
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<queue>
#include<ctype.h>
#define A 17 //地图的高
#define B 17 //地图的宽
#define C 30 //雷的总数
using namespace std;
//全局变量
DWORD a,b;
char map[A][B],news,spare;
int BoomTotalNum,floatx,floaty,flag[A][B],flagnum,mode,slect[A][B],game;
//颜色属性
const WORD FORE_BLUE = FOREGROUND_BLUE; //蓝色文本属性
const WORD FORE_GREEN = FOREGROUND_GREEN; //绿色文本属性
const WORD FORE_RED = FOREGROUND_RED; //红色文本属性
//开垦地图结构体
struct node {
int x;
int y;
};
queue <node> dui;
//打印位置
void position(int x,int y) {
COORD pos= {x,y};
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out,pos);
}
//隐藏光标
void Hide() {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
//初始化
void Beginning() {
while(!dui.empty()) {
dui.pop();
}
game=1;
//BoomTotalNum=C;
floatx=A/2;
floaty=B/2;
flagnum=0;
BoomTotalNum=C;
mode=0;
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
int x,y;
srand((unsigned)time(0));
for(int i=0; i<A; i++) for(int j=0; j<B; j++) {
map[i][j]=' ';
flag[i][j]=0;
slect[i][j]=0;
}
while(BoomTotalNum) {
x=rand()%A;
y=rand()%B;
if(map[x][y]==' ') {
map[x][y]='@';
BoomTotalNum--;
}
}
SetConsoleTextAttribute(handle_out, FORE_GREEN);
for(int i=0; i<A; i++) {
for(int j=0; j<B; j++) printf("█");
printf("\n");
}
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_RED);
printf(""); //光标位置
position(44,9);
printf("扫雷模式");
position(44,5);
printf("剩余雷数:%d ",C-flagnum);
SetConsoleTextAttribute(handle_out, FORE_GREEN);
position(5,22);
printf("按“空格”切换模式");
position(5,23);
printf("按“Enter”确认");
position(5,24);
printf("按“方向键”选择方块");
}
//打印地图的一块儿
void Lump(int xx,int yy) {
switch(map[xx][yy]) {
case '1' :
printf("①");
break; //周围雷的数量(下同)
case '2' :
printf("②");
break;
case '3' :
printf("③");
break;
case '4' :
printf("④");
break;
case '5' :
printf("⑤");
break;
case '6' :
printf("⑥");
break;
case '7' :
printf("⑦");
break;
case '8' :
printf("⑧");
break;
case ' ' :
if(xx==floatx&&yy==floaty) {
if(flag[xx][yy]==0) {
if(mode%2==0) printf("");
else printf("");
} else printf("");
} else {
if(flag[xx][yy]==0) printf("█");
else printf("");
}
break;
case '@' :
if(xx==floatx&&yy==floaty) {
if(flag[xx][yy]==0) {
if(mode%2==0) printf("");
else printf("");
} else printf("");
} else {
if(flag[xx][yy]==0) printf("█");
else printf("");
}
break;
case 'x' :
if(floatx==xx&&floaty==yy) printf("");
else printf(" ");
break; //已经挖开的空白
}
}
//移动光标
void Move() {
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
int xxx,yyy;
xxx=floatx;
yyy=floaty;
switch(news) {
case 72 :
floatx--;
break; //上
case 80 :
floatx++;
break; //下
case 75 :
floaty--;
break; //左
case 77 :
floaty++;
break; //右
}
if(floatx==-1) floatx=A-1;
floatx%=A; //两端穿模处理
if(floaty==-1) floaty=B-1;
floaty%=B;
position(yyy*2,xxx);
SetConsoleTextAttribute(handle_out, FORE_GREEN);
Lump(xxx,yyy); //删除原位置
if(map[floatx][floaty]=='x') {
position(floaty*2,floatx);
printf(" ");
}
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
Lump(floatx,floaty); //更新新位置
}
//插旗和排雷模式切换
void Mode() {
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
mode++;
SetConsoleTextAttribute(handle_out, FORE_BLUE);
position(floaty*2,floatx);
if(mode%2==0) printf("");
else printf("");
position(44,9);
if(mode%2==0) {
SetConsoleTextAttribute(handle_out, FORE_BLUE);
printf("扫雷模式");
} else {
SetConsoleTextAttribute(handle_out, FORE_RED);
printf("插旗模式");
}
}
//该点周围地雷数
int Boomnum(int xx,int yy) {
int num=0;
if((xx-1>=0)&&(yy-1>=0)&&(map[xx-1][yy-1]=='@')) num++;
if((xx-1>=0)&&(yy+0>=0)&&(map[xx-1][yy]=='@')) num++;
if((xx-1>=0)&&(yy+1<B) &&(map[xx-1][yy+1]=='@')) num++;
if((xx+0>=0)&&(yy-1>=0)&&(map[xx][yy-1]=='@')) num++;
if((xx+0>=0)&&(yy+1<B) &&(map[xx][yy+1]=='@')) num++;
if((xx+1<A)&&(yy-1>=0) &&(map[xx+1][yy-1]=='@')) num++;
if((xx+1<A)&&(yy+0>=0) &&(map[xx+1][yy]=='@')) num++;
if((xx+1<A)&&(yy+1<B) &&(map[xx+1][yy+1]=='@')) num++;
return num;
}
//更新地图
void Open() {
node c;
node d;
while(!dui.empty()) {
dui.pop();
}
c.x=floatx;
c.y=floaty;
dui.push(c);
slect[c.x][c.y]=1;
while(!dui.empty()) {
c=dui.front();
dui.pop();
if(Boomnum(c.x,c.y)!=0) {
map[c.x][c.y]=(Boomnum(c.x,c.y)+48);
continue;
} else {
map[c.x][c.y]='x';
if((c.x-1>=0)&&(c.y-1>=0)&&(map[c.x-1][c.y-1]==' ')&&(slect[c.x-1][c.y-1]==0)) {
d.x=c.x-1;
d.y=c.y-1;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x-1>=0)&&(c.y-0>=0)&&(map[c.x-1][c.y]==' ')&&(slect[c.x-1][c.y]==0)) {
d.x=c.x-1;
d.y=c.y-0;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x-1>=0)&&(c.y+1<B)&&(map[c.x-1][c.y+1]==' ')&&(slect[c.x-1][c.y+1]==0)) {
d.x=c.x-1;
d.y=c.y+1;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x-0>=0)&&(c.y-1>=0)&&(map[c.x][c.y-1]==' ')&&(slect[c.x][c.y-1]==0)) {
d.x=c.x-0;
d.y=c.y-1;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x-0>=0)&&(c.y+1<B)&&(map[c.x][c.y+1]==' ')&&(slect[c.x][c.y+1]==0)) {
d.x=c.x-0;
d.y=c.y+1;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x+1<A)&&(c.y-1>=0)&&(map[c.x+1][c.y-1]==' ')&&(slect[c.x+1][c.y-1]==0)) {
d.x=c.x+1;
d.y=c.y-1;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x+1<A)&&(c.y-0>=0)&&(map[c.x+1][c.y]==' ')&&(slect[c.x+1][c.y]==0)) {
d.x=c.x+1;
d.y=c.y-0;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x+1<A)&&(c.y+1<B)&&(map[c.x+1][c.y+1]==' ')&&(slect[c.x+1][c.y+1]==0)) {
d.x=c.x+1;
d.y=c.y+1;
dui.push(d);
slect[d.x][d.y]=1;
}
}
}
}
int main() {
freopen("排名.txt","r",stdin);
Relife: //重玩处
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO csbi; //定义窗口缓冲区信息结构体
GetConsoleScreenBufferInfo(handle_out, &csbi); //获得窗口缓冲区信息
Hide();
Beginning();
a=GetTickCount();
while(1) {
if(kbhit()!=0) {
spare=getch();
if((spare!=(-32))&&(spare!=13)&&(spare!=' ')) continue;
if(spare==13) {
;
if(mode%2==0) {
if(map[floatx][floaty]=='@'&&flag[floatx][floaty]==0) {
break;
game=0;
}
if(flag[floatx][floaty]==1) continue;
Open();
position(0,0);
SetConsoleTextAttribute(handle_out, FORE_GREEN);
for(int i=0; i<A; i++) {
for(int j=0; j<B; j++) Lump(i,j);
printf("\n");
}
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
Lump(floatx,floaty);
}
else {
if(map[floatx][floaty]=='x'||(map[floatx][floaty]>'0'&&map[floatx][floaty]<'9'))
continue;
if(flag[floatx][floaty]==0) {
flagnum++;
flag[floatx][floaty]=1;
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
Lump(floatx,floaty);
}
else {
flagnum--;
flag[floatx][floaty]=0;
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
Lump(floatx,floaty);
}
}
}
if(spare==' ') Mode();
//按方向键
if(spare==-32) {
news=getch();
Move();
}
for(int i=0; i<A; i++) for(int j=0; j<B; j++) if(map[i][j]=='x'||(map[i][j]>'0'&&map[i][j]<'9')) game++;
if(game==A*B-C+1) break;
else game=1;
SetConsoleTextAttribute(handle_out, FORE_RED);
position(44,5);
printf("剩余雷数:%d ",C-flagnum);
} else Sleep(10);
b=GetTickCount();
SetConsoleTextAttribute(handle_out, FORE_RED);
position(44,7);
printf("用时:");
if((b-a)/60000<10) printf("0");
printf("%d:",(b-a)/60000);
if(((b-a)/1000)%60<10) printf("0");
printf("%d:",((b-a)/1000)%60);
if(((b-a)/10)%100<10) printf("0");
printf("%d",((b-a)/10)%100);
}
SetConsoleTextAttribute(handle_out, FORE_RED);
position(5,5);
if(game==1) printf("游戏结束!");
else printf("恭喜通关!");
position(5,8);
printf("任意键重玩");
scanf("%c%c",&spare,&spare);
system("cls");
position(0,0);
goto Relife;
}
第二种
高能,实现简单横板射击小游戏
#include <easyx.h>
#include <time.h>
#include <conio.h>
class Bullet;
class Tank;
class E_Bullet;
class Boss;
bool dead = false;
bool wined = false;
struct pos { //坐标类
int a;
int b;
};
class E_Bullet { //敌人打出的子弹
public:
clock_t d;
int x;
int y;
bool on = false;
pos show() { //画出新的位置
setfillcolor(RGB(255, 180, 20));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
return pos { x,y };
}
pos del() { //覆盖原来的位置
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
rectangle(x - 5, y - 5, x + 5, y + 5);
return pos { x,y };
}
pos move() { //左移
x -= 3;
return pos { x,y };
}
};
class Bullet { //玩家打出的子弹,同上
public:
clock_t d;
int x;
int y;
bool on = false;
pos show() {
setfillcolor(RGB(150, 180, 210));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
return pos { x,y };
}
pos del() {
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
rectangle(x - 5, y - 5, x + 5, y + 5);
return pos { x,y };
}
pos move() { //右移
x += 3;
return pos { x,y };
}
};
class Boss { //敌人
public:
bool hurting = false;
clock_t d_hurt;
COLORREF clr = RGB(0, 130, 125);
int x;
int y;
int hp = 100;//生命
clock_t d;//判断举例上一次执行某一函数过了多久
clock_t att_d;
bool angle = false;//方向
pos show() {
setfillcolor(clr);
fillrectangle(x - 20, y - 40, x + 20, y + 40);
return pos { x,y };
}
pos del() {
setfillcolor(0);
setlinecolor(0);
rectangle(x - 20, y - 40, x + 20, y + 40);
fillrectangle(x - 20, y - 40, x + 20, y + 40);
return pos { x,y };
}
void fire(E_Bullet& but) { //攻击
but.on = true;//放置一个子弹
but.x = x - 20;
but.y = y;
but.d = clock();
}
void move() { //上上下下得移动
if (angle == true)
y -= 5;
if (angle == false)
y += 5;
if (y >= 440)
angle = true;
if (y <= 40)
angle = false;
}
void hurt() { //受伤
hp -= 4;
d_hurt = clock();
setfillcolor(0);
setlinecolor(WHITE);
fillrectangle(160, 485, 560, 510);//更新血条
rectangle(160, 485, 160 + hp * 4, 510);
setfillcolor(RGB(230, 0, 1));
setlinecolor(RGB(255, 255, 255));
fillrectangle(160, 485, 160 + hp * 4, 510);
rectangle(160, 485, 160 + hp * 4, 510);
hurting = true;
if (hp <= 0) { //死亡
wined = true;
}
}
};
class Tank { //玩家类,同上
public:
bool hurting = false;
int hp = 100;
int x;
COLORREF clr = RGB(150, 180, 210);
int y;
clock_t d_hurt;
Tank() {}
Tank(int _x, int _y) {
x = _x;
y = _y;
}
Tank operator=(pos p) {
x = p.a;
y = p.a;
}
pos show() {
setfillcolor(clr);
fillrectangle(x - 25, y - 25, x + 25, y + 25);
setfillcolor(RGB(100, 200, 180));
fillrectangle(x, y + 5, x + 40, y - 5);
return pos { x,y };
}
pos del() {
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 25, y - 25, x + 25, y + 25);
rectangle(x - 25, y - 25, x + 25, y + 25);
fillrectangle(x, y + 5, x + 40, y - 5);
rectangle(x, y + 5, x + 40, y - 5);
return pos { x,y };
}
void fire(Bullet& but) {
but.on = true;
but.x = x + 45;
but.y = y;
but.d = clock();
but.show();
}
void hurt() {
hp -= 2;
d_hurt = clock();
setfillcolor(0);
setlinecolor(WHITE);
fillrectangle(160, 515, 560, 540);
rectangle(160, 515, 560, 540);
rectangle(160, 515, 160 + hp * 4, 540);
setfillcolor(RGB(0, 255, 1));
setlinecolor(RGB(255, 255, 255));
fillrectangle(160, 515, 160 + hp * 4, 540);
rectangle(160, 515, 160 + hp * 4, 540);
hurting = true;
if (hp <= 0)
dead = true;
}
};
#define BT_MAX 8
int main() {
initgraph(640, 550, 4);//初始化屏幕
settextcolor(RGB(0, 254, 0));
settextstyle(35, 0, _T("黑体"));
outtextxy(150, 200, _T("W,S移动,K攻击"));
Sleep(3000);
setlinecolor(0);
setfillcolor(0);
rectangle(0, 0, 640, 550);
fillrectangle(0, 0, 640, 550);
setlinecolor(RGB(255, 255, 255));
setfillcolor(RGB(255, 255, 255));
clock_t delay = clock();//玩家移动的延时
clock_t d_f = clock();//玩家开火的延时
line(0, 481, 640, 481);//分割画面与血条
Bullet bt[BT_MAX];//玩家的子弹
Tank tk(30, 30);//玩家
Boss bo;//敌人
bo.x = 580;
bo.y = 240;
E_Bullet ebt[BT_MAX];//敌人的子弹
bo.d = clock();//初始化延时
bo.att_d = clock();
tk.show();
settextstyle(20, 0, _T("黑体"));
outtextxy(10, 485, _T("BOSS的生命值:"));
setfillcolor(RGB(230, 0, 1));
fillrectangle(160, 485, 560, 510);//敌人血条
outtextxy(10, 520, _T("玩家的生命值:"));
setfillcolor(RGB(0, 255, 1));
fillrectangle(160, 515, 560, 540);//玩家血条
while (1) { //主循环
if (wined || dead)//玩家死了或者敌人死了
break;
if (GetAsyncKeyState('W') & 0x8000) { //玩家移动
if (tk.y > 28 && (clock() - delay) >= 40) {
tk.del();
tk.y -= 3;
tk.show();
delay = clock();
}
}
if (GetAsyncKeyState('w') & 0x8000) { //玩家移动
if (tk.y > 28 && (clock() - delay) >= 40) {
tk.del();
tk.y -= 3;
tk.show();
delay = clock();
}
}
if (GetAsyncKeyState('k') & 0x8000) { //玩家开火
for (int i = 0; i < BT_MAX; i++) {
if (bt[i].on == false && (clock() - d_f) > 800) {
bt[i].on = true;
tk.fire(bt[i]);
d_f = clock();
break;
}
}
}
if (GetAsyncKeyState('K') & 0x8000) { //玩家开火
for (int i = 0; i < BT_MAX; i++) {
if (bt[i].on == false && (clock() - d_f) > 800) {
tk.fire(bt[i]);
d_f = clock();
break;
}
}
}
if (GetAsyncKeyState('S') & 0x8000) { //玩家移动
if (tk.y < 452 && (clock() - delay) >= 40) {
tk.del();
tk.y += 3;
tk.show();
delay = clock();
}
}
if (GetAsyncKeyState('s') & 0x8000)//玩家移动
if (tk.y < 452 && (clock() - delay) >= 40) {
tk.del();
tk.y += 3;
tk.show();
delay = clock();
}
for (int i = 0; i < BT_MAX; i++) { //遍历子弹,使子弹刷新
if (bt[i].on == true && (clock() - bt[i].d) > 20) {
bt[i].del();
bt[i].move();
bt[i].show();
bt[i].d = clock();
if (bt[i].x >= 635)
bt[i].on = false, bt[i].del();//到达了屏幕最右端
if ((bt[i].x + 5 >= bo.x - 20 && bt[i].x - 5 <= bo.x + 20) && (bt[i].y - 5 < bo.y + 40 && bt[i].y + 5 > bo.y - 40))
//击中敌人
bt[i].on = false, bo.hurt(), bt[i].del();
}
}
if (clock() - bo.att_d > 700) { //敌人自动开火
for (int i = 0; i < BT_MAX; i++) {
if (ebt[i].on == false) {
bo.fire(ebt[i]);
break;
}
}
bo.att_d = clock();
}
for (int i = 0; i < BT_MAX; i++) { //敌人子弹刷新,同上
if (ebt[i].on == true && (clock() - ebt[i].d > 20)) {
ebt[i].del();
ebt[i].move();
ebt[i].show();
ebt[i].d = clock();
if (ebt[i].x < 5)
ebt[i].del(), ebt[i].on = false;
if (ebt[i].x - 5 < tk.x + 25 && ebt[i].x + 5 > tk.x - 25 && ebt[i].y - 5 < tk.y + 25 && ebt[i].y + 5 > tk.y - 25) {
ebt[i].on = false, tk.hurt(), ebt[i].del();
}
}
}
if (tk.hurting == true)//玩家受伤闪烁0.1秒
if (clock() - tk.d_hurt > 100) {
tk.clr = RGB(150, 180, 210), tk.show(), tk.hurting = false;
} else
tk.clr = RGB(255, 0, 0), tk.show();
if (bo.hurting == true)//敌人受伤闪烁0.1秒
if (clock() - bo.d_hurt > 100) {
bo.clr = RGB(0, 130, 125), bo.show(), bo.hurting = false;
} else
bo.clr = RGB(0, 255, 0), bo.show();
if (clock() - bo.d > 50)//敌人移动延时;
bo.del(), bo.move(), bo.show(), bo.d = clock();
}
if (wined) { //胜负已分
settextcolor(RGB(0, 254, 0));
settextstyle(35, 0, _T("黑体"));
outtextxy(150, 200, _T("你打败了boss!你赢了!!"));
} else {
settextcolor(RGB(254, 0, 0));
settextstyle(35, 0, _T("黑体"));
outtextxy(140, 200, _T("你被boss打败了!"));
}
Sleep(5000);
closegraph();
return 0;
}
第三种
70行代码实现石头剪刀布小游戏:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain() {
chargamer;// 玩家出拳
intcomputer;// 电脑出拳
intresult;// 比赛结果
// 为了避免玩一次游戏就退出程序,可以将代码放在循环中
while(1) {
printf("这是一个猜拳的小游戏,请输入你要出的拳头:\n");
printf("A:剪刀\nB:石头\nC:布\nD:不玩了\n");
scanf("%c%*c",&gamer);
switch(gamer) {
case65://A
case97://a
gamer=4;
break;
case66://B
case98://b
gamer=7;
break;
case67://C
case99://c
gamer=10;
break;
case68://D
case100://d
return0;
default:
printf("你的选择为 %c 选择错误,退出...\n",gamer);
getchar();
system("cls");// 清屏
return0;
break;
}
srand((unsigned)time(NULL));// 随机数种子
computer=rand()%3;// 产生随机数并取余,得到电脑出拳
result=(int)gamer+computer;// gamer 为 char 类型,数学运算时要强制转换类型
printf("电脑出了");
switch(computer) {
case0:
printf("剪刀\n");
break;//4 1
case1:
printf("石头\n");
break;//7 2
case2:
printf("布\n");
break;//10 3
}
printf("你出了");
switch(gamer) {
case4:
printf("剪刀\n");
break;
case7:
printf("石头\n");
break;
case10:
printf("布\n");
break;
}
if(result==6||result==7||result==11)printf("你赢了!");
elseif(result==5||result==9||result==10)printf("电脑赢了!");
elseprintf("平手");
system("pause>nul&&cls");// 暂停并清屏
}
return0;
}
第四种
来自大佬的代码,竟可以在DEV C++上运行!!
实现修改版狼人杀:
#include <iostream>//C++输入输出流库
#include <cstdlib>//使用srand函数要用到这个库
#include <ctime>//使用time函数要用到这个库
#include <Windows.h>
#include <conio.h>
long long sr=0;
using namespace std;
void brc()
{
system("cls");
long long jy=1,wd=3;
long long sy=3,wj=3;
srand((int)time(0));
long long n=rand()%15+1;
cout<<" 你是"<<n<<"号"<<endl;
cout<<" 正在分配身份"<<endl;
Sleep(1500);
long long m[20];
for(long long i=1;i<=15;i++)
m[i]=1;
long long s[20]={0};
long long yy[20]={0};
long long l=0,w=0,j=0,p=0;
for(long long i=1;i<=15;i++)
{
do
{
long long a=rand()%4+1;
if(a==1&&l<1)
{
l++;
s[i]=1;
sr=i;
}
else if(a==2&&w<2)
{
w++;
s[i]=2;
}
else if(a==4&&p<3)
{
p++;
s[i]=4;
}
else if(a==3&&j<9
)
{
j++;
s[i]=3;
}
}
while(s[i]==0);
}
if(s[n]==1)
cout<<" 你是>>杀手<<"<<endl;
else if(s[n]==2)
cout<<" 你是>>预言家<<"<<endl;
else if(s[n]==3)
cout<<" 你是>>平民<<"<<endl;
else
cout<<" 你是>>法师<<"<<endl;
cout<<" 游戏将在5秒后开始"<<endl;
Sleep(5000);
long long c=15;
long long f=0,flag=0;
long long day=1;
long long lr;
long long tp[20]={0};
do
{
lr=0;
system("cls");
cout<<" 第"<<day<<"晚开始"<<endl;
cout<<"你是"<<n<<"号"<<endl;
long long x;
for(long long i=1;i<=15;i++)
{
if(i==n&&m[n]==1)
{
if(s[n]==1)
{
cout<<" 你是杀手"<<endl;
for(long long j=1;j<=15;j++)
if(m[j]==1&&s[j]!=1)
cout<<j<<" ";
cout<<endl;
cout<<" 你选择杀掉"<<endl;
long long a;
cin>>a;
m[a]=0;
cout<<endl;
cout<<" 杀手杀了"<<a<<"号";
if(s[a]==1)
cout<<"(杀手)"<<endl;
else if(s[a]==2)
cout<<"(预言家)"<<endl;
else if(s[a]==3)
cout<<"(平民)"<<endl;
else
cout<<"(法师)"<<endl;
c--;
}
else
if(s[n]==2)
{
cout<<" 你是预言家"<<endl;
cout<<" ";
for(long long j=1;j<=15;j++)
if(m[j]==1)
cout<<j<<" ";
cout<<endl<<" 你选择预言谁的身份"<<endl;
long long a;
cin>>a;
if(s[a]==1)
{
cout<<" "<<a<<"号是杀手,请注意"<<endl;
Sleep(1000);
}
else
cout<<" "<<a<<"号是好人"<<endl;
}
}
else if(s[i]==1&&m[i]==1)
{
long long a;
do
{
a=rand()%15+1;
}
while(a==i||m[a]==0||s[a]==1);
cout<<" 杀手杀掉了"<<a<<"号";
if(s[a]==1)
cout<<"(杀手)"<<endl;
else if(s[a]==2)
cout<<"(预言家)"<<endl;
else if(s[a]==3)
cout<<"(平民)"<<endl;
else
cout<<"(法师)"<<endl;
m[a]=0;
c--;
break;//
}
else if(s[i]==2)
{
if(flag==0||m[flag]==0)
{
long long a;
do
{
a=rand()%15+1;
}
while(a==i||m[a]==0);
if(s[a]==1)
yy[a]=1;
else if(s[a]==3)
yy[a]=3;
}
}
else if(s[i]==4)
{
f=0;
while(f==0)
{
long long a;
a=rand()%3+1;
if(a==3)break;
if(a==1)
{
if(sy>0)
{
sy--;
f=1;
long long b;
do
{
b=rand()%15+1;
b=rand()%15+1;
}
while(b==i||m[b]==0);
m[b]=0;
cout<<" 法师毒死了"<<b<<"号";
if(s[b]==1)
cout<<"(杀手)"<<endl;
else if(s[b]==2)
cout<<"(预言家)"<<endl;
else if(s[b]==3)
cout<<"(平民)"<<endl;
c--;
if(s[b]==1&&n!=sr)
{
cout<<endl<<" "<<sr<<"号是杀手!";
cout<<endl<<" 杀手已死亡!"<<endl;
cout<<" 游戏结束"<<endl;
return ;
}
break;//
}
}
else if(a==2&&day>1)
{
if(jy>0)
{
jy--;
f=1;
long long b;
do
{
b=rand()%15+1;
}
while(b==i||m[b]==1);
c++;
cout<<" 法师复活了"<<b<<"号"<<endl;
m[b]=1;
if(m[sr]==0&&n!=sr)
{
cout<<endl<<" "<<sr<<"号是杀手!";
cout<<endl<<" 杀手已死亡!"<<endl;
cout<<" 游戏结束"<<endl;
return ;
}
break;//
}
}
}
}
}
if(s[n]==4)
{
cout<<" 你是法师"<<endl;
cout<<"存活状况:"<<endl;
for(long long j=1;j<=15;j++)
if(m[j]==1)
cout<<j<<" ";
cout<<endl;
cout<<" 你选择 1.* 杀 *还是 2.* 救 * 3.* 啥都不做 *"<<endl;
long long a,f=0;
while(f==0)
{
cin>>a;
if(a==3) break;
if(a==1)
{
if(wd>0)
{
wd--;
f=1;
cout<<" 你选择了杀人"<<endl;
for(long long i=1;i<=15;i++)
if(m[i]==1)
cout<<i<<" ";
cout<<endl;
long long b;
cin>>b;
if(s[b]==1)
cout<<"(杀手)"<<endl;
else if(s[b]==2)
cout<<"(预言家)"<<endl;
else if(s[b]==3)
cout<<"(平民)"<<endl;
else
cout<<"(法师)"<<endl;
m[b]=0;
c--;
if(m[sr]==0&&n!=sr)
{
cout<<endl<<" "<<sr<<"号是杀手!";
cout<<endl<<" 杀手已死亡!"<<endl;
cout<<" 游戏结束"<<endl;
return ;
}
}
else
{
cout<<">>毒药<<不足"<<endl;
}
}
else if(a==2)
{
if(wj>0)
{
wj--;
f=1;
cout<<"你选择了复活"<<endl;
cout<<"阵亡名单:"<<endl;
for(long long i=1;i<=15;i++)
if(m[i]==0)
cout<<i<<"号,身份:"<<s[i]<<" "<<endl;;
long long b;
cin>>b;
m[b]=1;
c++;
}
else
{
cout<<">>复活药水<<不足"<<endl;
}
}
}
}
Sleep(1000);
long double p[20]={0};
cout<<" 第"<<day<<"晚结束"<<endl;
cout<<endl;
if(m[n]==0)
{
cout<<" 你已经>>死<<了";
break;
}
cout<<" 存活:"<<endl;
cout<<" ";
for(long long i=1;i<=15;i++)
if(m[i]==1)
cout<<i<<" ";
cout<<endl;
cout<<" 请投票...."<<endl;
for(long long i=1;i<=15;i++)
{
if(i==n&&m[n]==1)
{
cout<<endl;
cout<<" 你选择投几号"<<endl;
long long a=99;
while(a==99)
{
cin>>a;
if(a==99)
for(long long i=1;i<=15;i++)
if(m[i]==1)
cout<<" "<<i<<"."<<s[i]<<endl;
}
if(s[i]==3&&day>4)
p[a]+=1.5;
else
p[a]++;
cout<<endl;
cout<<" "<<i<<"->"<<a<<endl;
if(s[a]==3)
{
tp[a]=i;
}
}
else if(s[i]==1&&m[i]==1)
{
long long a;
do
{
a=rand()%15+1;
}
while(m[a]==0||a==i||s[a]==1);
p[a]++;
cout<<" "<<i<<"->"<<a<<endl;
if(s[a]==3)
{
tp[a]=i;
}
}
else if(s[i]==2&&m[i]==1)
{
if(flag!=0)
{
p[f]++;
cout<<" "<<i<<"->"<<flag<<endl;
}
else
{
long long a;
do
{
a=rand()%15+1;
}
while(m[a]==0||a==i||yy[a]==3);
p[a]++;
if(s[a]==3)
{
tp[a]=i;
}
cout<<" "<<i<<"->"<<a<<endl;
}
}
else if(s[i]==3&&m[i]==1)
{
if(tp[i]==0)
{
long long a;
do
{
a=rand()%15+1;
}
while(m[a]==0||a==i);
p[a]++;
cout<<" "<<i<<"->"<<a<<endl;
}
else
{
if(m[tp[i]]==1)
{
p[tp[i]]++;
cout<<" "<<i<<"->"<<tp[i]<<endl;
}
else
{
long long a;
do
{
a=rand()%15+1;
}
while(m[a]==0||a==i);
p[a]++;
cout<<" "<<i<<"->"<<a<<endl;
}
}
}
else if(s[i]==4&&m[i]==1)
{
long long a;
do
{
a=rand()%15+1;
}
while(m[a]==0||a==i);
p[a]++;
cout<<" "<<i<<"->"<<a<<endl;
}
}
system("cls");
cout<<" 投票情况:"<<endl;
for(long long i=1;i<=15;i++)
if(m[i]==1)
cout<<" "<<i<<"号"<<" 票数:"<<p[i]<<endl;
long long sw,max=-100;
for(long long i=1;i<=15;i++)
{
if(p[i]>max)
{
sw=i;
max=p[i];
}
}
m[sw]=0;
cout<<" "<<sw<<"死了"<<endl;
c--;
cout<<" "<<sw<<"号的身份是";
if(s[sw]==1)
cout<<"杀手"<<endl;
else if(s[sw]==2)
cout<<"预言家"<<endl;
else if(s[sw]==3)
cout<<"平民"<<endl;
else
cout<<"法师"<<endl;
if(s[sw]==1&&n!=sr)
{
cout<<endl<<" "<<sr<<"号是杀手!";
cout<<endl<<" 杀手已死亡!"<<endl;
cout<<" 游戏结束"<<endl;
return ;
}
day++;
if(s[n]!=1)
{
for(long long i=1;i<=15;i++)
if(s[i]==1&&m[i]==1)
lr=1;
}
else
{
if(s[n]==1&&c==2)
lr=0;
}
system("pause");
cout<<endl;
if(m[sr]==0&&n!=sr)
{
cout<<endl<<" "<<sr<<"号是杀手!"<<endl;
cout<<endl<<" 杀手已死亡!"<<endl;
cout<<" 游戏结束"<<endl;
return ;
}
}
while(m[n]==1&&c>1);
if(sr==n&&m[n]==1)
{
cout<<" 你杀掉了所有人!!!"<<endl;
}
cout<<" 游戏结束"<<endl;
return ;
}
int main()
{
srand((int)time(0));
char a='1';
while(1)
{
system("cls");
cout<<" 杀手游戏"<<endl;
cout<<"------------------------------------------------------------------------------------------------------------------------";
cout<<" 1.开始游戏"<<endl;
cout<<" 2.查看游戏规则"<<endl;
a=getch();
if(a=='1')
{
cout<<" 1. 15人场"<<endl;
cout<<" 2. 30人娱乐战(10猎人)<敬请期待>"<<endl;
a='2';
while(a=='2')
{
a=getch();
switch(a)
{
case '1':
brc();
break;
}
if(a=='1')
break;
}
if(a=='1')
break;
}
else if(a=='2')
{
cout<<"杀手:每当晚上的时候可使用杀人权杀掉一人"<<endl;//1
cout<<"预言家:每天晚上可以知道一个人的身份"<<endl;//2
cout<<"平民:无技能,当玩家是平民时,在第5天拥有1.5票的投票权"<<endl;//3
cout<<"猎人:此身份仅限于30人娱乐局中,死亡后可带走一人"<<endl;
cout<<"投票细则:\n(游戏中除了你以外全是由超级AI人工代替)"<<endl;
cout<<"预言家如果预言到杀手将一直对他投票,如果预言到好人将永远不会对他投票"<<endl;
cout<<"平民将投上一个晚上对他投票他的人"<<endl;
cout<<"********利用好超级AI游戏规则找出杀手**********"<<endl;
system("pause");
}
}
return 0;
}
第五种
实现简单的打字练习游戏:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h> //非标准库函数 Sleep 所需的头文件
#include <conio.h> //非标准库函数 kbhit 和 getch 所需的头文件
using namespace std;
int main() {
const int ESCKEY = 27; //定义整型常量ESCKEY,赋值为ESC键的ASCII编码
int pos, posmax = 72; //当前位置,右边界最大位置
int win = 0, loss = 0; //已赢局数和已输局数
cout << "打字游戏(按回车键开始,游戏过程中按 ESC 结束)" << endl;
getchar();
int ch = 0, key = 0; //随机字符,玩家键入字符
srand(time(0)); //设置随机数种子
while (key != ESCKEY) { //主循环。key 不是 ESC键时玩游戏
ch = rand() % ('z' + 1 - 'a') + 'a'; //产生随机字符
for (pos = 0; pos < posmax && key != 27; pos++) {
cout << "\b--" << (char)ch;
pos++;
Sleep(150);
if (kbhit() && (key = getch()) == ch) {
win++;
cout << "*\a"; //输出 * 并响铃
break;
}
}
if (key == ESCKEY) { //用户键入ESC键,需要确认
cout << "\n结束游戏吗?(y/n)";
while ((key = tolower(getchar())) != 'y' && ch != 'n')
; //空循环体
key = (key == 'y' ? ESCKEY : 0); //如果为'y'则改为 ESCKEY,用于控制主循环
}
if (pos >= posmax) { //到达右边界
loss ++;
}
cout << endl;
}
cout << "字符练习个数:" << win + loss << " 正确键入个数:" << win << endl;
cout << "游戏结束,谢谢使用" << endl;
return 0;
}
更多推荐
C++的经典小游戏
发布评论