① C语言设计俄罗斯方块
1、将游戏区域划分为18行*10列的棋盘,设立一个布尔型的二维数组变量,以表示棋盘上各个地方是否有方块。
2、用4个顶点代表各种不同形状的方块,一旦方块在游戏区域中,就把对应的布尔型二维数组变量置为真,表示该方格已经有方块了。
3、如上做方便方块移动是否会碰撞的判断。
4、代码已经修正了一个小BUG。
5、压缩包中的文件是未经修改的源代码,此处的代码为最新。
6、方向键上为改变形状,下为直接落到底部。p键为暂停(或者Pause键)。
② java俄罗斯方块实现新游戏、暂停、继续功能的代码怎么写
jMenuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//这里实现游戏开始
}
});
③ 用c++怎么样才能实现对俄罗斯方块自动下落的控制环境为qt+opengl
#include<windows.h>
doublelast_time=glfwGetTime(),
curr_time;
while(game_loop){
curr_time=glfwGetTime();
doubledet_time=(curr_time-last_time);
last_time=curr_time;
fps=int(1/det_time);//每秒帧数
interval=std::min(det_time,max_interval);//每一帧间隔时间
update(interval);//这里可以按照interval*speed来移动方块
render();//更新画面画出方块
Sleep(1000/max_fps);
}
差不多像上面这样吧
④ c#俄罗斯方块怎么设置关卡
1)第一个cpp:
#include "colorConsole.h"
HANDLE initiate()
{
HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
return hOutput;
}
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString)
{
DWORD cWritten;
BOOL fSuccess;
COORD coord;
coord.X = x; // start at first cell
coord.Y = y; // of first row
fSuccess = WriteConsoleOutputCharacter(
hOutput, // screen buffer handle
lpszString, // pointer to source string
lstrlen(lpszString), // length of string
coord, // first cell to write to
&cWritten); // actual number written
if (! fSuccess)
cout<<"error:WriteConsoleOutputCharacter"<<endl;
for (;fSuccess && coord.X < lstrlen(lpszString)+x; coord.X += nColors)
{
fSuccess = WriteConsoleOutputAttribute(
hOutput, // screen buffer handle
wColors, // pointer to source string
nColors, // length of string
coord, // first cell to write to
&cWritten); // actual number written
}
if (! fSuccess)
cout<<"error:WriteConsoleOutputAttribute"<<endl;
return 0;
}
(2)第二个cpp
#include <conio.h>
#include <stdlib.h>
#include<stdio.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib") //播放背景音乐的头文件
#include "colorConsole.h"
#include<time.h>
#define SQUARE_COLOR BACKGROUD_BLUE|BACKGROUD_RED| BACKGROUD_INTENSITY //背景颜色
#define SQUARE_COLOR FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY //方块的颜色
#define up 72
#define down 80
#define left 75
#define right 77
#define esc 27
#define MAPW 15 //地图的宽度
#define MAPH 25 //地图的高度
void initiate1();
int * build(); //创建方块 //初始化工作
BOOL isavailable(int a[],int x,int y,int w,int h); //判定是否能放下
void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors);
void delete_cache(); //清除键盘缓冲区
void revolve(int a[][4],int w,int h,int *x,int y); //转动方块
void pro();
void end();
void delete_blocks(int *a,int w,int h,int x,int y);
void gameover();
void deletefull_line(int m[][MAPW],int row,int w,int h); //消除一行
int dx=30,dy=5; //屏幕上的偏移量
int score=0,level=0;
int map[MAPH][MAPW];
int a1[4][4]={{1},{1,1,1}};
int a2[4][4]={{0,1},{1,1,1}};
int a3[4][4]={{1,1},{0,1,1}};
int a4[4][4]={{0,0,1},{1,1,1}};
int a5[4][4]={{0,1,1},{1,1}};
int a6[4][4]={{1,1,1,1}};
int a7[4][4]={{1,1},{1,1}};
int a[4][4];
int main()
{
HANDLE handle;
handle=initiate();
WORD wColors[1]={FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY };
while(1)
{
sndPlaySound("Resource\\Just Dance.wav",SND_LOOP|SND_ASYNC);//用异步方式播放音乐,PlaySound函数在开始播放后立即返回
system("CLS");
int n=0;
printf("目录\n1.开始游戏\n2.退出游戏\n\n\n");
scanf("%d",&n);
switch(n)
{
case 1:
system("CLS");
textout(handle,22,6,wColors+2,1,"请选择游戏等级:");
textout(handle,32,8,wColors+2,1,"1.初级");
textout(handle,32,10,wColors+2,1,"2.中级");
textout(handle,32,12,wColors+2,1,"3.高级");
while(1)
{
char choice;
choice=_getch();
if(choice=='1')
{
textout(handle,22,6,wColors+2,1,"开始游戏,初级");
textout(handle,32,8,wColors+2,1," ");
textout(handle,32,10,wColors+2,1," ");
textout(handle,32,12,wColors+2,1," ");
level=0,score=0;
Sleep(2000);
textout(handle,22,6,wColors+2,1," ");
break;
}
else if(choice=='2')
{
textout(handle,22,6,wColors+2,1,"开始游戏,中级");
textout(handle,32,8,wColors+2,1," ");
textout(handle,32,10,wColors+2,1," ");
textout(handle,32,12,wColors+2,1," ");
level=2,score=20;
Sleep(2000);
textout(handle,22,6,wColors+2,1," ");
break;
}
else if(choice=='3')
{
textout(handle,22,6,wColors+2,1,"开始游戏,高级");
textout(handle,32,8,wColors+2,1," ");
textout(handle,32,10,wColors+2,1," ");
textout(handle,32,12,wColors+2,1," ");
level=4,score=40;
Sleep(2000);
textout(handle,22,6,wColors+2,1," ");
break;
}
else if(choice!='1'&&choice!='2'&&choice!='3')
continue;
}
pro();
break;
case 2:
return 0;
default:
printf("错误,按键继续");
while(!_kbhit());
}
}
}
void pro() //游戏主题
{
initiate1();
int *b=NULL;
b=build(); //创建方块
int sign,blank,x,y;
while(1)
{
for(int i=0;i<4;i++) //复制方块
for(int j=0;j<4;j++)
if(a[i][j]=*(b+i*4+j)) blank=i;
y=1-blank;x=4;
delete_blocks(&a[0][0],4,4,16,10);
b=build();
HANDLE handle;
handle=initiate();
WORD wColors[1]={FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY };
drawblocks(b,4,4,16,10,wColors,1);
wColors[0]=SQUARE_COLOR;
drawblocks(&a[0][0],4,4,x,y,wColors,1);
delete_cache();
char string[5];
wColors[0]=FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY;
textout(handle,dx-10,8+dy,wColors,1,itoa(score,string,10));
textout(handle,dx-10,14+dy,wColors,1,itoa(level,string,10));
sign=1;
while(sign)
{
int delay=0,max_delay=100-10*level; //延迟量
while(delay<max_delay)
{
if(_kbhit()) //用if避免按住键使方块卡住
{
int draw=0;
int key=_getch();
switch (key)
{
case up:
delete_blocks(&a[0][0],4,4,x,y);
revolve(a,4,4,&x,y);
draw=1;
break;
case down:
delay=max_delay;
break;
case left:
if(isavailable(&a[0][0],x-1,y,4,4))
{
delete_blocks(&a[0][0],4,4,x,y);
x--;
draw=1;
}
break;
case right:
if(isavailable(&a[0][0],x+1,y,4,4))
{
delete_blocks(&a[0][0],4,4,x,y);
x++;
draw=1;
}
break;
case 32://32 是空格键的ASCII码,按空格键暂停
while(1)
{
textout(handle,dx,-2+dy,wColors,1,"Press any key to continue");
Sleep(200);
textout(handle,dx,-2+dy,wColors,1," ");
Sleep(200);
if(_kbhit())
{
draw=1;
break;
}
}
break;
case esc://按键退出游戏
exit(EXIT_SUCCESS);
}
if(draw)
{
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
drawblocks(&a[0][0],4,4,x,y,wColors,1);
draw=0;
}
}
_sleep(5);delay++;
}
if(isavailable(&a[0][0],x,y+1,4,4)) //是否能下移
{
delete_blocks(&a[0][0],4,4,x,y);
y++;
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
drawblocks(&a[0][0],4,4,x,y,wColors,1);
}
else
{
sign=0; //标记,使跳出 while(sign) 循环,产生新方块
if(y<=1)
{
system("CLS");
HANDLE handle;
handle=initiate();
WORD wColors[1]={FOREGROUND_RED| FOREGROUND_GREEN};
textout(handle,4+dx,6+dy,wColors,1,"GAME OVER!!!");
textout(handle,4+dx,8+dy,wColors,1,"分数:");
textout(handle,10+dx,8+dy,wColors,1,itoa(score,string,10));
textout(handle,4+dx,10+dy,wColors,1,"制作者:***");
delete_cache();
exit(EXIT_SUCCESS);
} //是否结束
for(int i=0;i<4;i++) //放下方块
for(int j=0;j<4;j++)
if(a[i][j]&&((i+y)<MAPH-1)&&((j+x)<MAPW-1))
map[i+y][j+x]=a[i][j];
int full,k=0;
for( i=y;i<min(y+4,MAPH-1);i++)
{
full=1;
for(int j=1;j<14;j++)
if(!map[i][j]) full=0;
if(full) //消掉一行
{
deletefull_line(map,i,MAPW,MAPH);
k++;
score=score+k;
level=min(score/10,9);
}
}
}
}
}
}
void initiate1() //初始化
{
int i;
for(i=0;i<25;i++)
{
map[i][0]=-2;
map[i][14]=-2;
}
for(i=0;i<15;i++)
{
map[0][i]=-1;
map[24][i]=-1;
}
map[0][0]=-3;
map[0][14]=-3;
map[24][0]=-3;
map[24][14]=-3;
HANDLE handle;
handle=initiate();
WORD wColors[1]={FOREGROUND_GREEN| FOREGROUND_BLUE|FOREGROUND_INTENSITY};
textout(handle,dx-10,6+dy,wColors,1,"SCORE");
textout(handle,dx-10,12+dy,wColors,1,"LEVEL");
textout(handle,32+dx,8+dy,wColors,1,"NEXT");
wColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY;
drawblocks(&map[0][0],15,25,0,0,wColors,1);
textout(handle,dx,dy,wColors,1,"◎═════════════◎");
wColors[0]=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY;
textout(handle,dx+8,dy+5,wColors,1,"按任意键开始");
wColors[0]=FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY ;
textout(handle,dx+7,dy-3,wColors,1,"制作者:***");
int x=_getch();
srand(time(NULL));
textout(handle,dx+8,dy+5,wColors,1," ");
}
int * build() //创建方块
{
int * a=NULL;
int c=rand()%7;
switch(c)
{
case 0:
a=&a1[0][0];break;
case 1:
a=&a2[0][0];break;
case 2:
a=&a3[0][0];break;
case 3:
a=&a4[0][0];break;
case 4:
a=&a5[0][0];break;
case 5:
a=&a6[0][0];break;
case 6:
a=&a7[0][0];break;
}
return a;
}
void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors) //画出方块
{
HANDLE handle;
handle = initiate();
int temp;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
if((temp=a[i*w+j])&&y+i>0)
{
if(temp==-3)
{
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◎");
_sleep(30);
}
else if(temp==-2)
{
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"║");
_sleep(30);
}
else if(temp==1)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◎");
else if(temp==-1)
{
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"═");
_sleep(30);
}
}
}
void delete_cache() //清除缓冲区
{
while(_kbhit())
{
_getch();
}
}
void delete_blocks(int *a,int w,int h,int x,int y) //覆盖方块
{
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
if(a[i*w+j]&&i+y>0)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,1," ");
}
void revolve(int a[][4],int w,int h,int *x,int y) //转动方块
{
int b[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
int sign=0,line=0;
for(int i=h-1;i>=0;i--)
{
for(int j=0;j<w;j++)
if(a[i][j])
{
b[j][line]=a[i][j];
sign=1;
}
if(sign)
{
line++;
sign=0;
}
}
for(i=0;i<4;i++)
if(isavailable(&b[0][0],*x-i,y,w,h))
{
*x-=i;
for(int k=0;k<h;k++)
for(int j=0;j<w;j++)
a[k][j]=b[k][j];
break;
}
}
void deletefull_line(int m[][MAPW],int row,int w,int h) //消除满行的方块
{
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
textout(handle,2+dx,row+dy,wColors,1,"﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌");
_sleep(100);
int i;
for(i=row;i>1;i--)
{
delete_blocks(&m[i][1],MAPW-2,1,1,i);
for(int j=1;j<MAPW-1;j++)
m[i][j]=m[i-1][j];
drawblocks(&m[i][1],MAPW-2,1,1,i,wColors,1);
}
for(i=1;i<MAPW-1;i++)
m[1][i]=0;
}
BOOL isavailable(int a[],int x,int y,int w,int h)
{
for(int i=max(y,1);i<y+h;i++)
for(int j=x;j<x+w;j++)
if(map[i][j]&&a[w*(i-y)+j-x])
return 0;
return 1;
}
⑤ 一个简单的c语言写的俄罗斯方块程序
1、考虑怎么存储俄罗斯方块
俄罗斯方块的形状一共有19种类型,如果拿数组来表示的话,可能会比较会浪费空间(网上有很多实现代码)
考虑到每种方块形状的范围是4 *4的小方块,用 字模点阵的方式来存储,即设置一个4行4列的数组,元素置1即代表这个位置有小
方块,元素置0即代表这个位置无小方块,这个整个的4*4的数组组成俄罗斯方块的形状。
1000
1000
1100
0000
上述4*4来表示L形状的方块。
4*4 =16 bit 正好为short类型,所以每一个方块可以用一个short类型的数据来表示。
我们把俄罗斯方块点阵的数位存在rockArray中,我们可以事先把这19种方块的字模点阵自己转化成十六进制,然后在rockArray数组的初始化时赋值进去。
但是这种方式扩展性不好,每当有一种新方块时需要改动,
所以可以写一个配置文件来表示19种方块。(RockShape.ini)
@###@###@@######1234
从配置文件中读取方块的类型的代码在(Init.h的ReadRock函数中)在下面3中解释下代码如何实现
2如何画出方块
可以使用EasyX库来画出简单的图形,
EasyX库是在VC下实现TC的简单绘图功能的一个库,这个库很容易学会(直接 网络EasyX库,里面有详细的教程)
那么如何画出方块,方块已经存储到一个short类型中了
从short中读取出,可以用一个掩码mask = 1来与short的每个bit位相与,结果为1,则画出一个小方块;
函数声明:
void DisplayRock(int rockIdx, RockLocation_t* LocatePtr, bool displayed)1
参数1:表示在数组中的下标,取出short类型的方块表示数据
参数2:表示当前坐标,即画出方块的左上角的坐标x,y
参数3:true表示画出该方块,false 表示擦除该方块。
//方块在图形窗口中的位置(即定位4*4大块的左上角坐标) typedef struct LOCATE
{ int left; int top;
} RockLocation_t;123456
3如何实现同一种类型方块的翻转,
在按‘↑’时应该翻转同一种类型的方块,
比如下面的横杆和竖杆
@###@###@###@###@@@@############****1234567891011
可以假想成静态循环链表来实现这种方式
使同一种类型的方块循环起来,
用一个struct结构来表示一种方块
typedef struct ROCK
{ //用来表示方块的形状(每一个字节是8位,用每4位表示方块中的一行)
unsigned short rockShapeBits; int nextRockIndex; //下一个方块,在数组中的下标 } RockType;123456
定义一个RockType类型的数组来存储19种方块
RockType RockArray[19] = { (0, 0) };
当我们按“↑”时,把传入画方块函数DrawRock中的rockIndex变为当前方块结构体中的nextRockIndex即可。
简单解释下ReadRock函数的实现:当读取到空行的时候表示 一种方块已经读取完毕,当读取到****行时 表示同一种类型的方块读取完毕,具体看代码实现,代码中具体的注释
4、主要游戏实现的逻辑
贴一个预览图吧
注:上述预览图的游戏控制区和游戏显示区在Draw.h的DrawGameWindow()函数实现的
(1)在初始位置画出方块,在预览区画出下一次的方块
(2)方块有两种行为:响应键盘命令UserHitKeyBoard(),自由下落
如果敲击键盘了(w ,a ,s ,d, )空格表示暂停,如果在规定时间内没有敲击键盘的话,方块自由下落一个单位
if (kbhit()) //如果敲击键盘了 就处理按键
{
userHit = getch();
UserHitKeyBoard(userHit, &curRockIndex, &curRockLocation);
} //没有 就自动下移一个单位 :不能用else,因为可能按键不是上下左右
DWORD newtime = GetTickCount(); if (newtime - oldtime >= (unsigned int)(300) && moveAbled == TRUE)
{
oldtime = newtime;
DisplayRock(curRockIndex, &curRockLocation, false);
curRockLocation.top += ROCK_SQUARE_WIDTH; //下落一格
}1234567891011121314
(3)当方块落地(即不能下移了)时,判断是否满行,如果满行则消除,然后再判断游戏是否结束,游戏结束的话,直接退出游戏
判断满行:FullLine()函数,从最底下的一行开始判断,直到遇到一行空行,
while (count != xROCK_SQUARE_NUM ) //遇到空行 14
{
linefull = true; count = 0; for (int i = 1; i <= xROCK_SQUARE_NUM; ++i)
{ if (game_board[idx][i] == 0)
{
linefull = false; count++;
}
} if (linefull) //满行,消除当前行,更新分数
{
DelCurLine(idx);//消除满行
game_socres += 3;
UpdateSocres(game_socres);
idx++;//因为下面要减1
}
idx--;
}
(4)消除满行
将要删除的满行擦除:即将方块化成与背景色相同的,该代码为黑色
然后将上面的一行向下移,移一行删除一行,直到遇到空行
具体看代码的具体实现 game.h
void DelCurLine(int rowIdx)
(4)判断方块是否能移动
在game.h中实现
bool MoveAble(int rockIndex, RockLocation_t* currentLocatePtr, int f_direction)1
**比较当前位置的坐标(左上角)开始,能否放下rockIndex的方块。
注:f_direction为”↑”的话,则传入的rockIndex为下一个方块**
如果不能移动的话,给游戏game_board设置标记表示该位置被占有
//全局变量-游戏板的状态描述(即表示当前界面哪些位置有方块) //0表示没有,1表示有(多加了两行和两列,形成一个围墙,便于判断方块是否能够移动) int game_board[yROCK_SQUARE_NUM + 2][xROCK_SQUARE_NUM + 2] = { 0 };123
实现过程遇到的一些问题
(1)在快速下落的时候,可能方块会掉出围墙的范围内,
快速下落是使方块每次下落2个单位距离。
在判断不能下落时,使当前坐标的top即y减去一个单位的距离
(2)遇到多行满行时消除不了,
在判断满行时,循环找出满行,找出一个满行,就消除一行,然后继续判断是否满行,直到遇到空行
⑥ 求俄罗斯方块游戏代码
手机游戏---俄罗斯方块http://www.onlinedown.net/soft/40673.htm
http://games.sina.com.cn/m/c/2004-10-21/1069.shtml
http://www.gamezero.cn/game8878.html
俄罗斯方块——java源代码提供
import java.awt.*;
import java.awt.event.*;
//俄罗斯方块类
public class ERS_Block extends Frame{
public static boolean isPlay=false;
public static int level=1,score=0;
public static TextField scoreField,levelField;
public static MyTimer timer;
GameCanvas gameScr;
public static void main(String[] argus){
ERS_Block ers = new ERS_Block("俄罗斯方块游戏 V1.0 Author:Vincent");
WindowListener win_listener = new WinListener();
ers.addWindowListener(win_listener);
}
//俄罗斯方块类的构造方法
ERS_Block(String title){
super(title);
setSize(600,480);
setLayout(new GridLayout(1,2));
gameScr = new GameCanvas();
gameScr.addKeyListener(gameScr);
timer = new MyTimer(gameScr);
timer.setDaemon(true);
timer.start();
timer.suspend();
add(gameScr);
Panel rightScr = new Panel();
rightScr.setLayout(new GridLayout(2,1,0,30));
rightScr.setSize(120,500);
add(rightScr);
//右边信息窗体的布局
MyPanel infoScr = new MyPanel();
infoScr.setLayout(new GridLayout(4,1,0,5));
infoScr.setSize(120,300);
rightScr.add(infoScr);
//定义标签和初始值
Label scorep = new Label("分数:",Label.LEFT);
Label levelp = new Label("级数:",Label.LEFT);
scoreField = new TextField(8);
levelField = new TextField(8);
scoreField.setEditable(false);
levelField.setEditable(false);
infoScr.add(scorep);
infoScr.add(scoreField);
infoScr.add(levelp);
infoScr.add(levelField);
scorep.setSize(new Dimension(20,60));
scoreField.setSize(new Dimension(20,60));
levelp.setSize(new Dimension(20,60));
levelField.setSize(new Dimension(20,60));
scoreField.setText("0");
levelField.setText("1");
//右边控制按钮窗体的布局
MyPanel controlScr = new MyPanel();
controlScr.setLayout(new GridLayout(5,1,0,5));
rightScr.add(controlScr);
//定义按钮play
Button play_b = new Button("开始游戏");
play_b.setSize(new Dimension(50,200));
play_b.addActionListener(new Command(Command.button_play,gameScr));
//定义按钮Level UP
Button level_up_b = new Button("提高级数");
level_up_b.setSize(new Dimension(50,200));
level_up_b.addActionListener(new Command(Command.button_levelup,gameScr));
//定义按钮Level Down
Button level_down_b =new Button("降低级数");
level_down_b.setSize(new Dimension(50,200));
level_down_b.addActionListener(new Command(Command.button_leveldown,gameScr));
//定义按钮Level Pause
Button pause_b =new Button("游戏暂停");
pause_b.setSize(new Dimension(50,200));
pause_b.addActionListener(new Command(Command.button_pause,gameScr));
//定义按钮Quit
Button quit_b = new Button("退出游戏");
quit_b.setSize(new Dimension(50,200));
quit_b.addActionListener(new Command(Command.button_quit,gameScr));
controlScr.add(play_b);
controlScr.add(level_up_b);
controlScr.add(level_down_b);
controlScr.add(pause_b);
controlScr.add(quit_b);
setVisible(true);
gameScr.requestFocus();
}
}
//重写MyPanel类,使Panel的四周留空间
class MyPanel extends Panel{
public Insets getInsets(){
return new Insets(30,50,30,50);
}
}
//游戏画布类
class GameCanvas extends Canvas implements KeyListener{
final int unitSize = 30; //小方块边长
int rowNum; //正方格的行数
int columnNum; //正方格的列数
int maxAllowRowNum; //允许有多少行未削
int blockInitRow; //新出现块的起始行坐标
int blockInitCol; //新出现块的起始列坐标
int [][] scrArr; //屏幕数组
Block b; //对方快的引用
//画布类的构造方法
GameCanvas(){
rowNum = 15;
columnNum = 10;
maxAllowRowNum = rowNum - 2;
b = new Block(this);
blockInitRow = rowNum - 1;
blockInitCol = columnNum/2 - 2;
scrArr = new int [32][32];
}
//初始化屏幕,并将屏幕数组清零的方法
void initScr(){
for(int i=0;i<rowNum;i++)
for (int j=0; j<columnNum;j++)
scrArr[j]=0;
b.reset();
repaint();
}
//重新刷新画布方法
public void paint(Graphics g){
for(int i = 0; i < rowNum; i++)
for(int j = 0; j < columnNum; j++)
drawUnit(i,j,scrArr[j]);
}
//画方块的方法
public void drawUnit(int row,int col,int type){
scrArr[row][col] = type;
Graphics g = getGraphics();
tch(type){ //表示画方快的方法
case 0: g.setColor(Color.black);break; //以背景为颜色画
case 1: g.setColor(Color.blue);break; //画正在下落的方块
case 2: g.setColor(Color.magenta);break; //画已经落下的方法
}
g.fill3DRect(col*unitSize,getSize().height-(row+1)*unitSize,unitSize,unitSize,true);
g.dispose();
}
public Block getBlock(){
return b; //返回block实例的引用
}
//返回屏幕数组中(row,col)位置的属性值
public int getScrArrXY(int row,int col){
if (row < 0 || row >= rowNum || col < 0 || col >= columnNum)
return(-1);
else
return(scrArr[row][col]);
}
//返回新块的初始行坐标方法
public int getInitRow(){
return(blockInitRow); //返回新块的初始行坐标
}
//返回新块的初始列坐标方法
public int getInitCol(){
return(blockInitCol); //返回新块的初始列坐标
}
//满行删除方法
void deleteFullLine(){
int full_line_num = 0;
int k = 0;
for (int i=0;i<rowNum;i++){
boolean isfull = true;
L1:for(int j=0;j<columnNum;j++)
if(scrArr[j] == 0){
k++;
isfull = false;
break L1;
}
if(isfull) full_line_num++;
if(k!=0 && k-1!=i && !isfull)
for(int j = 0; j < columnNum; j++){
if (scrArr[j] == 0)
drawUnit(k-1,j,0);
else
drawUnit(k-1,j,2);
scrArr[k-1][j] = scrArr[j];
}
}
for(int i = k-1 ;i < rowNum; i++){
for(int j = 0; j < columnNum; j++){
drawUnit(i,j,0);
scrArr[j]=0;
}
}
ERS_Block.score += full_line_num;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}
//判断游戏是否结束方法
boolean isGameEnd(){
for (int col = 0 ; col <columnNum; col ++){
if(scrArr[maxAllowRowNum][col] !=0)
return true;
}
return false;
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
//处理键盘输入的方法
public void keyPressed(KeyEvent e){
if(!ERS_Block.isPlay)
return;
tch(e.getKeyCode()){
case KeyEvent.VK_DOWN:b.fallDown();break;
case KeyEvent.VK_LEFT:b.leftMove();break;
case KeyEvent.VK_RIGHT:b.rightMove();break;
case KeyEvent.VK_SPACE:b.leftTurn();break;
}
}
}
//处理控制类
class Command implements ActionListener{
static final int button_play = 1; //给按钮分配编号
static final int button_levelup = 2;
static final int button_leveldown = 3;
static final int button_quit = 4;
static final int button_pause = 5;
static boolean pause_resume = true;
int curButton; //当前按钮
GameCanvas scr;
//控制按钮类的构造方法
Command(int button,GameCanvas scr){
curButton = button;
this.scr=scr;
}
//按钮执行方法
public void actionPerformed (ActionEvent e){
tch(curButton){
case button_play:if(!ERS_Block.isPlay){
scr.initScr();
ERS_Block.isPlay = true;
ERS_Block.score = 0;
ERS_Block.scoreField.setText("0");
ERS_Block.timer.resume();
}
scr.requestFocus();
break;
case button_levelup:if(ERS_Block.level < 10){
ERS_Block.level++;
ERS_Block.levelField.setText(""+ERS_Block.level);
ERS_Block.score = 0;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}
scr.requestFocus();
break;
case button_leveldown:if(ERS_Block.level > 1){
ERS_Block.level--;
ERS_Block.levelField.setText(""+ERS_Block.level);
ERS_Block.score = 0;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}
scr.requestFocus();
break;
case button_pause:if(pause_resume){
ERS_Block.timer.suspend();
pause_resume = false;
}else{
ERS_Block.timer.resume();
pause_resume = true;
}
scr.requestFocus();
break;
case button_quit:System.exit(0);
}
}
}
//方块类
class Block {
static int[][] pattern = {
{0x0f00,0x4444,0x0f00,0x4444},//用十六进至表示,本行表示长条四种状态
{0x04e0,0x0464,0x00e4,0x04c4},
{0x4620,0x6c00,0x4620,0x6c00},
{0x2640,0xc600,0x2640,0xc600},
{0x6220,0x1700,0x2230,0x0740},
{0x6440,0x0e20,0x44c0,0x8e00},
{0x0660,0x0660,0x0660,0x0660}
};
int blockType; //块的模式号(0-6)
int turnState; //块的翻转状态(0-3)
int blockState; //快的下落状态
int row,col; //块在画布上的坐标
GameCanvas scr;
//块类的构造方法
Block(GameCanvas scr){
this.scr = scr;
blockType = (int)(Math.random() * 1000)%7;
turnState = (int)(Math.random() * 1000)%4;
blockState = 1;
row = scr.getInitRow();
col = scr.getInitCol();
}
//重新初始化块,并显示新块
public void reset(){
blockType = (int)(Math.random() * 1000)%7;
turnState = (int)(Math.random() * 1000)%4;
blockState = 1;
row = scr.getInitRow();
col = scr.getInitCol();
dispBlock(1);
}
//实现“块”翻转的方法
public void leftTurn(){
if(assertValid(blockType,(turnState + 1)%4,row,col)){
dispBlock(0);
turnState = (turnState + 1)%4;
dispBlock(1);
}
}
//实现“块”的左移的方法
public void leftMove(){
if(assertValid(blockType,turnState,row,col-1)){
dispBlock(0);
col--;
dispBlock(1);
}
}
//实现块的右移
public void rightMove(){
if(assertValid(blockType,turnState,row,col+1)){
dispBlock(0);
col++;
dispBlock(1);
}
}
//实现块落下的操作的方法
public boolean fallDown(){
if(blockState == 2)
return(false);
if(assertValid(blockType,turnState,row-1,col)){
dispBlock(0);
row--;
dispBlock(1);
return(true);
}else{
blockState = 2;
dispBlock(2);
return(false);
}
}
//判断是否正确的方法
boolean assertValid(int t,int s,int row,int col){
int k = 0x8000;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if((int)(pattern[t][s]&k) != 0){
int temp = scr.getScrArrXY(row-i,col+j);
if (temp<0||temp==2)
return false;
}
k = k >> 1;
}
}
return true;
}
//同步显示的方法
public synchronized void dispBlock(int s){
int k = 0x8000;
for (int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(((int)pattern[blockType][turnState]&k) != 0){
scr.drawUnit(row-i,col+j,s);
}
k=k>>1;
}
}
}
}
//定时线程
class MyTimer extends Thread{
GameCanvas scr;
public MyTimer(GameCanvas scr){
this.scr = scr;
}
public void run(){
while(true){
try{
sleep((10-ERS_Block.level + 1)*100);
}
catch(InterruptedException e){}
if(!scr.getBlock().fallDown()){
scr.deleteFullLine();
if(scr.isGameEnd()){
ERS_Block.isPlay = false;
suspend();
}else
scr.getBlock().reset();
}
}
}
class WinListener extends WindowAdapter{
public void windowClosing (WindowEvent l){
System.exit(0);
}
}
⑦ FC 俄罗斯方块调关
选关:在游戏中按 START 键暂停,顺序输入上,下,上,下,左,右, B , B , A 即可,每按一次 A ,选一关,直到 17 关。
⑧ win32俄罗斯方块怎么暂停
用多线程,或者用ontimer函数里添加画方块的操作,当按下暂停键时就不更新画图就行了,其实ontimer函数也是vc框架提供的另一个线程
⑨ C++编俄罗斯方块:怎样用键盘控制上下翻转
这个是没有专门的函数来控制俄罗斯方块的翻转的,
一般来讲,是通过如下的方式来实现俄罗斯方块的翻转:
首先,用3维数组来存放七种基本方块图形:
例如:
int BOX[7][4][4]={
{
{1,1,1,1}, // ----型
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,1,0}, // ┌--型
{1,0,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,1,0}, // --┐型
{0,0,1,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,1,0}, // -┬-型
{0,1,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,0,0}, // -T_型
{0,1,1,0},
{0,0,0,0},
{0,0,0,0}
},
{
{0,1,1,0}, // _T-型
{1,1,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,0,0}, // 田型
{1,1,0,0},
{0,0,0,0},
{0,0,0,0}
}
};
如果希望实现用键盘控制翻转,需要做两件事情:
1.接收并响应键盘按键事件
2.在进行按键事件处理时,进行翻转
由于1比较简单,就不多说了。
对于2,其实有两种方法实现:
1.进行矩阵的转置处理(也就是右转90度),按一次键,转置一次
2.将19种变换图形也用3维数组来存放,通过数组元素的下标访问找到旋转后的形状
例如:
L型:
{
{1,0,0,0}, // L型
{1,0,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,1,1,0}, // ┌--型
{1,0,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,0,0}, // ┐型
{0,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,0,1,0}, // --┘型
{1,1,1,0},
{0,0,0,0},
{0,0,0,0}
},
为了方便读取,减少程序中算法上的开销(比如进行4*4矩阵的转置),
我建议你采取将19种变换图形也用3维数组来存放的方式
⑩ 手机上玩俄罗斯方块暂停后不知点什么按键再开始
动一动脑筋。