c++实现俄罗斯方块

这是一个使用C++编写的俄罗斯方块游戏代码,专为Dev-C++5.9.2设计。代码中包含了游戏的基本功能,如图形绘制、键盘控制、游戏循环等。玩家可以通过键盘操作进行游戏,程序还包括游戏菜单、游戏结束界面以及分数计算等功能。
摘要由CSDN通过智能技术生成

以下就是俄罗斯方块的c++代码,大家可以参考(这是原创)
          

 注意:这个代码只能在dev-c++ 5.9.2编译器里运行,其他的编译器不能。

#include<iostream>
#include<math.h>
#include<Windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
 
enum DIR
{
	UP,
	RIGHT,
	DOWN,
	LEFT
};
 
time_t start = 0, finish = 0;
 
int _x = 6, _y = 1;//图形生成位置
 
int map[30][16] = { 0 };
 
int sharp[20][8] = {
	{0,0,0,0,0,0,0,0},
	//I形
	{0,0,0,1,0,2,0,3},
	{0,0,1,0,2,0,3,0},
	//■形
	{0,0,1,0,0,1,1,1},
	//L形
	{0,0,0,1,0,2,1,2},
	{0,0,0,1,1,0,2,0},
	{0,0,1,0,1,1,1,2},
	{0,1,1,1,2,0,2,1},
	//J形
	{0,2,1,0,1,1,1,2},
	{0,0,0,1,1,1,2,1},
	{0,0,0,1,0,2,1,0},
	{0,0,1,0,2,0,2,1},
	//Z形
	{0,0,1,0,1,1,2,1},
	{0,1,0,2,1,0,1,1},
	//S形
	{0,1,1,0,1,1,2,0},
	{0,0,0,1,1,1,1,2},
	//T形
	{0,1,1,0,1,1,2,1},
	{0,0,0,1,0,2,1,1},
	{0,0,1,0,1,1,2,0},
	{0,1,1,0,1,1,1,2}
};
 
 
class Game
{
public:
	int score;//游戏分数
	int _id;//图形编号
	int top;//最高点高度
	int speed;//下落速度
 
	Game();
	void showMenu();//显示菜单
	void showGround();//显示游戏界面
	void gameOver();//游戏结束界面
	void Run();//运行游戏
	void sharpDraw(int id, bool show = false);//绘制图形
	void keyControl();//键盘控制
	bool move(int dir, int id);//移动判断
	bool downSet(int id);//下落
	void Turn(int id);//旋转
	void clean();//消行
};
 
void SetPos(int i, int j)//控制光标位置, 列, 行
{
	COORD pos = { i,j };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
 
int main()
{
	CONSOLE_CURSOR_INFO cursor;
	GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
	cursor.bVisible = 0;	//这四行用来设置光标不显示
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
 
	srand((unsigned)time(NULL));
 
	Game game;
	game.showMenu();
	return 0;
}
 
Game::Game()
{
	score = 0;
	_id = 0;
	top = 58;
	speed = 1000;
}
 
void Game::showMenu()
{
	for (int i = 0; i < 30; i++)
	{
		for (int j = 0; j < 26; j++)
		{
			if ((i == 0 || i == 29) || (j == 0 || j == 25))
			{
				cout << "■";
			}
			else
			{
				cout << "  ";
			}
		}
		cout << endl;
	}
 
	SetPos(17, 8);
	cout << "俄 罗 斯 方 块" << endl;
	SetPos(13, 12);
	cout << "↑旋转方块  ↓加速下滑" << endl;
	SetPos(12, 14);
	cout << "← →左右移动  空格  暂停" << endl;
	SetPos(15, 20);
	cout << "0 退出  Enter 开始" << endl;
 
	while (1)
	{
		int select = _getch();
		if (select == 13)
		{
			system("cls");
			this->Run();
		}
		else if (select = 48)
		{
			system("cls");
			exit(0);
		}
	}
}
 
void Game::showGround()
{
	for (int i = 0; i < 30; i++)
	{
		for (int j = 0; j < 26; j++)
		{
			if ((i == 0 || i == 29) || (j == 0 || j == 25 || j == 15))
			{
				cout << "■";
			}
			else if (i == 15 && j > 15)
			{
				cout << "■";
			}
			else
			{
				cout << "  ";
			}
		}
		cout << endl;
	}
 
	SetPos(31, 2);
	cout << "下 个图形" << endl;
	SetPos(31, 17);
	cout << "当 前得分" << endl;
 
	for (int i = 0; i < 30; i++)
	{
		for (int j = 0; j < 16; j++)
		{
			if ((i == 0 || i == 29) || (j == 0 || j == 15))
			{
				map[i][j] = 1;
			}
			else
			{
				map[i][j] = 0;
			}
		}
	}
}
 
void Game::gameOver()
{
	for (int i = 5; i < 15; i++)
	{
		SetPos(1, i);
		cout << "                            " << endl;
	}
 
	SetPos(8, 7);
	cout << "G a m e   O v e r" << endl;
 
	SetPos(3, 10);
	cout << "0 退出   Enter 重新开始" << endl;
 
	while (1)
	{
		int select = _getch();
		if (select == 13)
		{
			system("cls");
			this->Run();
		}
		else if (select == 48)
		{
			system("cls");
			exit(0);
		}
	}
 
}
 
void Game::Run()
{
	score = 0;
	_id = 0;
	top = 58;
	_x = 6;
	_y = 1;
	showGround();
	start = clock();
	int new_id = rand() % 19 + 1;
 
	while (1)
	{
		sharpDraw(_id);
		keyControl();
 
		if (downSet(_id))
		{
			sharpDraw(-new_id, 1);
			_id = new_id;
			new_id = rand() % 19 + 1;
			sharpDraw(new_id, 1);
			clean();
		}
 
		SetPos(34, 20);
		cout << score << endl;
	}
}
 
void Game::sharpDraw(int id, bool show)
{
	int x, y;
 
	if (show == true)
	{
		if (id > 0)
		{
			for (int i = 0; i < 4; i++)
			{
				x = 19 + sharp[id][2 * i];
				y = 6 + sharp[id][2 * i + 1];
				SetPos(2 * x, y);
				cout << "■";
			}
		}
		else
		{
			for (int i = 0; i < 4; i++)
			{
				x = 19 + sharp[-id][2 * i];
				y = 6 + sharp[-id][2 * i + 1];
				SetPos(2 * x, y);
				cout << "  ";
			}
		}
		return;
	}
 
 
	if (id > 0)
	{
		for (int i = 0; i < 4; i++)
		{
			x = _x + sharp[id][2 * i];
			y = _y + sharp[id][2 * i + 1];
			SetPos(2 * x, y);
			cout << "■";
		}
	}
	else
	{
		for (int i = 0; i < 4; i++)
		{
			x = _x + sharp[-id][2 * i];
			y = _y + sharp[-id][2 * i + 1];
			SetPos(2 * x, y);
			cout << "  ";
		}
	}
	return;
 
}
 
bool Game::downSet(int id)
{
	if (id == 0)
		return true;
 
	finish = clock();
 
	if (finish - start < speed)
	{
		return false;
	}
 
	start = clock();
 
	if (!move(DOWN, _id))
	{
		int x, y;
		for (int i = 0; i < 4; i++)
		{
			x = _x + sharp[id][2 * i];
			y = _y + sharp[id][2 * i + 1];
			map[y][x] = 1;
 
			if (y < top)
			{
				top = y;
			}
			if (top <= 1)
			{
				gameOver();
			}
		}
		_x = 6;
		_y = 1;
		return true;
	}
 
	sharpDraw(-id);
	_y++;
	sharpDraw(id);
	return false;
}
 
bool Game::move(int dir, int id)
{
	int x, y;
	switch (dir)
	{
	case UP:
		for (int i = 0; i < 4; i++)
		{
			x = _x + sharp[id][2 * i];
			y = _y + sharp[id][2 * i + 1];
			if (map[y][x] == 1)
			{
				return false;
			}
		}
		break;
	case DOWN:
	{
		for (int i = 0; i < 4; i++)
		{
			x = _x + sharp[id][2 * i];
			y = _y + sharp[id][2 * i + 1];
			if (map[y + 1][x] == 1)
			{
				return false;
			}
		}
	}
	break;
	case RIGHT:
	{
		for (int i = 0; i < 4; i++)
		{
			x = _x + sharp[id][2 * i];
			y = _y + sharp[id][2 * i + 1];
			if (map[y][x + 1] == 1)
			{
				return false;
			}
		}
	}
	break;
	case LEFT:
	{
		for (int i = 0; i < 4; i++)
		{
			x = _x + sharp[id][2 * i];
			y = _y + sharp[id][2 * i + 1];
			if (map[y][x - 1] == 1)
			{
				return false;
			}
		}
	}
	break;
	default:
		break;
	}
	return true;
}
 
void Game::Turn(int id)
{
	switch (id)
	{
	case 1:id++; break;
	case 2:id--; break;
 
	case 3: break;
 
	case 4:id++; break;
	case 5:id++; break;
	case 6:id++; break;
	case 7:id -= 3; break;
 
	case 8:id++; break;
	case 9:id++; break;
	case 10:id++; break;
	case 11:id -= 3; break;
 
	case 12:id++; break;
	case 13:id--; break;
 
	case 14:id++; break;
	case 15:id--; break;
 
	case 16:id++; break;
	case 17:id++; break;
	case 18:id++; break;
	case 19:id -= 3; break;
 
	default:
		break;
	}
 
	if (!move(UP, id))
	{
		return;
	}
 
	sharpDraw(-_id);
	_id = id;
}
 
void Game::keyControl()
{
	if (!_kbhit())
		return;
 
	int key = _getch();
 
	switch (key)
	{
	case 72:
		Turn(_id);
		break;
	case 80:
		if (move(DOWN, _id))
		{
			sharpDraw(-_id);
			_y++;
		}
		break;
	case 75:
		if (move(LEFT, _id))
		{
			sharpDraw(-_id);
			_x--;
		}
		break;
	case 77:
		if (move(RIGHT, _id))
		{
			sharpDraw(-_id);
			_x++;
		}
		break;
	case 32:
	{
		for (int i = 5; i < 15; i++)
		{
			SetPos(1, i);
			cout << "                            " << endl;
		}
 
		SetPos(10, 7);
		cout << "游 戏 暂 停" << endl;
 
		SetPos(3, 10);
		cout << "0 返回菜单  回车 继续游戏" << endl;
 
		while (1)
		{
			int select = _getch();
 
			if (select == 13)
			{
				for (int i = 5; i < 15; i++)
				{
					SetPos(1, i);
					cout << "                            " << endl;
				}
				break;
			}
			else if (select == 48)
			{
				system("cls");
				showMenu();
			}
		}
 
	}
	default:
		break;
	}
}
 
void Game::clean()
{
	int n = -1;
	int line = -1;
	while (1)
	{
		for (int i = 28; i > 0; i--)
		{
			for (int j = 1; j < 15; j++)
			{
				line = i;
				if (map[i][j] == 0)
				{
					line = -1;
					break;
				}
			}
			if (line != -1)
				break;
		}
 
		if (line == -1)
			break;
 
		for (int i = line; i > 0; i--)
		{
			for (int j = 1; j < 15; j++)
			{
				if (i == 1)
					map[i][j] = 0;
				else
				{
					map[i][j] = map[i - 1][j];
					SetPos(2 * j, i);
					if (map[i][j] == 1)
						cout << "■";
					else
						cout << "  ";
				}
			}
		}
		top++;
		n++;
	}
 
	if (n >= 0)
	{
		score += n * n * 100 + 100;
		if (speed > 100)
			speed = 1000 - score / 10;
	}
}

以下是一个简单的C语言实现俄罗斯方块游戏的代码,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> #include <windows.h> #define WIDTH 10 // 地图宽度 #define HEIGHT 20 // 地图高度 #define SIZE 4 // 方块大小 // 方块类型 enum BlockType { I_BLOCK, J_BLOCK, L_BLOCK, O_BLOCK, S_BLOCK, T_BLOCK, Z_BLOCK }; // 方块结构体 struct Block { int x[SIZE]; // 方块的x坐标 int y[SIZE]; // 方块的y坐标 int type; // 方块类型 int dir; // 方块的方向(0-3) }; // 地图数组 int map[HEIGHT][WIDTH]; // 方块数组 struct Block blocks[] = { { {0, 1, 2, 3}, {0, 0, 0, 0}, I_BLOCK, 0 }, { {0, 1, 2, 2}, {0, 0, 0, 1}, J_BLOCK, 0 }, { {0, 1, 2, 2}, {0, 0, 0, 1}, L_BLOCK, 0 }, { {0, 1, 0, 1}, {0, 0, 1, 1}, O_BLOCK, 0 }, { {1, 2, 0, 1}, {0, 0, 1, 1}, S_BLOCK, 0 }, { {0, 1, 2, 1}, {0, 0, 0, 1}, T_BLOCK, 0 }, { {0, 1, 1, 2}, {0, 0, 1, 1}, Z_BLOCK, 0 } }; // 随机生成一个方块 struct Block generateBlock() { struct Block block = blocks[rand() % 7]; for (int i = 0; i < SIZE; i++) { block.x[i] += WIDTH / 2 - 1; block.y[i] += 1; } return block; } // 判断方块是否可以移动 int canMove(struct Block block, int dx, int dy) { for (int i = 0; i < SIZE; i++) { int x = block.x[i] + dx; int y = block.y[i] + dy; if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT || map[y][x]) { return 0; } } return 1; } // 移动方块 void moveBlock(struct Block *block, int dx, int dy) { if (canMove(*block, dx, dy)) { for (int i = 0; i < SIZE; i++) { block->x[i] += dx; block->y[i] += dy; } } } // 旋转方块 void rotateBlock(struct Block *block) { int dir = block->dir; block->dir = (dir + 1) % 4; int x = block->x[1]; int y = block->y[1]; int dx, dy; for (int i = 0; i < SIZE; i++) { dx = block->x[i] - x; dy = block->y[i] - y; block->x[i] = x + dy; block->y[i] = y - dx; } if (!canMove(*block, 0, 0)) { block->dir = dir; for (int i = 0; i < SIZE; i++) { dx = block->x[i] - x; dy = block->y[i] - y; block->x[i] = x - dy; block->y[i] = y + dx; } } } // 将方块放入地图 void putBlock(struct Block block) { for (int i = 0; i < SIZE; i++) { map[block.y[i]][block.x[i]] = block.type + 1; } } // 消除满行 void clearLines() { int count = 0; for (int i = HEIGHT - 1; i >= 0; i--) { int flag = 1; for (int j = 0; j < WIDTH; j++) { if (!map[i][j]) { flag = 0; break; } } if (flag) { count++; for (int k = i; k > 0; k--) { for (int j = 0; j < WIDTH; j++) { map[k][j] = map[k - 1][j]; } } for (int j = 0; j < WIDTH; j++) { map[0][j] = 0; } i++; } } } // 绘制地图 void drawMap() { system("cls"); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (map[i][j]) { printf("[]"); } else { printf(" "); } } printf("\n"); } } int main() { srand(time(NULL)); struct Block block = generateBlock(); int score = 0; while (1) { drawMap(); if (_kbhit()) { char ch = _getch(); switch (ch) { case 'a': moveBlock(&block, -1, 0); break; case 'd': moveBlock(&block, 1, 0); break; case 's': moveBlock(&block, 0, 1); break; case 'w': rotateBlock(&block); break; case 'q': exit(0); break; } } if (!canMove(block, 0, 1)) { putBlock(block); clearLines(); score += 10; block = generateBlock(); if (!canMove(block, 0, 0)) { break; } } else { moveBlock(&block, 0, 1); } Sleep(100); } printf("Game over! Your score is %d.\n", score); return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值