高级光能
太空大战
飞船迫降,来到一颗未知的星球,星球上有很多怪,如何在这里生存下去
这样就可以了
赵泰来在2020-07-18 18:26:49追加了内容
望采纳,却豆豆了
新手天翼
俄罗斯方块:
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
using namespace std;
int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } };
int block01[4][4] = { { 11,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 } };
int block02[4][4] = { { 12,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,1,0,0 } };
int block03[4][4] = { { 13,0,0,0 },{ 0,1,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block04[4][4] = { { 14,0,0,0 },{ 0,0,0,0 },{ 0,1,0,0 },{ 1,1,1,0 } };
int block05[4][4] = { { 15,0,0,0 },{ 0,1,0,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block06[4][4] = { { 16,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 1,0,0,0 } };
int block07[4][4] = { { 17,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block08[4][4] = { { 18,0,0,0 },{ 0,0,0,0 },{ 0,0,1,0 },{ 1,1,1,0 } };
int block09[4][4] = { { 19,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 0,1,1,0 } };
int block10[4][4] = { { 20,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,0,1,0 } };
int block11[4][4] = { { 21,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 1,1,0,0 } };
int block12[4][4] = { { 22,0,0,0 },{ 0,0,0,0 },{ 1,0,0,0 },{ 1,1,1,0 } };
int block13[4][4] = { { 23,0,0,0 },{ 0,1,1,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block14[4][4] = { { 24,0,0,0 },{ 0,0,0,0 },{ 0,1,1,0 },{ 1,1,0,0 } };
int block15[4][4] = { { 25,0,0,0 },{ 1,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block16[4][4] = { { 26,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 0,1,1,0 } };
int block17[4][4] = { { 27,0,0,0 },{ 0,0,1,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block18[4][4] = { { 28,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 1,1,0,1 } };
void initialWindow(HANDLE hOut);//初始化窗口
void initialPrint(HANDLE hOut);//初始化界面
void gotoXY(HANDLE hOut, int x, int y);//移动光标
void roundBlock(HANDLE hOut, int block[4][4]);//随机生成方块并打印到下一个方块位置
bool collisionDetection(int block[4][4], int map[21][12], int x, int y);//检测碰撞
void printBlock(HANDLE hOut, int block[4][4], int x, int y);//打印方块
void clearBlock(HANDLE hOut, int block[4][4], int x, int y);//消除方块
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//左移
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//右移
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//顺时针旋转90度
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);//加速下落
void myStop(HANDLE hOut, int block[4][4]);//游戏暂停
void gameOver(HANDLE hOut, int block[4][4], int map[21][12]);//游戏结束
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);//判断是否能消行并更新分值
int main()
{
int map[21][12];
int blockA[4][4];//候选区的方块
int blockB[4][4];//下落中的方块
int positionX, positionY;//方块左上角的坐标
bool check;//检查方块还能不能下落
char key;//用来存储按键
int val;//用来控制下落速度
int fraction;//用来存储得分
int checkpoint;//用来存储关卡
int times;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄
initialWindow(hOut);
initial:
gotoXY(hOut, 0, 0);
initialPrint(hOut);
check = true;
val = 50;
fraction = 0;
checkpoint = 1;
times = val;
for (int i = 0; i < 20; ++i)
{
for (int j = 1; j < 11; ++j)
{
map[i][j] = 0;
}
}
for (int i = 0; i < 20; ++i)
{
map[i][0] = map[i][11] = 1;
}
for (int i = 0; i < 12; ++i)
{
map[20][i] = 1;
}
srand((unsigned)time(NULL));
roundBlock(hOut, blockA);
while (true)
{
if (check)
{
eliminateRow(hOut, map, val, fraction, checkpoint);
check = false;
positionX = -3;
positionY = 4;
if (collisionDetection(blockA, map, positionX, positionY))
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
blockB[i][j] = blockA[i][j];
}
}
roundBlock(hOut, blockA);
}
else
{
gameOver(hOut, blockA, map);
goto initial;
}
}
printBlock(hOut, blockB, positionX, positionY);
if (_kbhit())
{
key = _getch();
switch (key)
{
case 72:
myUp(hOut, blockB, map, positionX, positionY);
break;
case 75:
myLeft(hOut, blockB, map, positionX, positionY);
break;
case 77:
myRight(hOut, blockB, map, positionX, positionY);
break;
case 80:
switch (myDown(hOut, blockB, map, positionX, positionY))
{
case 0:
check = false;
break;
case 1:
check = true;
break;
case 2:
gameOver(hOut, blockB, map);
goto initial;
default:
break;
}
break;
case 32:
myStop(hOut, blockA);
break;
case 27:
exit(0);
default:
break;
}
}
Sleep(20);
if (0 == --times)
{
switch (myDown(hOut, blockB, map, positionX, positionY))
{
case 0:
check = false;
break;
case 1:
check = true;
break;
case 2:
gameOver(hOut, blockB, map);
goto initial;
default:
break;
}
times = val;
}
}
cin.get();
return 0;
}
void initialWindow(HANDLE hOut)
{
SetConsoleTitle("俄罗斯方块");
COORD size = { 80, 25 };
SetConsoleScreenBufferSize(hOut, size);
SMALL_RECT rc = { 0, 0, 79, 24 };
SetConsoleWindowInfo(hOut, true, &rc);
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(hOut, &cursor_info);
}
void initialPrint(HANDLE hOut)
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
for (int i = 0; i < 20; ++i)
{
cout << "■ ■☆ ☆" << endl;
}
gotoXY(hOut, 26, 0);
cout << "☆☆☆☆☆☆☆☆☆☆☆";
gotoXY(hOut, 0, 20);
cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆";
gotoXY(hOut, 26, 1);
cout << "分 数: ";
gotoXY(hOut, 26, 2);
cout << "关 卡: ";
gotoXY(hOut, 26, 4);
cout << "下一方块:";
gotoXY(hOut, 26, 9);
cout << "操作方法:";
gotoXY(hOut, 30, 11);
cout << "↑:旋转 ↓:速降";
gotoXY(hOut, 30, 12);
cout << "→:右移 ←:左移";
gotoXY(hOut, 30, 13);
cout << "空格键:开始/暂停";
gotoXY(hOut, 30, 14);
cout << "Esc 键:退出";
gotoXY(hOut, 26, 16);
cout << "关 于:";
gotoXY(hOut, 30, 18);
cout << "俄罗斯方块V1.0";
gotoXY(hOut, 35, 19);
cout << "作者:X X X";
}
void gotoXY(HANDLE hOut, int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos);
}
void roundBlock(HANDLE hOut, int block[4][4])
{
clearBlock(hOut, block, 5, 15);
switch (rand() % 19)
{
case 0:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
break;
case 1:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block01[i][j];
}
}
break;
case 2:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
break;
case 3:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
break;
case 4:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
break;
case 5:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
break;
case 6:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
break;
case 7:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
break;
case 8:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
break;
case 9:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
break;
case 10:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
break;
case 11:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
break;
case 12:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
break;
case 13:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
break;
case 14:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
break;
case 15:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
break;
case 16:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
break;
case 17:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
break;
case 18:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block18[i][j];
}
}
break;
default:
break;
}
printBlock(hOut, block, 5, 15);
}
bool collisionDetection(int block[4][4], int map[21][12], int x, int y)
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1)
{
return false;
}
}
}
return true;
}
void printBlock(HANDLE hOut, int block[4][4], int x, int y)
{
switch (block[0][0])
{
case 10:
case 11:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
break;
case 12:
case 13:
case 14:
case 15:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 16:
case 17:
case 18:
case 19:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 20:
case 21:
case 22:
case 23:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 24:
case 25:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 26:
case 27:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 28:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
default:
break;
}
for (int i = 0; i < 4; ++i)
{
if (i + x >= 0)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
gotoXY(hOut, 2 * (y + j), x + i);
cout << "■";
}
}
}
}
}
void clearBlock(HANDLE hOut, int block[4][4], int x, int y)
{
for (int i = 0; i < 4; ++i)
{
if (i + x >= 0)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
gotoXY(hOut, 2 * (y + j), x + i);
cout << " ";
}
}
}
}
}
void gameOver(HANDLE hOut, int block[4][4], int map[21][12])
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 9, 8);
cout << "GAME OVER";
gotoXY(hOut, 8, 9);
cout << "空格键:重来";
gotoXY(hOut, 8, 10);
cout << "ESC键:退出";
char key;
while (true)
{
key = _getch();
if (key == 32)
{
return;
}
if (key == 27)
{
exit(0);
}
}
}
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y)
{
if (collisionDetection(block, map, x + 1, y))
{
clearBlock(hOut, block, x, y);
++x;
return 0;
}
if (x < 0)
{
return 2;
}
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
map[x + i][y + j] = 1;
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 2 * (y + j), x + i);
cout << "■";
}
}
}
return 1;
}
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
if (collisionDetection(block, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
--y;
}
}
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
if (collisionDetection(block, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
++y;
}
}
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
switch (block[0][0])
{
case 10:
if (collisionDetection(block01, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block01[i][j];
}
}
}
break;
case 11:
if (collisionDetection(block00, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
}
else if (collisionDetection(block00, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
--y;
}
else if (collisionDetection(block00, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
++y;
}
else if (collisionDetection(block00, map, x, y - 2))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
y = y - 2;
}
else if (collisionDetection(block00, map, x, y + 2))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
y = y + 2;
}
break;
case 12:
if (collisionDetection(block03, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
}
else if (collisionDetection(block03, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
--y;
}
else if (collisionDetection(block03, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
++y;
}
break;
case 13:
if (collisionDetection(block04, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
}
else if (collisionDetection(block04, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
--y;
}
else if (collisionDetection(block04, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
++y;
}
break;
case 14:
if (collisionDetection(block05, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
}
else if (collisionDetection(block05, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
--y;
}
else if (collisionDetection(block05, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
++y;
}
break;
case 15:
if (collisionDetection(block02, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
}
else if (collisionDetection(block02, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
--y;
}
else if (collisionDetection(block02, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
++y;
}
break;
case 16:
if (collisionDetection(block07, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
}
else if (collisionDetection(block07, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
--y;
}
else if (collisionDetection(block07, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
++y;
}
break;
case 17:
if (collisionDetection(block08, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
}
else if (collisionDetection(block08, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
--y;
}
else if (collisionDetection(block08, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
++y;
}
break;
case 18:
if (collisionDetection(block09, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
}
else if (collisionDetection(block09, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
--y;
}
else if (collisionDetection(block09, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
++y;
}
break;
case 19:
if (collisionDetection(block06, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
}
else if (collisionDetection(block06, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
--y;
}
else if (collisionDetection(block06, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
++y;
}
break;
case 20:
if (collisionDetection(block11, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
}
else if (collisionDetection(block11, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
--y;
}
else if (collisionDetection(block11, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
++y;
}
break;
case 21:
if (collisionDetection(block12, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
}
else if (collisionDetection(block12, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
--y;
}
else if (collisionDetection(block12, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
++y;
}
break;
case 22:
if (collisionDetection(block13, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
}
else if (collisionDetection(block13, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
--y;
}
else if (collisionDetection(block13, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
++y;
}
break;
case 23:
if (collisionDetection(block10, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
}
else if (collisionDetection(block10, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
--y;
}
else if (collisionDetection(block10, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
++y;
}
break;
case 24:
if (collisionDetection(block15, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
}
else if (collisionDetection(block15, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
--y;
}
else if (collisionDetection(block15, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
++y;
}
break;
case 25:
if (collisionDetection(block14, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
}
else if (collisionDetection(block14, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
--y;
}
else if (collisionDetection(block14, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
++y;
}
break;
case 26:
if (collisionDetection(block17, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
}
else if (collisionDetection(block17, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
--y;
}
else if (collisionDetection(block17, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
++y;
}
break;
case 27:
if (collisionDetection(block16, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
}
else if (collisionDetection(block16, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
--y;
}
else if (collisionDetection(block16, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
++y;
}
break;
default:
break;
}
}
void myStop(HANDLE hOut, int block[4][4])
{
clearBlock(hOut, block, 5, 15);
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 30, 7);
cout << "游戏暂停";
char key;
while (true)
{
key = _getch();
if (key == 32)
{
gotoXY(hOut, 30, 7);
cout << " ";
printBlock(hOut, block, 5, 15);
return;
}
if (key == 27)
{
exit(0);
}
}
}
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint)
{
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
for (int i = 19; i >= 0; --i)
{
int x = 0;
for (int j = 1; j < 11; ++j)
{
x += map[i][j];
}
if (x == 10)
{
fraction += 100;
if (val > 1 && fraction / 1000 + 1 != checkpoint)
{
checkpoint = fraction / 1000 + 1;
val -= 5;
}
for (int m = i; m > 0; --m)
{
for (int n = 1; n < 11; ++n)
{
map[m][n] = map[m - 1][n];
gotoXY(hOut, 2 * n, m);
if (map[m][n] == 1)
{
cout << "■";
}
else
{
cout << " ";
}
}
}
++i;
}
}
gotoXY(hOut, 36, 1);
cout << fraction;
gotoXY(hOut, 36, 2);
cout << checkpoint;
}
张天璨在2020-07-18 18:29:46追加了内容
扫雷:
#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 = 15;//雷区的宽
const int MAXY = 15;//雷区的高
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();
}
张天璨在2020-07-18 19:06:18追加了内容
贪吃蛇:
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#define N 22
using namespace std;
int gameover;
int x1, y1; // 随机出米
int x,y;
long start;
//=======================================
//类的实现与应用initialize
//=======================================
//下面定义贪吃蛇的坐标类
class snake_position
{
public:
int x,y; //x表示行,y表示列
snake_position(){};
void initialize(int &);//坐标初始化
};
snake_position position[(N-2)*(N-2)+1]; //定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标
void snake_position::initialize(int &j)
{
x = 1;
y = j;
}
//下面定义贪吃蛇的棋盘图
class snake_map
{
private:
char s[N][N];//定义贪吃蛇棋盘,包括墙壁。
int grade, length;
int gamespeed; //前进时间间隔
char direction; // 初始情况下,向右运动
int head,tail;
int score;
bool gameauto;
public:
snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){}
void initialize(); //初始化函数
void show_game();
int updata_game();
void setpoint();
void getgrade();
void display();
};
//定义初始化函数,将贪吃蛇的棋盘图进行初始化
void snake_map::initialize()
{
int i,j;
for(i=1;i<=3;i++)
s[1][i] = '*';
s[1][4] = '#';
for(i=1;i<=N-2;i++)
for(j=1;j<=N-2;j++)
s[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分
for(i=0;i<=N-1;i++)
s[0][i] = s[N-1][i] = '-'; //初始化贪吃蛇棋盘上下墙壁
for(i=1;i<=N-2;i++)
s[i][0] = s[i][N-1] = '|'; //初始化贪吃蛇棋盘左右墙壁
}
//============================================
//输出贪吃蛇棋盘信息
void snake_map::show_game()
{
system("cls"); // 清屏
int i,j;
cout << endl;
for(i=0;i<N;i++)
{
cout << '\t';
for(j=0;j<N;j++)
cout<<s[i][j]<<' '; // 输出贪吃蛇棋盘
if(i==2) cout << "\t等级:" << grade;
if(i==6) cout << "\t速度:" << gamespeed;
if(i==10) cout << "\t得分:" << score << "分" ;
if(i==14) cout << "\t暂停:按一下空格键" ;
if(i==18) cout << "\t继续:按两下空格键" ;
cout<<endl;
}
}
//输入选择等级
void snake_map::getgrade()
{
cin>>grade;
while( grade>7 || grade<1 )
{
cout << "请输入数字1-7选择等级,输入其他数字无效" << endl;
cin >> grade;
}
switch(grade)
{
case 1: gamespeed = 1000;gameauto = 0;break;
case 2: gamespeed = 800;gameauto = 0;break;
case 3: gamespeed = 600;gameauto = 0;break;
case 4: gamespeed = 400;gameauto = 0;break;
case 5: gamespeed = 200;gameauto = 0;break;
case 6: gamespeed = 100;gameauto = 0;break;
case 7: grade = 1;gamespeed = 1000;gameauto = 1;break;
}
}
//输出等级,得分情况以及称号
void snake_map::display()
{
cout << "\n\t\t\t\t等级:" << grade;
cout << "\n\n\n\t\t\t\t速度:" << gamespeed;
cout << "\n\n\n\t\t\t\t得分:" << score << "分" ;
}
//随机产生米
void snake_map::setpoint()
{
srand(time(0));
do
{
x1 = rand() % (N-2) + 1;
y1 = rand() % (N-2) + 1;
}while(s[x1][y1]!=' ');
s[x1][y1]='*';
}
char key;
int snake_map::updata_game()
{
gameover = 1;
key = direction;
start = clock();
while((gameover=(clock()-start<=gamespeed))&&!kbhit());
//如果有键按下或时间超过自动前进时间间隔则终止循环
if(gameover)
{
getch();
key = getch();
}
if(key == ' ')
{
while(getch()!=' '){};//这里实现的是按空格键暂停,按空格键继续的功能,但不知为何原因,需要按两下空格才能继续。这是个bug。
}
else
direction = key;
switch(direction)
{
case 72: x= position[head].x-1; y= position[head].y;break; // 向上
case 80: x= position[head].x+1; y= position[head].y;break; // 向下
case 75: x= position[head].x; y= position[head].y-1;break; // 向左
case 77: x= position[head].x; y= position[head].y+1; // 向右
}
if(!(direction==72||direction==80||direction==75 ||direction==77)) // 按键非方向键
gameover = 0;
else if(x==0 || x==N-1 ||y==0 || y==N-1) // 碰到墙壁
gameover = 0;
else if(s[x][y]!=' '&&!(x==x1&&y==y1)) // 蛇头碰到蛇身
gameover = 0;
else if(x==x1 && y==y1)
{ // 吃米,长度加1
length ++;
if(length>=8 && gameauto)
{
length -= 8;
grade ++;
if(gamespeed>=200)
gamespeed -= 200; // 改变贪吃蛇前进速度
else
gamespeed = 100;
}
s[x][y]= '#'; //更新蛇头
s[position[head].x][position[head].y] = '*'; //吃米后将原先蛇头变为蛇身
head = (head+1) % ( (N-2)*(N-2) ); //取蛇头坐标
position[head].x = x;
position[head].y = y;
show_game();
gameover = 1;
score += grade*20; //加分
setpoint(); //产生米
}
else
{ // 不吃米
s[position[tail].x][position[tail].y]=' ';//将蛇尾置空
tail= (tail+1) % ( (N-2) * (N-2) );//更新蛇尾坐标
s[position[head].x][position[head].y]='*'; //将蛇头更为蛇身
head= (head+1) % ( (N-2) * (N-2) );
position[head].x = x;
position[head].y = y;
s[position[head].x][position[head].y]='#'; //更新蛇头
gameover = 1;
}
return gameover;
}
//====================================
//主函数部分
//====================================
int main()
{
MessageBox(NULL,"欢迎使用由张天璀璨制作的贪吃蛇游戏~祝您愉快","贪吃蛇",MB_OK);
char ctn = 'y';
int nodead;
cout<<"\n\n\n\n\n\t\t\t 欢迎进入贪吃蛇游戏!"<<endl;//欢迎界面;
cout<<"\n\n\n\t\t\t 按任意键马上开始。。。"<<endl;//准备开始;;
getch();
while( ctn=='y' )
{
system("cls"); // 清屏
snake_map snake;
snake.initialize();
cout << "\n\n请输入数字选择游戏等级:" << endl;
cout << "\n\n\n\t\t\t1.等级一:速度 0 \n\n\t\t\t2.等级二:速度 1 \n\n\t\t\t3.等级三:速度 2 ";
cout << "\n\n\t\t\t4.等级四:速度 3 \n\n\t\t\t5.等级五:速度 4 \n\n\t\t\t6.等级六:速度 5 \n\n\t\t\t7.自动升级模式" << endl;
snake.getgrade();//获取等级
for(int i=1;i<=4;i++)
{
position[i].initialize(i);//初始化坐标
}
snake.setpoint(); // 产生第一个米
do
{
snake.show_game();
nodead = snake.updata_game();
}while(nodead);
system("cls"); //清屏
cout << "\n\n\n\t\t\t\tGame over!\n\n"<<endl;
snake.display();//输出等级/得分情况
cout << "\n\n\n\t\t 是否选择继续游戏?输入 y 继续,n 退出" << endl;
cin >> ctn;
}
return 0;
}
张天璨在2020-07-18 19:24:10追加了内容
跪求采纳!
张天璨在2020-07-18 19:46:03追加了内容
注:贪吃蛇原作者为曹砚青
中级光能
/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
char qipan[20][80];
int snake = 1;
int change(int *head, int *tail, int *score, char *direction, int zuobiao[2][80])
{
int x, y;
switch(*direction)
{
case 72:
x = zuobiao[0][*head]-1;
y = zuobiao[1][*head];
break;
case 87:
x = zuobiao[0][*head]-1;
y = zuobiao[1][*head];
break;
case 80:
x = zuobiao[0][*head]+1;
y = zuobiao[1][*head];
break;
case 83:
x = zuobiao[0][*head]+1;
y = zuobiao[1][*head];
break;
case 75:
x = zuobiao[0][*head];
y = zuobiao[1][*head]-1;
break;
case 65:
x = zuobiao[0][*head];
y = zuobiao[1][*head]-1;
break;
case 68:
x = zuobiao[0][*head];
y = zuobiao[1][*head]+1;
break;
default:
x = zuobiao[0][*head];
y = zuobiao[1][*head]+1;
}
if(qipan[x][y] == '_' || qipan[x][y] == '|' || qipan[x][y] == '*')
return 1;
if(qipan[x][y] == ' ')
{
qipan[zuobiao[0][*tail]][zuobiao[1][*tail]]=' ';
*tail=(*tail+1)%80;
qipan[zuobiao[0][*head]][zuobiao[1][*head]]='*';
*head=(*head+1)%80;
zuobiao[0][*head]=x;
zuobiao[1][*head]=y;
qipan[x][y]='#';
return 0;
}
if(qipan[x][y] == '@')
{
qipan[zuobiao[0][*head]][zuobiao[1][*head]]='*';
*head=(*head+1)%80;
zuobiao[0][*head]=x;
zuobiao[1][*head]=y;
qipan[x][y]='#';
*score += 1;
return 0;
}
}
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void show_start()
{
gotoxy(20,0);
color(1);
int u;
for(u = 0; u < 18; u ++)
printf("* ");
gotoxy(20, 12);
for(u = 0; u < 18; u ++)
printf("* ");
gotoxy(28,2);
color(13);
printf("S N A K E G A M E");
gotoxy(31, 5);
color(14);
printf("#");
gotoxy(34, 5);
color(11);
printf("One snake");
gotoxy(34, 7);
printf("Two snakes");
gotoxy(28,9);
printf("The speed you want: ");
gotoxy(31, 5);
char temp = 'a';
color(14);
while(1)
{
while(!kbhit());
temp = getch();
if(temp == 72 || temp == 80)
{
gotoxy(31,5+2*(snake-1));
printf(" ");
snake = snake % 2 + 1;
gotoxy(31,5+2*(snake-1));
printf("#");
gotoxy(31,5+2*(snake-1));
}
if(temp == 13)
break;
}
gotoxy(27,10);
color(10);
printf("GOOD LUCK TO YOU !");
}
int main()
{
srand(time(0));
int head1 = 3, tail1 = 0, score1 = 0;
int head2 = 3, tail2 = 0, score2 = 0;
int zuobiao1[2][80];
int zuobiao2[2][80];
/*棋盘 20×80 初始化*/
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 80; j++)
{
qipan[i][j] = ' ';
}
}
for(int i = 0; i < 20; i++)
{
qipan[i][0] = qipan[i][79] = '|';//第一列、最后一列是墙20*1
}
for(int i = 0; i < 80; i++)
{
qipan[0][i] = qipan[19][i] = '_';//第一行、最后一行是墙1*80
}
/*蛇的坐标 2×8 初始化, 将蛇放到棋盘上*/
int x1 = 1, x2 = 18, y, temp = 0;
for(int m = tail1; m < head1; m++)
{
zuobiao1[0][m] = x1;//初始行号
y = zuobiao1[1][m] = ++temp;//初始列号
qipan[x1][y] = '*';
}
zuobiao1[0][head1] = x1;//初始行号
y = zuobiao1[1][head1] = ++temp;//初始列号
qipan[x1][y] = '#';//蛇头
show_start();
if(snake == 2)
{
temp = 0;
for(int m = tail2; m < head2; m++)
{
zuobiao2[0][m] = x2;//初始行号
y = zuobiao2[1][m] = ++temp;//初始列号
qipan[x2][y] = '*';
}
zuobiao2[0][head2] = x2;//初始行号
y = zuobiao2[1][head2] = ++temp;//初始列号
qipan[x2][y] = '#';//蛇头
}
clock_t start;
int timeover;
char direction1 = 77;//72 88 75 77
char direction2 = 68;//87 83 65 68
char new_direction;
char new_direction1 ;
char new_direction2 ;
int gamespeed;
gotoxy(48, 9);
color(14);
scanf("%d", &gamespeed);
int rand_i = rand()%18 + 1;
int rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';
system("cls");
gotoxy(10, 1);
color(10);
printf("Your present score: ");
gotoxy(30, 1);
color(11);
printf("%d", score1);
gotoxy(0,4);
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
{
if(qipan[i][j] == '*' || qipan[i][j] == '#')
color(13);
else if(qipan[i][j] == '@')
color(12);
else
color(15);
printf("%c", qipan[i][j]);
}
while(1)//87 83 65 68
{
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
{
if(qipan[i][j] == '*' || qipan[i][j] == '#')
{
gotoxy(j, i+4);
color(13);
}
if(qipan[i][j] == '@')
{
gotoxy(j, i+4);
color(12);
}
if(qipan[i][j] == '_' || qipan[i][j] == '|')
{
gotoxy(j, i+4);
color(15);
}
printf("%c", qipan[i][j]);
}
for(int q = 0; q < 3; q++)
{
gotoxy(rand()%78 + 1, rand()%18 + 5 );
color(14);
printf("+");
}
start = clock();
timeover = 1;
while(!kbhit() && (timeover = clock() - start/CLOCKS_PER_SEC <= gamespeed));
if(timeover)//有按键
{
if(snake == 1)
getch();
new_direction = getch();
if(snake == 2)
{
if(new_direction == 87 || new_direction ==83 ||new_direction == 65||new_direction == 68)
{
new_direction2 = new_direction;
new_direction1 = direction1;
if(new_direction2 + direction2 == 170 || new_direction2 + direction2 == 133)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
change(&head2, &tail2, &score2, &direction2, zuobiao2);
continue;
}
}
if(new_direction == 72 || new_direction ==80 ||new_direction == 75||new_direction == 77)
{
new_direction1 = new_direction;
new_direction2 = direction2;
if(new_direction1 + direction1 == 152)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
change(&head2, &tail2, &score2, &direction2, zuobiao2);
continue;
}
}
}
if(snake == 1 && (new_direction == 72 || new_direction ==80 ||new_direction == 75||new_direction == 77))
{
new_direction1 = new_direction;
if(new_direction1 + direction1 == 152)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
continue;
}
}
}
else
{
if(snake == 2)
{
new_direction2 = direction2;
new_direction1 = direction1;
}
else
{
new_direction1 = direction1;
}
}
if(snake == 1)
{
direction1 = new_direction1;
if(change(&head1, &tail1, &score1, &direction1, zuobiao1) == 1)
{
system("cls");
gotoxy(30,10);
color(7);
printf("G A M E O V E R !");
gotoxy(0,20);
color(1);
return 0;
}
}
if(snake == 2)
{
direction1 = new_direction1;
if(change(&head1, &tail1, &score1, &direction1, zuobiao1) == 1)
{
system("cls");
gotoxy(30,8);
color(7);
printf("G A M E O V E R !");
gotoxy(30,10);
printf("Snake Two is the Hero!");
gotoxy(0,20);
color(1);
return 0;
}
direction2 = new_direction2;
if(change(&head2, &tail2, &score2, &direction2, zuobiao2) == 1)
{
system("cls");
gotoxy(30,8);
color(7);
printf("G A M E O V E R !");
gotoxy(30,10);
printf("Snake One is the Hero!");
gotoxy(0,20);
color(1);
return 0;
}
}
int randnew = 0;
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
if(qipan[i][j] == '@')
{
randnew ++;
}
if(randnew == 0)
{
rand_i = rand()%18 + 1;
rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';
}
if(snake == 2 && randnew == 1)
{
rand_i = rand()%18 + 1;
rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';
}
gotoxy(30, 1);
printf("%d", score1);
if(snake == 2)
{
gotoxy(30,2);
printf("%d", score2);
}
}
}
新手天翼
基础游戏分为两种:文字和动作。
1.文字:是以剧情为开头,选择游戏动作的c++游戏,我建议你编这样逻辑性较强、函数简单的文字游戏,思路如下:
①写出剧情。
②设置用量,如游戏中用到的金钱、生命等。
③用多个if嵌套来实施程序。
④一轮结束后反复循环。
⑤如果死了,那么自动return0退出游戏。
在下自编代码献上
MZ?
啊,代码文件丢失了,看我之前的问答记录吧!
2.动作:键位控制物体移动,此游戏函数较难,建议高手使用(我不会,O(∩_∩)O哈哈~),经典代码:贪吃蛇、俄罗斯方块。