资深守护
下落的小鸟:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
/********函数变量声明********/
#define PR_Box printf("■")
#define PR_Gold printf("★")
#define PR_Ag printf("☆")
#define PR_FBird printf("○")
#define PR_DBird printf("Ф")
#define PR_Land printf("┳┳┯")
#define PR_Bg_TL printf("╔")
#define PR_Bg_TR printf("╗")
#define PR_Bg_DL printf("╚")
#define PR_Bg_DR printf("╝")
#define PR_Bg_X printf("═")
#define PR_Bg_Y printf("║")
#define PR_Blank printf(" ");
int Grade = 1, C_Gold = 0, C_Ag = 0, Score = 0, Delay_time = 1000, Max_blank = 9, Distance = 18;
struct Birds
{
int x, y;
int condition;
};
Birds *Bird = (Birds*)malloc(sizeof(Birds));
struct Bg
{
int x, y;
int l_blank;
int reward[9];
Bg *pri;
Bg *next;
};
Bg *Bg1 = new Bg[sizeof(Bg)];
void Position(int x, int y)
{
COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void CreatBird()
{
Bird->x = 41;
Bird->y = 10;
Bird->condition = 0;
}
void CreatBg()
{
Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8;
Bg2->x = Bg1->x + Distance; Bg2->y = 9;
Bg1->l_blank = Max_blank - Grade;
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2;
Bg1->pri = Bg2;
Bg2->next = Bg1;
Bg2->pri = Bg1;
}
void InsertBg(Bg *p)
{
int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0));
temp = rand();
if (temp % 2 == 0)//++
{
if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else
Bgs->y = p->pri->y;
}
else
{
if ((p->pri->y - temp % 4)>2)
Bgs->y = p->pri->y - temp % 4;
else
Bgs->y = p->pri->y;
}
Bgs->pri = p->pri;
Bgs->next = p;
p->pri->next = Bgs;
p->pri = Bgs;
}
void Check_Bg(Bg *q)
{
Bg *p = q; int i = 0, temp;
while (++i <= 5)
{
if (p->x>-4)
p = p->next;
else
{
srand((int)time(0));
temp = rand();
if (temp % 2 == 0)//++
{
if ((temp % 4 + p->y + Max_blank - Grade)<21)
p->y = p->y + temp % 4;
else
p->y = p->y;
p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{
if ((p->y - temp % 4)>2)
p->y = p->y - temp % 4;
else
p->y = p->y;
p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}
void Loop_Bg(Bg *q)
{
Bg *p = q; int i = 0;
while (++i <= 5)
{
p->x = p->x - 1;
p = p->next;
if (Bird->x == p->x)
{
Score += 1;
if (Score % 4 == 0 && Grade<4)
Grade++;
}
}
}
void Prt_Bg(Bg *q)
{
Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{
if (p->x>0 && p->x <= 78)
{
for (k = 2; k<p->y; k++)
{
Position(p->x + 1, k);
PR_Box; PR_Box; PR_Blank
}
Position(p->x, p->y);
PR_Box; PR_Box; PR_Box; PR_Blank;
Position(p->x, p->y + p->l_blank);
PR_Box; PR_Box; PR_Box; PR_Blank;
k = k + p->l_blank + 1;
for (k; k <= 22; k++)
{
Position(p->x + 1, k);
PR_Box; PR_Box; PR_Blank;
}
Position(p->x, 23);
for (k = 1; k<Distance / 3 - 2; k++)
PR_Land;
}
p = p->next;
if (p->x == 0)
{
for (j = 2; j<p->y; j++)
{
Position(p->x + 1, j);
PR_Blank; PR_Blank;
}
Position(p->x + 1, p->y);
PR_Blank; PR_Blank; PR_Blank;
Position(p->x + 1, p->y + Max_blank - Grade);
PR_Blank; PR_Blank; PR_Blank;
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++)
{
Position(p->x + 1, j);
PR_Blank; PR_Blank;
}
}
}
}
void PrtBg()
{
int i;
Position(1, 1); PR_Bg_TL;
Position(79, 1); PR_Bg_TR;
Position(1, 24); PR_Bg_DL;
Position(79, 24); PR_Bg_DR;
for (i = 3; i <= 78; i += 2)
{
Position(i, 1); PR_Bg_X;
Position(i, 24); PR_Bg_X;
}
/*for(i=2;i<=23;i++)
{ Position(1,i);PR_Bg_Y;printf("%d",i-1);
Position(79,i);PR_Bg_Y;
}*/
}
void PrtBird()
{
Position(Bird->x, Bird->y - 1);
PR_Blank;
Position(Bird->x, Bird->y);
PR_FBird;
Position(38, 2);
printf("分数:%d", Score);
}
int CheckYN(Bg *q)
{
Bg *p = q; int i = 0;
while (++i <= 5)
{
if (Bird->y>23)
return 0;
if (Bird->x == p->x&&Bird->y <= p->y)
return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)
return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)
return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}
void Prtfirst()
{
printf("══════════════════════════════════════\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■ C++语言版 Flappy Bird\n");
printf(" ■■ ■■ 瞎搞人:欧阳语聪\n");
printf(" ■■ ■■ 瞎搞日期:2014.2\n");
printf(" ■■ ■■ 耗时:4小时\n");
printf(" ■■■ ■■ 游戏说明:\n");
printf(" ■■ 1-按上箭头使鸟起飞\n");
printf(" ■■ 2-等级越高,难度越大!\n");
printf(" Ю123 ■■■\n");
printf("\n");
printf(" ■■■ 欢迎各路大佬与我探讨哦!\nC++、VB、PHP、C#\n");
printf(" ■■\n");
printf(" ■■\n");
printf(" ■■ ■■■ \n");
printf(" ■■ ■■\n");
printf(" ■■ Ф ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳\n");
system("pause");
Position(1, 1);
int i = 0;
while (i++<40 * 25)
PR_Blank;
}
int main()
{
int i = 0; char ch;
Prtfirst();
PrtBg();
CreatBg();
InsertBg(Bg1);
InsertBg(Bg1);
InsertBg(Bg1);
CreatBird();
while (1)
{
if (!CheckYN(Bg1))
break;
Check_Bg(Bg1);
Prt_Bg(Bg1);
PrtBird();
Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP))
{
Position(Bird->x, Bird->y - 1);
PR_Blank;
Bird->y = Bird->y - 4;
}
while (i++<500);
{
Sleep(100);
}
i = 0;
}
Position(38, 10);
printf("You Lost!");
Position(1, 25);
system("pause");
return 0;
}
贪吃蛇:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
typedef struct snake
{
int a;
int b;
struct snake *u;
struct snake *n;
}snake,*snake1;
typedef struct food
{
int a;
int b;
}food;
int main()
{
char c,c0 = 'd';
int i,j,k,n=1,t,at;
snake p,q;
snake *dd,*dd0,*dd1,*dd2;
food f;
srand(time(NULL));
p.u = NULL;
p.n = &q;
p.a = 5;p.b = 6;q.a = 5;q.b = 5;
q.u = &p;q.n = NULL;
dd=dd2= &q;
f.a=(rand()%15+1);
f.b=(rand()%15+1);
while(1)
{
srand(time(NULL));
system("cls");
for(i = 0;i < 17;i ++)
{
for(j = 0; j < 17;j++)
{
if(i == 0 )
printf("▁");
else if(i == 16)
printf("▔");
else if(j == 0)
printf("▕");
else if(j == 16)
printf("▏");
else if(i == p.a && j == p.b)
printf("■");
else if(i == f.a && j == f.b)
printf("★");
else
{
t = 0;
dd = dd2;
for(k = 0; k < n ;k++)
{
if(i == dd->a && j == dd->b)
{
printf("□");
t = 1;
break;
}
dd = dd->u;
}
if(t == 0)
printf(" ");
}
}printf("\n");
}
at = 0;
dd =dd2;
for(i=0;i<n;i++)
{
if(p.a == dd->a && p.b == dd->b)
{
printf("game over!!\n");
exit(0);
}
dd = dd->u;
}
if(p.a == f.a && p.b == f.b)
{
dd = dd2;
at =1;
f.a = (rand()%15+1);
f.b = (rand()%15+1);
for(i=0;i<n;i++)
{
if(f.a == dd->a && f.b == dd->b)
{
f.a = dd2->a;
f.b = dd2->b;
break;
}
}
n++;
}
if(kbhit())
{
c = getch();
dd = dd2;
if(c == 'w' && c0 != 's')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.a == 1)
p.a = 15;
else
p.a = (p.a-1)%15;
}
else if(c == 's' && c0 != 'w')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.a = (p.a%15)+1;
}
else if(c == 'a' && c0 != 'd')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.b == 1)
p.b = 15;
else
p.b = (p.b-1)%15;
}
else if(c == 'd' && c0 != 'a')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.b = (p.b%15)+1;
}
else
{
goto qq;
}
c0 = c;
}
else
{
qq: if(c0 == 'w')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.a == 1)
p.a = 15;
else
p.a=(p.a-1)%15;
}
else if(c0 == 's')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.a=(p.a%15)+1;
}
else if(c0 == 'a')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.b == 1)
p.b = 15;
else
p.b=(p.b-1)%15;
}
else if(c0 == 'd')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.b=(p.b%15)+1;
}
}
fflush(stdin);
dd = &q;
_sleep(200);
}
}
欧阳语聪在2020-01-13 15:49:16追加了内容
有两个哦
高级守护
#include<stdio.h>
#include<Windows.h>
#define YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define CYAN FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define ORANGE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define PURPLE FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY
using namespace std;
const int STARTX = 30;
const int STARTY = 6;
const int MAXX = 10;//雷区的宽
const int MAXY = 10;//雷区的高
const int BOMBNUMBER = 10;//地雷数量
class Cube{
private:
bool ifHaveBomb;//该方块是否含有炸弹
bool ifOpen;//该方块有无被玩家翻开
int nearBombNumber;//该区块周围8格的含有炸弹的方块的数量
public:
void setOpen() {
//将Open的值改为true
ifOpen = true;
}
bool getOpen() {
//获取ifOpen的值
return ifOpen;
}
void setNearBombNumber(int number) {
//给nearBombNumber赋值
nearBombNumber = number;
}
void haveBomb() {
//给方块放置地雷
ifHaveBomb = true;
}
bool getIfHaveBomb() {
//获取ifHaveBomb的值
return ifHaveBomb;
}
int getNearBombNumber() {
//获取nearBombNumber的值
return nearBombNumber;
}
void resetCube(bool ifhavebomb = false, bool ifopen = false, int nearbombnumber = 0){
//初始化成员数据
ifHaveBomb = ifhavebomb;
ifOpen = ifopen;
nearBombNumber = nearbombnumber;
}
};
Cube cube[MAXX][MAXY];
void GoTo(int x, int y);//定位光标
void setBomb(int bombNumber);//生成bombNumber个炸弹并且放进随机的方块中
void show();//显示地雷阵
int checkAndSetNearBombNumber(int x, int y);//检查当前方块周围的雷数量
void gameStart();//初始化游戏
void showXY();//显示雷区坐标
bool player(bool &life);//玩家输入坐标翻开方块
void message(bool life);//玩家游戏结束后输出的信息
void autoOpen(int x,int y);//玩家翻开的方块为不含雷且周围无雷的方块时,自动翻开周围无雷的方块
bool ifWin();//判断玩家是否扫雷成功
void showBomb();//游戏结束后显示地雷位置
int main() {
gameStart();
show();
bool life = true, win = true;
while (player(life) && !ifWin()) {
}
message(life && ifWin());
return 0;
}
void GoTo(int x, int y) {
//定位光标
COORD coord = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void setBomb(int bombNumber = BOMBNUMBER) {
//生成bombNumber个炸弹并且放进随机的方块中
srand((unsigned)GetCurrentTime());
while (bombNumber--) {
int x = MAXX + 1, y = MAXY + 1;
while ((x >= MAXX || y >= MAXY) || cube[x][y].getIfHaveBomb() == true) {
x = rand() % MAXX;
y = rand() % MAXY;
}
cube[x][y].haveBomb();
}
}
void show() {
//显示地雷阵
system("cls");
showXY();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CYAN);
for (int i = 0;i < MAXY;i++) {
GoTo(STARTX, STARTY + i);
for (int j = 0;j < MAXX;j++) {
if (cube[j][i].getOpen() == true) {
if (cube[j][i].getIfHaveBomb() == false) {
if (cube[j][i].getNearBombNumber() == 0) { //挖开无雷的方块显示该方块周围多少个方块含雷,若为0则显示空格
printf(" ");
} else {
printf(" %d", cube[j][i].getNearBombNumber());
}
} else {
printf("×");//有雷的方块被挖开后显示×
}
} else {
printf("■");//未翻开的方块用■显示
}
}
}
}
void showXY() {
//显示坐标轴
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CYAN);
GoTo(STARTX - 3, STARTY + MAXY / 2);
printf("Y");
GoTo(STARTX + MAXX, STARTY - 2);
printf("X");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);
for (int i = 0;i < MAXY;i++) {
GoTo(STARTX - 1, STARTY + i);
printf("%d ", i);
}
for (int i = 0;i < 2 * MAXX;i += 2) {
GoTo(STARTX + i + 1, STARTY - 1);
printf("%d ", i / 2);
}
}
int checkAndSetNearBombNumber(int x, int y) {
//检查当前方块周围的雷数量
int num = 0;
if (cube[x][y].getIfHaveBomb() == true) {
//若该方块有地雷,则不用判断它周围有几个雷
return 0;
} else {
//用两个循环当前方块周围8格扫一遍
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int nx = x + i;
int ny = y + j;
if (!(ny == y && nx == x) && (nx >= 0 && nx <= MAXX - 1) &&
(ny >= 0 && ny <= MAXY - 1)) {
if (cube[nx][ny].getIfHaveBomb()) {
num++;
}
}
}
}
cube[x][y].setNearBombNumber(num);//设置该方块附近的地雷的数量
return 0;
}
}
void gameStart() {
//初始化游戏
for (int i = 0;i < MAXY;i++) {
for (int j = 0;j < MAXX;j++) {
cube[j][i].resetCube();
}
}
setBomb();
for (int i = 0;i < MAXY;i++) {
for (int j = 0;j < MAXX;j++) {
checkAndSetNearBombNumber(j, i);
}
}
}
bool player(bool &life) {
//玩家输入坐标翻开方块
int x, y;
GoTo(STARTX - 3, STARTY + MAXY + 1);
printf("请输入坐标(x,y),x和y用空格隔开");
GoTo(STARTX + MAXX / 2, STARTY + MAXY + 2);
scanf("%d%d", &x, &y);
if ((x < 0) || (x > MAXX - 1) || (y < 0) || (y > MAXY - 1)) {
//当玩家输入的坐标超出范围时
show();
GoTo(STARTX - 3, STARTY + MAXY + 3);
printf("该坐标不存在,请重新输入坐标");
GoTo(STARTX + MAXX / 2, STARTY + MAXY + 2);
} else if (cube[x][y].getIfHaveBomb() == true) {
//当玩家翻开的方块有地雷时
cube[x][y].setOpen();
show();
life = false;
return false;
} else if (cube[x][y].getOpen() == false) {
//当玩家翻开的方块无雷时
if (cube[x][y].getNearBombNumber() == 0) {
autoOpen(x, y);
cube[x][y].setOpen();
show();
} else {
cube[x][y].setOpen();
show();
}
} else if (cube[x][y].getOpen() == true) {
//当玩家输入已翻开方块的坐标时
show();
GoTo(STARTX, STARTY + MAXY + 3);
printf("该方块已被挖开,请再次输入坐标");
GoTo(STARTX + MAXX / 2, STARTY + MAXY + 2);
}
ifWin();
return true;
}
void message(bool result) {
if (result == true) {
//玩家胜利时输出的信息
showBomb();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);
GoTo(STARTX - 1, STARTY + MAXY + 1);
printf("祝贺你,你胜利了!");
GoTo(STARTX, STARTY + MAXY + 2);
} else {
//玩家失败时输出的信息
showBomb();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), PURPLE);
GoTo(STARTX - 1, STARTY + MAXY + 1);
printf("××你踩中地雷了××");
GoTo(STARTX, STARTY + MAXY + 2);
}
}
void autoOpen(int x, int y) {
//玩家翻开的方块为不含雷且周围无雷的方块时,自动翻开周围无雷的方块
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int nx = x + i;
int ny = y + j;
if (!(ny == y && nx == x) && (nx >= 0 && nx <= MAXX - 1) &&
(ny >= 0 && ny <= MAXY - 1) && cube[nx][ny].getOpen() == false) {
if (cube[nx][ny].getNearBombNumber() == 0) {
cube[nx][ny].setOpen();
autoOpen(nx, ny);
} else {
cube[nx][ny].setOpen();
}
}
}
}
}
bool ifWin() {
//判断玩家是否扫雷成功达到游戏结束条件
int num = 0;
for (int i = 0;i < MAXX;i++) {
for (int j = 0;j < MAXY;j++) {
if (cube[j][i].getOpen() == false) {
num++;
}
}
}
if (num == BOMBNUMBER) {
return true;
} else {
return false;
}
}
void showBomb() {
//游戏结束后显示地雷位置
for (int i = 0;i < MAXY;i++) {
for (int j = 0;j < MAXX;j++) {
if (cube[j][i].getIfHaveBomb() == true) {
cube[j][i].setOpen();
}
}
}
show();
}