实现了基本的功能,解决了卡的问题
///
/*
★→☆→★→☆→★→☆→★→☆→★→☆→★→☆→★→☆
↑ 程序的版权和版本声明部分 ↓
☆ Copyright (c) 2011, 烟台大学计算机学院学生 ★
↑ All rights reserved. ↓
★ 文件名称: cubo.cpp//俄罗斯方块 ☆
↑ 作 者: 王兴锋 ↓
☆ ★
↑ 版 本 号: V 2.0 ↓
★←☆←★←☆←★←☆←★←☆←★←☆←★←☆←★←☆
*//
//编译环境:Visual C++ 6.0 / 2010, Easyx 2011惊蛰版
//编写者:王兴锋 wangxingfengcpp@sina.com
///
#include "graphics.h"
#include "iostream"
#include <conio.h>
#include <time.h>
using namespace std;
//宏定义
#define PIXEL 20//方块大小
#define WIDTH 12//游戏区的宽度
#define HEIGHT 22//游戏区的高度
//类定义
/
//定义方块类
class Cubo
{
private:
int bx,by;
int cx,cy;
int setnum, setstyle, setstyle2;
int cub[4][4], cubstore[4][4];
int tmep_cub[4][4];
/
//设置方块属性
/
//给数组赋值
void selectCuboNum(int selcet)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
cubstore[i][j]=0;
switch(selcet)
{
case 0:
//0
//11
//011
cubstore[1][0] = 1;
cubstore[1][1] = 1;
cubstore[2][1] = 1;
cubstore[2][2] = 1;
break;
case 1:
//0
//010
//111
cubstore[2][0] = 1;
cubstore[1][1] = 1;
cubstore[2][1] = 1;
cubstore[2][2] = 1;
break;
case 2:
//010
//010
//010
//010
cubstore[0][1] = 1;
cubstore[1][1] = 1;
cubstore[2][1] = 1;
cubstore[3][1] = 1;
break;
case 3:
//0000
//0110
//0110
//0000
cubstore[1][2] = 1;
cubstore[1][1] = 1;
cubstore[2][1] = 1;
cubstore[2][2] = 1;
break;
case 4:
//0
//01
//0111
cubstore[2][3] = 1;
cubstore[1][1] = 1;
cubstore[2][1] = 1;
cubstore[2][2] = 1;
break;
case 5:
//0
//001
//111
cubstore[1][2] = 1;
cubstore[2][0] = 1;
cubstore[2][1] = 1;
cubstore[2][2] = 1;
break;
case 6:
//0
//011
//11
cubstore[1][1] = 1;
cubstore[1][2] = 1;
cubstore[2][1] = 1;
cubstore[2][0] = 1;
break;
case 7:
cubstore[0][0] = 1;
}
}
//设置颜色填充类型
void setCuboStyle(int selcet)
{
switch(selcet)
{
case 0:
setfillstyle(LIGHTMAGENTA, SOLID_FILL, NULL);
break;
case 1:
setfillstyle(YELLOW, SOLID_FILL, NULL);
break;
case 2:
setfillstyle(GREEN, SOLID_FILL, NULL);
break;
case 3:
setfillstyle(RED, SOLID_FILL, NULL);
break;
case 4:
setfillstyle(MAGENTA, SOLID_FILL, NULL);
break;
case 5:
setfillstyle(LIGHTGREEN, SOLID_FILL, NULL);
break;
case 6:
setfillstyle(LIGHTCYAN, SOLID_FILL, NULL);
break;
}
}
public:
Cubo()
{
srand (( unsigned ) time ( NULL ));
setnum = rand()%7;
setstyle = rand()%7;
getCuboNum();
}
/* void intb()//检测函数
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
cout<< cub[i][j];
cout<<endl;
}
for ( i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
cout<<cubstore[i][j];
cout<<endl;
}
}*/
void setcxy(int x,int y)
{
bx = cx;
by = cy;
this->cx = x*PIXEL;
this->cy = y*PIXEL;
}
//获取方块数组cub[y][x]的值
int get_Cubostore(int x,int y)
{
return cub[y][x];
}
int get_Cubocx(int x)
{
return cx/PIXEL+x-2;
}
int get_Cubocy(int y)
{
return cy/PIXEL+y-1;
}
//获取坐标
int get_cx()
{
return cx;
}
int get_cy()
{
return cy;
}
//设置方块属性
void setCuboNum()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
cub[i][j] = cubstore[i][j];
}
void getCuboNum()
{
selectCuboNum(setnum);
// selectCuboNum(7);
}
void getCuboStyle(int x)
{
if (x)
setCuboStyle(setstyle);
else
setCuboStyle(setstyle2);
}
//画方块(按设置)
void barCubo()
{
int i, j;
setfillstyle(BLACK, SOLID_FILL, NULL);//设置黑色先画黑块
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
if (cub[i][j])
bar(bx+j*PIXEL, by+i*PIXEL, bx+PIXEL-2+j*PIXEL, by+PIXEL-2+i*PIXEL);
}
getCuboStyle(0);
for ( i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
if (cub[i][j])
bar(cx+j*PIXEL, cy+i*PIXEL, cx+PIXEL-2+j*PIXEL, cy+PIXEL-2+i*PIXEL);
}
}
void barCubostore()
{
setfillstyle(BLACK, SOLID_FILL, NULL);
bar(430+3, 30+3, 550-3, 150-3);
//setfillstyle(LIGHTRED, SOLID_FILL, NULL);
getCuboStyle(1);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
if (cubstore[i][j])
bar(cx+16*PIXEL+j*PIXEL, cy+3*PIXEL+i*PIXEL, cx+16*PIXEL+PIXEL-2+j*PIXEL, cy+3*PIXEL+PIXEL-2+i*PIXEL);
}
}
void setCubo()
{
setcxy(7, -1);
setcxy(7, -1);
setCuboNum();
setnum = rand()%7;
setstyle2 = setstyle;
setstyle = rand()%7;
getCuboNum();
barCubostore();//画备选图形
}
//控制移动方块
void onLeft()
{
bx = cx;
by = cy;
cx = cx - PIXEL;
// barCubo();
}
void onRight()
{
bx = cx;
by = cy;
cx = cx + PIXEL;
// barCubo();
}
void onDown()
{
bx = cx;
by = cy;
cy = cy + PIXEL;
barCubo();
}
void onUp()
{
int temp[4];
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
tmep_cub[x][y] = cub[x][y];
//先将原有方块覆盖
setfillstyle(BLACK, SOLID_FILL, NULL);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
if (cub[i][j])
bar(cx+j*PIXEL, cy+i*PIXEL, cx+PIXEL-2+j*PIXEL, cy+PIXEL-2+i*PIXEL);
}
for(int n=0;n<=2;n++)
temp[n]=cub[0][n];
temp[3]=cub[1][1];
for(n = 0; n <= 2; n++)
cub[0][n] = cub[3-n][0];
for(n = 0; n <= 2; n++)
cub[3-n][0] = cub[3][3-n];
for(n = 0; n <= 2; n++)
cub[3][3-n] = cub[n][3];
for(n = 0; n <= 2; n++)
cub[n][3] = temp[n];
cub[1][1] = cub[2][1];
cub[2][1] = cub[2][2];
cub[2][2] = cub[1][2];
cub[1][2] = temp[3];
// barCubo();//画图
}
void onUpBack()
{
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
cub[x][y] = tmep_cub[x][y];
}
};
//定义界面类
class InterFace
{
private:
int Ix,Iy;
int vessel[HEIGHT][WIDTH+2];
//设置容器坐标
void setInter(int x,int y)
{
Ix = x*PIXEL;
Iy = y*PIXEL;
}
public:
InterFace()//构造函数
{
// inti();
}
//设置容器值
void setVess(int x,int y)
{
vessel[y][x] = 1;
}
void setVess0(int x,int y)
{
vessel[y][x] = 0;
}
int get_Vessstore(int x,int y)
{
return vessel[y][x];
}
//给容器赋值
void getVess()
{
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH+2; j++)
{
vessel[i][j] = 0;
}
for (i = 0; i < HEIGHT; i++)
{
vessel[i][13] = 2;
vessel[i][0] = 2;
if (i < 22)
{
vessel[21][i] = 2;
}
}
}
//画容器
void barVess()
{
setfillstyle(BLUE, SOLID_FILL, NULL);
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH+2; j++)
{
if (vessel[i][j] == 2)
{
bar(Ix+j*PIXEL, Iy+i*PIXEL, Ix+PIXEL-2+j*PIXEL, Iy+PIXEL-2 + i*PIXEL);
}
}
}
//画备选
void barStock()
{
setcolor(LIGHTGREEN);
setlinestyle(PS_SOLID, NULL, 3);
rectangle(430, 30, 550, 150);
}
//输出成绩
void outScore()
{
setfont(0,0,0,0,0,TRUE,FALSE,FALSE,"隶书"); //文字设为宋体
outtextxy(430,180,"成绩:说的发生地方 0");
}
//初始化图形界面
void inti()
{
initgraph(640, 480);//生成界面
cleardevice();//清屏
setInter(2,1);
getVess();
barVess();
barStock();
outScore();
}
};
///
//定义对象
Cubo cu;//方块对象
InterFace inter;//容器对象
///
///
//函数声明
///
void control();//控制方块移动
int onBlank();//按空格
bool detectDown();//碰撞检测下
bool detectLeft();//碰撞检测左
bool detectRight();//碰撞检测右
void set_DeVess();//检测到碰撞给容器赋值
void clearCubo();//检测容器是否已满并消行
//
void jiance();
///
//主函数//
///
void main()
{
//生成种子时间
inter.inti();//初始化界面
// cu.initCubo();
while(true)
{
cu.setCubo();//设置数据
// cu.intb();
// cu.barCubostore();
control();//控制方块
clearCubo();//消行
}
// jiance();
getchar();
//关闭图形模式
closegraph();
}
/主函数结束
/
//定义函数
/
//控制方块移动
void control()
{
bool pass = false;
bool right, left;
int cycle_time = 0;//循环次数
while(true)
{
//键盘控制区;
if(kbhit())
{
switch(getch())
{
case 'a':
case 'A':
if (detectLeft())break;
cu.onLeft();
break;
case 'd':
case 'D':
if (detectRight())break;
cu.onRight();
break;
case 's':
case 'S':
if(detectDown())//检测
{
set_DeVess();//赋值
break;
}
cu.onDown();
cu.barCubo();//画方块
break;
case 'w':
case 'W':
left = false;
right = false;
cu.onUp();
while(detectLeft())
{
cu.onRight();
left = true;
}
if (left)
cu.onLeft();
while(detectRight())
{
cu.onLeft();
right = true;
}
if (right)
cu.onRight();
/* if (right || left)
inter.barVess();*/
if (detectDown())
cu.onUpBack();
break;
case ' ':
pass = true;
break;
}
}
cu.barCubo();//画方块
if (cycle_time > 20 || pass)//循环20次也就是用时800ms下降一次
{
if(detectDown())//检测
{
set_DeVess();//赋值
break;
}
cu.onDown();//方块下落
cycle_time = 0;
}
if(!pass)
Sleep(40);
cycle_time = cycle_time + 1;
}
}
//按空格
int onBlank()
{
for (int j = 0; j < 20; j++)
{
cu.onDown();
}
return HEIGHT;
}
///检测///
//碰撞检测
bool detectDown()
{
for (int i = 3; i >= 0; i--)
for (int j = 0; j < 4; j++)
if (cu.get_Cubostore(j, i))
{
if (inter.get_Vessstore(cu.get_Cubocx(j), cu.get_Cubocy(i)+1))
return true;
}
return false;
}
bool detectLeft()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (cu.get_Cubostore(j, i))
{
if (cu.get_Cubocx(j) < 2)
return true;
if (inter.get_Vessstore(cu.get_Cubocx(j)-1, cu.get_Cubocy(i)))
return true;
}
return false;
}
bool detectRight()
{
for (int i = 3; i >= 0; i--)
for (int j = 0; j < 4; j++)
if (cu.get_Cubostore(i, j))
{
if (cu.get_Cubocx(i) > 15)
return true;
if (inter.get_Vessstore(cu.get_Cubocx(i)+1, cu.get_Cubocy(j)))
return true;
}
return false;
}
//给vess赋值
void set_DeVess()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(cu.get_Cubostore(j,i))
inter.setVess(cu.get_Cubocx(j), cu.get_Cubocy(i)) ;
}
}
//检测容器是否已满并消行
void clearCubo()
{
int i;
bool pass = true;
for (i = 20; i >= 0; i--)
{
pass = true;
for (int j = 1; j < WIDTH+1; j++)
{
if (!inter.get_Vessstore(j ,i))
{
pass = false;
break;
}
}
//擦掉已满行数
if (pass)
{
// cout<<i<<endl;
i++;
//消行效果
setfillstyle(YELLOW, DIAGCROSS3_FILL, NULL);
bar((WIDTH+3)*PIXEL, (i+1)*PIXEL-1-2, 3*PIXEL, i*PIXEL);
Sleep(100);
setfillstyle(YELLOW, VERTICAL3_FILL, NULL);
bar((WIDTH+3)*PIXEL, (i+1)*PIXEL-1-2, 3*PIXEL, i*PIXEL);
Sleep(100);
setfillstyle(YELLOW, CROSS3_FILL, NULL);
bar((WIDTH+3)*PIXEL, (i+1)*PIXEL-2, 3*PIXEL, i*PIXEL);
Sleep(100);
setfillstyle(YELLOW, DOT3_FILL, NULL);
bar((WIDTH+3)*PIXEL, (i+1)*PIXEL-1-2, 3*PIXEL, i*PIXEL);
Sleep(100);
//图像移动
IMAGE img;
getimage(&img, 3*PIXEL, PIXEL, 15*PIXEL, (i-1)*PIXEL);
putimage(3*PIXEL, 2*PIXEL, &img);
//给容器赋值
// for (int n = 1; n < 13; n++)
// inter.vessel[i][n] = 0;
for (int x = i-1; x >= 1; x--)
{
for (int y = 1; y < 13; y++)
{
if (inter.get_Vessstore(y, x-1))
inter.setVess(y, x);
else
inter.setVess0(y ,x);
// inter.vessel[x][y] = inter.vessel[x-1][y];
}
}
}//if(pass)
}
}
///
///
///
///
///
///
///
///
void jiance()
{
int i,j;
inter.getVess();
cu.setCubo();//初始化数据
/* for(i=0;i<4;i++)
{
for( j=0;j<4;j++)
cout<<cu.get_Cubostore(j,i);
cout<<endl;
}*/
while(true)
{
cu.setCubo();//初始化数据
control();
for(i=0;i<HEIGHT;i++)
{
for(int j=0;j<(WIDTH+2);j++)
{
cout<<inter.get_Vessstore(j,i);
}
cout<<i;
cout<<endl;
}
clearCubo();//消行
}
}
点击下载源代码(运行环境:VS 6.0)