C++练习实例———控制台代码实现俄罗斯方块小游戏

    在vs上写了一个俄罗斯方块的小游戏,输出画面就靠windows.h 下面上代码。

    首先写一个坐标类,方便后面的操作

#ifndef POINT_H
#define POINT_H
//坐标类
class Point
{
public:
	Point(int x = 0, int y = 0) : m_x(x), m_y(y){};
	~Point() {};
	Point& operator=(const Point &p)
	{
		m_x = p.m_x;
		m_y = p.m_y;
		return *this;
	}
	void Set(const int x,const int y) {m_x = x;m_y = y;}
	void SetX(const int x) { m_x = x; }
	void SetY(const int y) { m_y = y; }
	int GetX() const { return m_x; }
	int GetY()const { return m_y; }
private:
	int m_x;
	int m_y;
};


#endif

    再创建一个设置类,用来管理游戏中的数值。因为这些数值(分数、游戏速度等)是伴随着整个游戏过程的,所以设为静态

#include <list>
#ifndef SETTING_H
#define SETTING_H
using namespace std;
//数值设定类,用于计算分数和速度等
class Setting
{
public:
	static int GetSumScore() { return m_SumScore; }
	static void AddSpeed() {  m_GameSpeed -= 3; }
	static void AddScore() { m_SumScore++; }
	static int GetGameSpeed() { return m_GameSpeed; }
	static void HalfSpeed() { m_GameSpeed /= 2; }
	static void DoubleSpeed(){ m_GameSpeed *= 2; }
private:
	static int m_GameSpeed;     //游戏速度,数字越小速度越快
	static int m_SumScore;		// 分数
};
#endif
#include "Setting.h"
int Setting::m_GameSpeed = 500;
int Setting::m_SumScore = 0;

  创建一个Object类,是所有游戏对象都要继承的父类

#ifndef OBJECT
#define OBJECT
#include <list>
#include"Point.h"
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
//物体类,一切游戏对象的基类
class Object
{
public:
	virtual void Display() = 0;//输出画面
	virtual void SetDisappear() = 0;//清除画面
protected:
	Point m_Position;
};
#endif

    然后是方块类,是指组成一个整体形状的每一个小方块

#ifndef SINGLE_BOX
#define SINGLE_BOX
#include"Object.h"
//小方块类
class SingleBox :Object
{
private:
	int m_color;
public:
	SingleBox() {}
	SingleBox(Point pos, int color)
	{
		m_Position = pos;
		m_color = color;
	}
	~SingleBox(){}
	void Display();//输出小方块
	void SetDisappear();//删除小方块
	int  GetBoxX() const{ return m_Position.GetX(); }
	int  GetBoxY()const{ return m_Position.GetY(); }
};
#endif
#include"SingleBox.h"
void SingleBox:: Display()
{
	COORD pos;
	pos.X = m_Position.GetX();
	pos.Y = m_Position.GetY();
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), m_color);
	cout << "■";
}
void SingleBox::SetDisappear()
{
	COORD pos;
	pos.X = m_Position.GetX();
	pos.Y = m_Position.GetY();
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
	cout << " ";
}

    小方块类写好后,就可以写组合形状的类了,它包含一个存储小方块的数组,因为每个形状由四个小方块组成

#ifndef COMBO_BOX
#define COMBO_BOX
#include"SingleBox.h"
enum Type { A, B, C, D, E};//一共五种组合方块
enum Form { a, b, c, d };//每个组合方块最多四种形态
enum Dir { LEFT, RIGHT ,EMPTY};
//组合方块类
class ComboBox :Object
{
private:
	SingleBox m_ComBox[4];//每个组合方块由四个小方块组成
	Type m_Type;
	Form m_Form;
	Dir m_Direction;
	int m_XDis;//每输出一个方块,x轴占两个坐标(y轴只占一个)
public:
	ComboBox(Point pos, Type);
	~ComboBox(){}
	Type GetType()const{return m_Type;}
	void SetDirection(const Dir dir) { 
  • 16
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
罗斯方块是一款经典的游戏,它的实现可以使用C++语言。游戏实现需要用到图形界面库和计时器等功能。游戏的主要逻辑是方块的移动和旋转,以及方块与地图的碰撞检测。下面是一个简单的俄罗斯方块游戏实现代码框架: ``` #include <iostream> #include <windows.h> #include <conio.h> #include <time.h> using namespace std; const int WIDTH = 10; // 地图宽度 const int HEIGHT = 20; // 地图高度 const int BLOCK_SIZE = 4; // 方块大小 int map[HEIGHT][WIDTH]; // 地图数组 int block[BLOCK_SIZE][BLOCK_SIZE]; // 方块数组 int curX, curY; // 当前方块的位置 int curBlock; // 当前方块的形状 int nextBlock; // 下一个方块的形状 int score; // 得分 int level; // 等级 // 初始化游戏 void initGame(); // 绘制游戏界面 void drawGame(); // 生成新的方块 void newBlock(); // 方块下落 void blockDown(); // 方块左移 void blockLeft(); // 方块右移 void blockRight(); // 方块旋转 void blockRotate(); // 消除满行 void clearLine(); // 判断游戏是否结束 bool isGameOver(); // 主函数 int main() { initGame(); while (true) { drawGame(); blockDown(); if (isGameOver()) { break; } } return 0; } ``` 其中,initGame()函数用于初始化游戏数据,drawGame()函数用于绘制游戏界面,newBlock()函数用于生成新的方块,blockDown()函数用于方块下落,blockLeft()和blockRight()函数用于方块左右移动,blockRotate()函数用于方块旋转,clearLine()函数用于消除满行,isGameOver()函数用于判断游戏是否结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值