游戏截图 游戏背景图
背景图为黑色 每一块与img与运算 得到的就是背景图
每一种的俄罗斯块可以用 4*4数组表示
移动的同时 需要随时获取键盘事件 可以多线程
效果显示 如:正方形 使用了easyx第三方库 可直接百度 很好搭建
代码
#include <graphics.h>
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <stdlib.h>
#define BG_WIDTH 10
#define BG_HEIGHT 20
void Put_image(int Map[10][20]);
using namespace std;
class Game_1
{
public:
Game_1();
int curBlockIndex ; //方块类型
int BlockIndex; //方向类型
int poX; //方块所在位置
int poY;
struct MatriData //4X4数组表示一个方向上的所有数据
{
int data[4][4];
};
struct Block
{
int dirctionCount; //方向数量
MatriData matriData[4]; //最多四个方向上的数据
};
Block arrBlock[7]; //俄罗斯方块的七种形状
void CreateNewBlock();
void DrawBlock(Block &block,int posX,int poY); //动态
void I_getch(void); //键盘检测
void changeBackGround(void); //页面添加新的方块
bool Check(); //检测方块到达底部
};
int backGround[BG_WIDTH][BG_HEIGHT]={
0}; //地图
DWORD WINAPI Game_getch(LPVOID lpParamter);
char CH;
int flag_1=0; //关于清除图片的标志位
int Ran[25]={
1,1,1,0,2,3,2,5,0,2,1,0,1,2,2,4,4,4,4,4,3 };
int Ran_1=0;
void main()
{
HANDLE hThread = CreateThread(NULL, 0, Game_getch, NULL, 0, NULL);
CloseHandle(hThread); //多线程
initgraph(420, 840); // 创建绘图窗口,大小为 420X840 像素
Game_1 game_1;
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
cout<<game_1.arrBlock[0].matriData[1].data[i][j];
cout <<endl;
}
game_1.CreateNewBlock(); //初始化 随机建立新的方块
while(1)
{
game_1.I_getch();
game_1.DrawBlock(game_1.arrBlock[game_1.BlockIndex],game_1.poX,game_1.poY);
if(game_1.Check())
{
flag_1=1;
game_1.changeBackGround();
game_1.CreateNewBlock();
}
//方块移动
Sleep(0.2*1000);
game_1.poY++;
}
getch();
}
void Game_1::changeBackGround()
{
int i=0,j=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(this->arrBlock[this->curBlockIndex].matriData[BlockIndex].data[i][j]==1)
{
backGround[this->poX+i][this->poY+j]=1;
}
}
}
Put_image(backGround);
}
bool Game_1::Check()
{
int tmpY =this->poY +1; //假设可以到下一行的Y
bool isNewTurn =false;
int i=0,j=0;
//触底
if( poY == 16 )
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(this->arrBlock[this->curBlockIndex].matriData[BlockIndex].data[i][3]==1)
return true;
}
}
}
if( poY == 17 )
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(this->arrBlock[this->curBlockIndex].matriData[BlockIndex].data[i][2]==1)
return true;
}
}