这是关于俄罗斯方块的游戏程序 运行时出现问题 但不知道是什么问题 求高手解答
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game extends Frame implements ActionListener{
private Button left=new Button("left");
private Button right=new Button("right");
private Button turn=new Button("turn");
CreateBlack s=new CreateBlack();
Game(){
super("俄罗斯方块第一版 ");
setSize(600,600);
Panel buttons=new Panel();
setLayout(new FlowLayout());
buttons.add(right);buttons.add(left);
buttons.add(turn);
setLayout(new BorderLayout());
add("North",buttons);
add("Center",s);
left.addActionListener(this);
right.addActionListener(this);
turn.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==right){
s.left();
}
if(e.getSource()==left){
s.right();
}
if(e.getSource()==turn){
s.turn();
}
} public static void main(String args[]){
Game a=new Game();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
class CreateBlack extends Canvas{
int x,y,i,j,flag;
int map[][]=new int[13][23];
int sharp,state;
private final int sharps[][][]=new int[][][]{
{{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},//长棍
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
{{0,0,0,0,0,1,1,0,0,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,1,0,0,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,1,1,0,0,0,0,1,1,0,0,0,0},//左z
{0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},
{0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0}},
{{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},// 右z
{0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0}},
{{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},//右f
{0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}},
{{0,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0},//左f
{0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
{{0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0},//t
{0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0},
{0,0,1,0,0,0,1,1,0,0,1,0,0,0,0,0}}};
CreateBlack(){
newBlack();
drawWall();
newMap();
Timer time=new Timer(1000,new TimeListener());
time.start();
}
public void newBlack(){//产生新图形
sharp=((int)(Math.random()*1000))%7;
state=((int)(Math.random()*1000))%4;
x=4;
y=0;
if(gameOver(x,y)){
drawWall();
newMap();
}
}
public void drawWall(){//画边界
for(i=0;i<13;i++)
map[i][0]=2;
for(j=0;j<21;j++){
map[0][j]=2;
map[12][j]=2;
}
}
public void newMap(){//初始化地图
for(i=0;i<13;i++)
for(j=0;j<23;j++)
map[i][j]=0;
}
public boolean check(int x,int y,int sharp,int state){//判断是否合法
int a,b;
for(a=0;a<4;a++)
{for(b=0;b<4;b++)
if(((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==1))||((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==2)))
return false;
}
return true;
}
public boolean gameOver(int x,int y){//判断挂的方法
if(check(x,y,sharp,state))
return true;
return false;
}
public void deline(){//消行
int a,b,c=0;
for(a=0;a<22;a++){
for(b=0;b<12;b++)
if(map[a][b]==1)
c++;
if(c==10){
for(int d=a;d<0;d--){
for(int e=0;e<11;e++)
map[d][e]=map[d][e-1];
}
}
}
}
public void addBlack(int x,int y,int sharp,int state){//添加
int j=0;
for(int a=0;a<4;a++){
for(int b=0;b<4;b++){
if(map[x+b+1][y+a]==0)
map[x+b+1][y+a]=sharps[sharp][state][j];
}
j++;
}
}
public void paint(Graphics g){
super.paint(g);
for(i=0;i<16;i++){
if(sharps[sharp][state][j]==1){
g.fillRect((j%4+1+x)*10,(j/4+y)*10,10,10);
}
}
for(int a=0;a<12;a++){
for(int b=0;b<22;b++)
if(map[a][b]==1){
g.setColor(Color.black);
g.fillRect(a*10,b*10,10,10);
}
}
} public void turn(){//变形
int turnState;
turnState=state;
state=(turnState%4+1);
if(check(x,y,sharp,state)){
}
if(check(x,y,sharp,state)){
state=turnState;
}
repaint();
}
public void left(){左移
if(check(x-1,y,sharp,state)){
x=x-1;
repaint();
}
}
public void right(){//右移
if(check(x+1,y,sharp,state)){
x=x=1;
repaint();
}
}
class TimeListener implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
if(check(x,y+1,sharp,state)){
y=y+1;
deline();
}
if(!check(x,y+1,sharp,state)){
if(flag==1){
addBlack(x,y,sharp,state);
deline();
newBlack();
flag=0;
}
flag=1;
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game extends Frame implements ActionListener{
private Button left=new Button("left");
private Button right=new Button("right");
private Button turn=new Button("turn");
CreateBlack s=new CreateBlack();
Game(){
super("俄罗斯方块第一版 ");
setSize(600,600);
Panel buttons=new Panel();
setLayout(new FlowLayout());
buttons.add(right);buttons.add(left);
buttons.add(turn);
setLayout(new BorderLayout());
add("North",buttons);
add("Center",s);
left.addActionListener(this);
right.addActionListener(this);
turn.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==right){
s.left();
}
if(e.getSource()==left){
s.right();
}
if(e.getSource()==turn){
s.turn();
}
} public static void main(String args[]){
Game a=new Game();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
class CreateBlack extends Canvas{
int x,y,i,j,flag;
int map[][]=new int[13][23];
int sharp,state;
private final int sharps[][][]=new int[][][]{
{{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},//长棍
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
{{0,0,0,0,0,1,1,0,0,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,1,0,0,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,1,1,0,0,0,0,1,1,0,0,0,0},//左z
{0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},
{0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0}},
{{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},// 右z
{0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0}},
{{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},//右f
{0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}},
{{0,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0},//左f
{0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
{{0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0},//t
{0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0},
{0,0,1,0,0,0,1,1,0,0,1,0,0,0,0,0}}};
CreateBlack(){
newBlack();
drawWall();
newMap();
Timer time=new Timer(1000,new TimeListener());
time.start();
}
public void newBlack(){//产生新图形
sharp=((int)(Math.random()*1000))%7;
state=((int)(Math.random()*1000))%4;
x=4;
y=0;
if(gameOver(x,y)){
drawWall();
newMap();
}
}
public void drawWall(){//画边界
for(i=0;i<13;i++)
map[i][0]=2;
for(j=0;j<21;j++){
map[0][j]=2;
map[12][j]=2;
}
}
public void newMap(){//初始化地图
for(i=0;i<13;i++)
for(j=0;j<23;j++)
map[i][j]=0;
}
public boolean check(int x,int y,int sharp,int state){//判断是否合法
int a,b;
for(a=0;a<4;a++)
{for(b=0;b<4;b++)
if(((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==1))||((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==2)))
return false;
}
return true;
}
public boolean gameOver(int x,int y){//判断挂的方法
if(check(x,y,sharp,state))
return true;
return false;
}
public void deline(){//消行
int a,b,c=0;
for(a=0;a<22;a++){
for(b=0;b<12;b++)
if(map[a][b]==1)
c++;
if(c==10){
for(int d=a;d<0;d--){
for(int e=0;e<11;e++)
map[d][e]=map[d][e-1];
}
}
}
}
public void addBlack(int x,int y,int sharp,int state){//添加
int j=0;
for(int a=0;a<4;a++){
for(int b=0;b<4;b++){
if(map[x+b+1][y+a]==0)
map[x+b+1][y+a]=sharps[sharp][state][j];
}
j++;
}
}
public void paint(Graphics g){
super.paint(g);
for(i=0;i<16;i++){
if(sharps[sharp][state][j]==1){
g.fillRect((j%4+1+x)*10,(j/4+y)*10,10,10);
}
}
for(int a=0;a<12;a++){
for(int b=0;b<22;b++)
if(map[a][b]==1){
g.setColor(Color.black);
g.fillRect(a*10,b*10,10,10);
}
}
} public void turn(){//变形
int turnState;
turnState=state;
state=(turnState%4+1);
if(check(x,y,sharp,state)){
}
if(check(x,y,sharp,state)){
state=turnState;
}
repaint();
}
public void left(){左移
if(check(x-1,y,sharp,state)){
x=x-1;
repaint();
}
}
public void right(){//右移
if(check(x+1,y,sharp,state)){
x=x=1;
repaint();
}
}
class TimeListener implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
if(check(x,y+1,sharp,state)){
y=y+1;
deline();
}
if(!check(x,y+1,sharp,state)){
if(flag==1){
addBlack(x,y,sharp,state);
deline();
newBlack();
flag=0;
}
flag=1;
}
}
}
}
解决方案 »
- 父类引用指向子类对象的问题
- java 把一些常量声明放到一个文件中
- 前辈们快进!!!!!!!前辈们快进!!!!
- oracle怎么设置自动增长键?
- 请教javac编译支持路径,为什么java运行就不支持???
- ****使用CVS后图片读取出错!——那位仁兄给指点一下吧!
- JavaAPI查找工具JawaAPIHelper发布了,这下学习查找API方便了。
- 当用rs=stmt.executeQuery(strsql)返回的rs是空时,怎样得知rs为空呢?
- 急问:IE中浏览applet时,提示找不到oracle的jdbc驱动程序?
- jboss-3.0.1_tomcat-4.0.4的默认WEB目录在哪儿?
- 今天突然想到了一个问题,关于java如何实现对文件分配表的操作
- 人员管理系统 请高手进! 急求!!java se
import java.awt.event.*;
import javax.swing.*;public class Game extends Frame implements ActionListener
{
private Button left=new Button("left");
private Button right=new Button("right");
private Button turn=new Button("turn");
CreateBlack s=new CreateBlack();
Game()
{
super("俄罗斯方块第一版 ");
setSize(600,600);
Panel buttons=new Panel();
setLayout(new FlowLayout());
buttons.add(right);buttons.add(left);
buttons.add(turn);
setLayout(new BorderLayout());
add("North",buttons);
add("Center",s);
left.addActionListener(this);
right.addActionListener(this);
turn.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==right)
{
s.left();
}
if(e.getSource()==left)
{
s.right();
}
if(e.getSource()==turn)
{
s.turn();
}
} public static void main(String args[])
{
Game a=new Game();
}
}class CreateBlack extends Canvas
{
int x,y,i,j,flag;
int map[][]=new int[13][23];
int sharp,state;
private final int sharps[][][]=new int[][][] {
{{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},//长棍
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
{{0,0,0,0,0,1,1,0,0,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,1,0,0,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,1,1,0,0,0,0,1,1,0,0,0,0},//左z
{0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},
{0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0}},
{{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},// 右z
{0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0}},
{{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},//右f
{0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}},
{{0,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0},//左f
{0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
{{0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0},//t
{0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0},
{0,0,1,0,0,0,1,1,0,0,1,0,0,0,0,0}}};
CreateBlack()
{
newBlack();
drawWall();
newMap();
Timer time=new Timer(1000,new TimeListener());
time.start();
}
public void newBlack()
{//产生新图形
sharp=((int)(Math.random()*1000))%7;
state=((int)(Math.random()*1000))%4;
x=4;
y=0;
if(gameOver(x,y))
{
drawWall();
newMap();
}
}
public void drawWall()
{//画边界
for(i=0;i<13;i++)
map[i][0]=2;
for(j=0;j<21;j++)
{
map[0][j]=2;
map[12][j]=2;
}
}
public void newMap()
{//初始化地图
for(i=0;i<13;i++)
for(j=0;j<23;j++)
map[i][j]=0;
}
public boolean check(int x,int y,int sharp,int state)
{//判断是否合法
int a,b;
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
if(((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==1))||((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==2)))
return false;
}
return true;
}
public boolean gameOver(int x,int y)
{//判断挂的方法
if(check(x,y,sharp,state))
return true;
return false;
}
public void deline()
{//消行
int a,b,c=0;
for(a=0;a<22;a++)
{
for(b=0;b<12;b++)
if(map[b][a]==1) //a和b反了
c++;
if(c==10)
{
for(int d=a;d<0;d--)
{
for(int e=0;e<11;e++)
map[d][e]=map[d][e-1];
}
}
}
}
public void addBlack(int x,int y,int sharp,int state)
{//添加
int j=0;
for(int a=0;a<4;a++)
{
for(int b=0;b<4;b++)
{
if(map[x+b+1][y+a]==0)
map[x+b+1][y+a]=sharps[sharp][state][j];
}
j++;
}
}
public void paint(Graphics g)
{
super.paint(g);
for(j=0;j<16;j++) //这里原来是i
{
if(sharps[sharp][state][j]==1)
{
g.fillRect((j%4+1+x)*10,(j/4+y)*10,10,10);
}
}
for(int a=0;a<12;a++)
{
for(int b=0;b<22;b++)
if(map[a][b]==1)
{
g.setColor(Color.black);
g.fillRect(a*10,b*10,10,10);
}
}
}
public void turn()
{//变形
int turnState;
turnState=state;
state=(turnState%4+1);
if(check(x,y,sharp,state))
{
}
if(check(x,y,sharp,state))
{
state=turnState;
}
repaint();
}
public void left()
{//左移
if(check(x-1,y,sharp,state))
{
x=x-1;
repaint();
}
}
public void right()
{//右移
if(check(x+1,y,sharp,state))
{
x=x=1;
repaint();
}
}
class TimeListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
repaint();
if(check(x,y+1,sharp,state))
{
y=y+1;
deline();
}
if(!check(x,y+1,sharp,state))
{
if(flag==1)
{
addBlack(x,y,sharp,state);
deline();
newBlack();
flag=0;
}
flag=1;
}
}
}
}