俄罗斯方块,求指教
java吧
全部回复
仅看楼主
level 1
dj7vg 楼主

总共3个类,一个线程类。好多效果还没实现。现在最主要的问题是新方块落下后如果它左边有旧方块,那新方块就会全部被着色。附上代码,求指教[扯花]
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class EGame extends JFrame implements paintListener
{
private JMenuBar bar = new JMenuBar();
JMenu mgame = new JMenu("游戏"),mcontrol = new JMenu("控制"),
mstyle = new JMenu("风格"),mhelp = new JMenu("帮助");
JMenuItem mnewgame = new JMenuItem("新游戏"),
mexit = new JMenuItem("退出");
//设置窗口风格
JCheckBoxMenuItem mwindows = new JCheckBoxMenuItem("Windows"),
mMotif = new JCheckBoxMenuItem("Motif"),
mMetal = new JCheckBoxMenuItem("Metal",true);
void createMenu()
{
bar.add(mgame);
bar.add(mcontrol);
bar.add(mstyle);
bar.add(mhelp);
mgame.add(mnewgame);
mgame.add(mexit);
mstyle.add(mwindows);
mstyle.add(mMotif);
mstyle.add(mMetal);
setJMenuBar(bar);
mexit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
//顶部5个按钮实现操作功能
private JButton
bl = new JButton("左"),
br = new JButton("右"),
bd = new JButton("下"),

bp
= new JButton("变"),
bs = new JButton("开始");
private JPanel jpanel=new JPanel();
//设置容器大小为18X12的空间
int color[][]=new int[18][12];
JButton button[][]=new JButton[18][12];
JPanel boxPanel=new JPanel(new GridLayout(18, 12));
//声明并实例化方块类
EBlock eblock=new EBlock();
private void setTopButton()
{//定义控件的响应事件
bl.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
eblock.moveLeft();}
});
br.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
eblock.moveRight();}
});
bd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
eblock.moveDown();}
});
bp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
eblock.turnAround();}
});
bs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
new EGame();}
});
jpanel.setLayout(new FlowLayout());
jpanel.add(bl);
jpanel.add(br);
jpanel.add(bd);
jpanel.add(bp);
jpanel.add(bs);
}
//键盘响应
public class ControlKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
eblock.moveDown();
break;
case KeyEvent.VK_LEFT:

2011年09月15日 07点09分 1
level 1
dj7vg 楼主
int x=X,y=Y;
if(ncol==3)
{ncol=0;}
else
{
ncol++;
int key = 0x8000;
for(int i=0;i<4;i++)
{
for(int j =0;j<4;j++)
{
if(((key & STYLES[row][ncol])!=0)||(key&STYLES[row][ncol-1])!=0)
{
if(x+i>=17||x+j<0||x+j>11||IsMovable(x,y)==false)
{
flag=false;
}
}
key>>=1;
}
}
}
if(flag)
{
col=ncol;
}
notifyListenters(this);
}
//判断是否可以移动
public boolean IsMovable(int x,int y)
{
int key=0x8000;
boolean flag = true;
for(int i=0;i<4;i++)
{
for(int j =0;j<4;j++)
{
if((key & STYLES[row][col])!=0)
{
int n;
if(y+i>=17)n=17;
else if(y+i<0) n =0;
else{n=y+i;}
if(x+j<0||x+j>11||y+i>17||button[n][x+j]==1)
{
flag = false;
}
}
key>>=1;
}
}
return flag;
}
public void setlabel(int x,int y)
{
button[x][y]=1;
}
public int getlabel(int n,int m)
{
int k;
k=button[n][m];
return k;
}
private void display()
{}
// 随机产生新的方块
public void newBlock(){
X=4;Y=0;
row=(int) (Math.random() * 6);
col=(int) (Math.random() * 3);
notifyListenters(this);
}
public final static int[][] STYLES = {// 共28种状态
{0x0f00, 0x4444, 0x0f00, 0x4444}, // 长条型的四种状态
{0x04e0, 0x0464, 0x00e4, 0x04c4}, // 'T'型的四种状态
{0x4620, 0x6c00, 0x4620, 0x6c00}, // 反'Z'型的四种状态
{0x2640, 0xc600, 0x2640, 0xc600}, // 'Z'型的四种状态
{0x6220, 0x1700, 0x2230, 0x0740}, // '7'型的四种状态
{0x6440, 0x0e20, 0x44c0, 0x8e00}, // 反'7'型的四种状态
{0x0660, 0x0660, 0x0660, 0x0660}, // 方块的四种状态
};
}
---------------还有个线程类----------------
这就不用了吧。
2011年09月15日 07点09分 4
level 2
初学者表示看不懂
2011年09月15日 08点09分 5
level 1
dj7vg 楼主
我也是初学者啊,完全不知道哪里的问题。问老师,老师也看不懂,说下午再来教我。结果他下午干脆就不来了[瀑布汗~]
2011年09月15日 08点09分 6
level 6
看不懂哦!
2011年09月15日 08点09分 7
level 7
[啊!]不懂啊
2011年09月15日 13点09分 8
level 1
新手学习php的朋友,给你个学习的方法,先要把最基本的语法知识搞懂,再就是在网上找个好点的源码,你在看源码的过程中会遇到很多问题,这也就是你进步的时候,你最好是将你的问题独立解决。我一般解决方法就是,去各大搜索引擎找解决问题的办法,有时一个搜索引擎找不到答案,需切换到另一个搜索引擎,非常麻烦,因为,需重新输入网址与关键字.我分享个好的搜索引擎www.gokeep.com它整合了各大搜索引擎,使用非常方便。希望对你有所帮助哦
2011年09月16日 03点09分 9
1