俄罗斯方块的实现

package www.csdn.net.frame;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;


public class IndexJFrame extends JFrame {
	//构造器:方法名与类名相同并且没有返回值
	public Font type;
	
public IndexJFrame() {
	this.setTitle("俄罗斯方块游戏");
	this.setVisible(true);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setResizable(false);
	this.setBounds(500,100,400,435);
	URL url=this.getClass().getClassLoader().getResource("images/game_start.png");
	
	ImageIcon starIcon=new ImageIcon(url);
	
	ImageIcon endIcon=new ImageIcon(this.getClass().getClassLoader().getResource("images/exit.png"));
	JButton starBtn=new JButton(starIcon);
	JButton endBtn=new JButton(endIcon);
	starBtn.setBounds(125,130,136,40);
	endBtn.setBounds(125,230, 136,40);
	
	type=new Font("隶书", Font.BOLD,40);
	JLabel you=new JLabel("俄罗斯方块游戏");
	you.setFont(type);	
	
	you.setForeground(Color.BLUE);
	you.setBounds(50,30,350,90);
	//you.setMaximumSize(Dimension  g);
	//you.setFont(Font.LAYOUT_NO_LIMIT_CONTEXT)
	
	Container container=this.getContentPane();
	container.setLayout(null);
	container.add(starBtn);
	container.add(endBtn);
	container.add(you);
	
	starBtn.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {		       
			    IndexJFrame.this.dispose();
				new  GameMain();
		}
	});
	endBtn.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
		System.exit(0);					
		}
	});
}

public static void main(String[] args) {
	new IndexJFrame();
}

}
package www.csdn.net.frame;

import javax.swing.JFrame;

public class GameMain extends JFrame{
public GameMain() {
	super("俄罗斯方块");
	setVisible(true);
	/*setSize(380,420);
	setLocation(200,200);*/
	setLocationRelativeTo(null);
	setResizable(false);
	setBounds(500, 100,380,420);
	
	GameWin g=new GameWin();
	add(g);
	
	
}
public static void main(String[] args) {
	new GameMain();	
}
}

package www.csdn.net.frame;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GameWin extends JPanel  implements ActionListener,KeyListener{
	JButton newGame=new JButton("开始");
	JButton endGame=new JButton("退出");
	int fenshu=0,speed=0,tFenShu=0;
	GameAct[] act=new GameAct[4];
	GameAct[] actTemp=new GameAct[4];
	Random random=new Random();
	boolean start=false;
	int[][]  map=new int [10][18];
	Timer t;
	int temp;
	JDialog dialog=new JDialog();
	JLabel label=new JLabel();
	JButton button=new JButton("ok");
public GameWin() {
	for (int i = 0; i < act.length; i++) {
		act[i]=new GameAct();
	}
	for (int i = 0; i < act.length; i++) {
		actTemp[i]=new GameAct();
	}
	for (int i = 0; i <10; i++) {
		for (int j = 0; j <18; j++) {
			map[i][j]=0;
		}
	}
	setLayout(new FlowLayout(FlowLayout.RIGHT));
	add(newGame);
	add(endGame);
	newGame.addActionListener(this);
	endGame.addActionListener(this);
	addKeyListener(this);
	dialog.setLayout(new GridLayout(2,1));
	dialog.add(label);
	dialog.add(button);
	button.addActionListener(this);
	dialog.setLocationRelativeTo(null);
	dialog.setSize(200,100);
	dialog.setVisible(false);
}
@Override
public void paint(Graphics g) {
	// TODO Auto-generated method stub
	super.paint(g);
	g.drawRect(9, 10,200,360);
    g.drawString("分数"+fenshu,220,60);
    g.drawLine(220,65,360,65);
    g.drawString("速度"+speed,220,90);
    g.drawLine(220,95,360,95);
    g.drawString("下一个方块",250,120);
    g.drawString("俄罗斯方块",220,340);
    g.drawString("某某",220,360);
    /
    if (start) {
    	g.setColor(new Color(255,0,0));
		for (int i = 0; i <4; i++) {
			g.fillRect(10+act[i].x*20,10+act[i].y*20, 20, 20);
		}
		for (int i = 0; i <10; i++) {
			for (int j = 0; j <18; j++) {
				if (map[i][j]==1) {
					g.fillRect(10+i*20,10+j*20, 20, 20);
				}
			}
		}
		g.setColor(Color.blue);
		for (int i = 0; i < 4; i++) {
			g.fillRect(220+actTemp[i].x*20,160+actTemp[i].y*20, 20, 20);
		}
	}
    
}
public void nextAct() {
	switch (temp) {
	case 0:
		actTemp[0].x=3;actTemp[0].y=0;
		actTemp[1].x=4;actTemp[1].y=0;
		actTemp[2].x=2;actTemp[2].y=1;
		actTemp[3].x=3;actTemp[3].y=1;		
		break;
	case 1:
		actTemp[0].x=1;actTemp[0].y=0;
		actTemp[1].x=2;actTemp[1].y=0;
		actTemp[2].x=3;actTemp[2].y=0;
		actTemp[3].x=4;actTemp[3].y=0;		
		break;
	case 2:
		actTemp[0].x=2;actTemp[0].y=0;
		actTemp[1].x=3;actTemp[1].y=0;
		actTemp[2].x=2;actTemp[2].y=1;
		actTemp[3].x=3;actTemp[3].y=1;		
		break;
	case 3:
		actTemp[0].x=2;actTemp[0].y=0;
		actTemp[1].x=2;actTemp[1].y=1;
		actTemp[2].x=2;actTemp[2].y=2;
		actTemp[3].x=3;actTemp[3].y=2;		
		break;
	case 4:
		actTemp[0].x=3;actTemp[0].y=0;
		actTemp[1].x=3;actTemp[1].y=1;
		actTemp[2].x=3;actTemp[2].y=2;
		actTemp[3].x=2;actTemp[3].y=2;		
		break;
	case 5:
		actTemp[0].x=3;actTemp[0].y=0;
		actTemp[1].x=2;actTemp[1].y=1;
		actTemp[2].x=3;actTemp[2].y=1;
		actTemp[3].x=4;actTemp[3].y=1;		
		break;
	case 6:
		actTemp[0].x=2;actTemp[0].y=0;
		actTemp[1].x=3;actTemp[1].y=0;
		actTemp[2].x=3;actTemp[2].y=1;
		actTemp[3].x=4;actTemp[3].y=1;		
		break;
	}

}
public void Move(int x, int y) {
	if (minYes(x,y)) {		
		for (int i = 0; i < 4; i++) {
			act[i].x+=x;
			act[i].y+=y;
		}
	}
	repaint();
}
//告诉你消了几行
private int delact() {
	int line=0;
	for (int i = 0; i < 18; i++) {
		int j;
		for (j = 0; j < 10; j++) {
			if (map[j][i]==0) {
				break;
			}
		}
		if (j>=10) {
			line+=1;
			if (i!=0) {
				for (int j2 = i-1; j2 >0; j2--) {
					for (int k = 0; k <10; k++) {
						map[k][j2+1]=map[k][j2];
					}
				}
				for (int j2 = 0; j2 < 10; j2++) {
					map[0][j2]=0;
				}
			}
		}
	}
	repaint();
	return line;
}
private void down() {
	if (minYes(0, 1)) {
		for (int i = 0; i <4; i++) {
			act[i].y+=1;
		}
		repaint();
	}else{
		t.stop();
		for (int i = 0; i < 4; i++) {
			map[act[i].x][act[i].y]=1;
		}
		int line=delact();
		if (line!=0) {
			fenshu+=fenshu+10*line*line;
		    if (fenshu-tFenShu>=300) {
				tFenShu=fenshu;
				if (speed<=9) {
					speed+=1;
				}
			}
		}
		if (!newAct()) {
           temp=random.nextInt(7);
           nextAct();
           t.start();
          repaint();
		}else{
			//挂掉了
			System.out.println("挂了");
		    t.stop();
		    label.setText("游戏结束,您的分数是"+fenshu);
		    dialog.setVisible(true);
		}
		repaint();
	}
}
public void up(){
	GameAct[] tt=new GameAct[4];
	for(int i=0;i<tt.length;i++){
		tt[i]=new GameAct();
		tt[i].x=act[i].x;
		tt[i].y=act[i].y;
	}
	int cx=(tt[0].x+tt[1].x+tt[2].x+tt[3].x)/4;
	int cy=(tt[0].y+tt[1].y+tt[2].y+tt[3].y)/4;
    for (int i = 0; i < tt.length; i++) {
		tt[i].x=cx+cy-act[i].y;
		tt[i].y=cy-cx+act[i].x;
	}
    for (int  i= 0;  i< tt.length; i++) {
		if (!maxYes(tt[i].x,tt[i].y)) {
			return ;
		}
	}
     for (int i = 0; i < tt.length; i++) {
		act[i].x=tt[i].x;
		act[i].y=tt[i].y;
	} 
     repaint();
}
public boolean minYes(int x,int y){
	for (int i = 0; i < 4; i++) {
		
		if (!maxYes(act[i].x+x,act[i].y+y)) {
			return false;
		}
	}
	return true;
}
public boolean maxYes(int x,int y){
	if (x<0||x>=10||y<0||y>=18) {
		return false;
	}
   if (map[x][y]==1) {
	return false;
    }
	
	return true;
}
@Override
public void keyTyped(KeyEvent e) {
	// TODO Auto-generated method stub
	
}
@Override
public void keyPressed(KeyEvent e) {
	if (start) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_DOWN:
			down();
			break;
		case KeyEvent.VK_UP:
			 up();
			break;
		case KeyEvent.VK_LEFT:
			Move(-1, 0);
			break;
		case KeyEvent.VK_RIGHT:
			Move(1, 0);
			break;

		default:
			break;
		}
	}
}
@Override
public void keyReleased(KeyEvent e) {
	// TODO Auto-generated method stub
	
}
private boolean newAct(){
	switch (temp) {
	case 0:
		act[0].x=3;act[0].y=0;
		act[1].x=4;act[1].y=0;
		act[2].x=2;act[2].y=1;
		act[3].x=3;act[3].y=1;		
		break;
	case 1:
		act[0].x=1;act[0].y=0;
		act[1].x=2;act[1].y=0;
		act[2].x=3;act[2].y=0;
		act[3].x=4;act[3].y=0;		
		break;
	case 2:
		act[0].x=2;act[0].y=0;
		act[1].x=3;act[1].y=0;
		act[2].x=2;act[2].y=1;
		act[3].x=3;act[3].y=1;		
		break;
	case 3:
		act[0].x=2;act[0].y=0;
		act[1].x=2;act[1].y=1;
		act[2].x=2;act[2].y=2;
		act[3].x=3;act[3].y=2;		
		break;
	case 4:
		act[0].x=3;act[0].y=0;
		act[1].x=3;act[1].y=1;
		act[2].x=3;act[2].y=2;
		act[3].x=2;act[3].y=2;		
		break;
	case 5:
		act[0].x=3;act[0].y=0;
		act[1].x=2;act[1].y=1;
		act[2].x=3;act[2].y=1;
		act[3].x=4;act[3].y=1;		
		break;
	case 6:
		act[0].x=2;act[0].y=0;
		act[1].x=3;act[1].y=0;
		act[2].x=3;act[2].y=1;
		act[3].x=4;act[3].y=1;		
		break;
	}
	for (int i = 0; i < 4; i++) {
		if (maxYes(act[i].x,act[i].y)) {
			return false;
		}
	}
	
	return true;
	
	
}
@Override
public void actionPerformed(ActionEvent e) {
	if (e.getSource()==newGame) {
		if (e.getActionCommand().equals("开始")) {
			newGame.setText("重置");
			requestFocus(true);
			start=true;
			temp=random.nextInt(7);
			if (!newAct()) {
				t=new Timer(1000-(100*speed),new myTimer() );
				t.start();
				temp=random.nextInt(7);
				nextAct();
				repaint();
			}else{
				//这里是游戏失败
				return;
			}
		}else{
			start=false;
			fenshu=0;
			tFenShu=0;
			speed=0;
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j <18; j++) {
					map[i][j]=0;
				}
			}
			newGame.setText("开始");
		}
		
	}
	if(e.getSource()==endGame){
		System.exit(0);
	}
	if (e.getSource()==button) {
		fenshu=0;
		tFenShu=0;
		speed=0;
		start=false;
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j <18; j++) {
				map[i][j]=0;
			}
		}
		newGame.setText("开始");
		dialog.setVisible(false);
		repaint();
		
	}
	
}
public class myTimer implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		if (start) {
			down();
		}
	}
	
}
}

package www.csdn.net.frame;

public class GameAct {
    int x;
    int y;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值