?? game.java
字號:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;
public class Game extends Frame{
private Choice speedControl;//游戲速度選擇控件
private DrawTable drawTable;//控制游戲桌顯示
private DrawNextShape drawNextShape;//顯示下一個游戲塊
private int delayTime;//塊下落的時間間隔,也就是游戲速度
private final int speedTime = 800;//
private Button startButton ;//開始按鈕
private boolean isKeyEnable = false;//標識鍵盤輸入是否可用
private Shape firstShape = randomShape();//產生第一個游戲塊
private Shape nextShape = randomShape();//產生下第二個游戲塊
private Timer timer;//計時器,讓游戲塊自動下落
private int score = 0;//分數
private Label scoreLabel;//顯示分數的標簽
private Label speedControlLabel;//提示速度選擇的標簽
private Label nextShapeLabel;//提示下一個游戲塊的標簽
private Label myInformation;//顯示游戲制作者信息
private Label noticeLabel;//提示標簽
private Label noticContentLabel;//提示內容1
private Label noticContentLabel2;//提示內容2
//對游戲進行初始化
public void inint(){
this.setTitle("俄羅斯方塊");
this.setLayout(null);
this.setLocation(300, 200);
this.setSize(500,500);
this.setBackground(new Color(500555));
this.addWindowListener(new WinClose());
startButton = new Button("開始");
startButton.setBounds(370, 400, 90, 30);
startButton.setBackground(new Color(5555555));
startButton.setFont(new Font("1",1,16));
startButton.addActionListener(new StartButtonListener());
startButton.setEnabled(true);
startButton.addKeyListener(new MyKeyListener());
this.add(startButton);
scoreLabel = new Label("分數: "+score);
scoreLabel.setBounds(370, 125, 90, 30);
scoreLabel.setFont(new Font("1",1,16));
scoreLabel.setBackground(new Color(55550255));
this.add(scoreLabel);
drawTable = new DrawTable();
drawTable.setLocation(40, 50);
drawTable.addKeyListener(new MyKeyListener());
this.add(drawTable);
drawNextShape = new DrawNextShape();
drawNextShape.setLocation(370, 200);
drawNextShape.addKeyListener(new MyKeyListener());
this.add(drawNextShape);
speedControlLabel = new Label("請選擇速度:");
speedControlLabel.setFont(new Font("1",1,16));
speedControlLabel.setBounds(370, 60, 100, 20);
this.add(speedControlLabel);
nextShapeLabel = new Label("下一個塊:");
nextShapeLabel.setFont(new Font("1",1,16));
nextShapeLabel.setBounds(370, 170, 100, 20);
this.add(nextShapeLabel);
noticeLabel = new Label("提示:");
noticeLabel.setFont(new Font("1",1,16));
noticeLabel.setBounds(370, 325, 100, 20);
this.add(noticeLabel);
noticContentLabel2 = new Label("請用方向鍵");
noticContentLabel2.setFont(new Font("1",1,16));
noticContentLabel2.setBounds(370, 345, 100, 20);
this.add(noticContentLabel2);
noticContentLabel = new Label("等待開始...");
noticContentLabel.setFont(new Font("1",1,16));
noticContentLabel.setBounds(370, 375, 100, 20);
this.add(noticContentLabel);
myInformation = new Label("版權所有:丁小偉 QQ:284983884 郵箱:dingxw92@yahoo.com.cn");
myInformation.setFont(new Font("1",1,16));
myInformation.setBounds(5, 460, 490, 30);
this.add(myInformation);
speedControl = new Choice();
speedControl.setBounds(370,90,90,30);
speedControl.setBackground(new Color(500500));
speedControl.setFont(new Font(" ",1,16));
speedControl.add("1");
speedControl.add("2");
speedControl.add("3");
speedControl.add("4");
speedControl.add("5");
speedControl.add("6");
speedControl.add("7");
speedControl.add("8");
speedControl.addItemListener(new SpeedControlListener());
speedControl.setEnabled(true);
this.add(speedControl);
delayTime = speedTime;
timer = new Timer(delayTime,new TimeListener());
this.addKeyListener(new MyKeyListener());
firstShape.begin();
nextShape.beforeBegin();
}
//產生隨機游戲塊
private Shape randomShape(){
Random randomNumCreater =new Random();
int randomNum = randomNumCreater.nextInt(7);
Shape shape = null;
switch(randomNum){
case 0:shape = new Shape0();break;
case 1:shape = new Shape1();break;
case 2:shape = new Shape2();break;
case 3:shape = new Shape3();break;
case 4:shape = new Shape4();break;
case 5:shape = new Shape5();break;
case 6:shape = new Shape6();break;
case 7:shape = new Shape7();break;
default:break;
}
return shape;
}
//游戲結束的一些處理
public void gameOver(){
isKeyEnable = false;
timer.stop();
drawTable.setEnabled(false);
speedControl.setEnabled(true);
noticContentLabel.setText("游戲結束");
startButton.setLabel("我還要玩");
}
//當游戲塊不能下落時,調用此方法處理
public void afterDownFalse(){
addScore(firstShape.addPoint());
scoreLabel.setText("分數: "+score);
firstShape = nextShape;
nextShape = randomShape();
nextShape.beforeBegin();
if(isKeyEnable == true){
isKeyEnable = false;
if(firstShape.begin() == false){
gameOver();
}
else
isKeyEnable = true;
}
else{
if(firstShape.begin() == false)
gameOver();
}
}
public void addScore(int rows){
if(rows!=0){
switch(delayTime*1000/speedTime){
case 1000 : score = (rows*2-1)*10 + score;break;
case 875 : score = (rows*2-1)*20 + score;break;
case 750 : score = (rows*2-1)*30 + score;break;
case 625 : score = (rows*2-1)*40 + score;break;
case 500 : score = (rows*2-1)*50 + score;break;
case 375 : score = (rows*2-1)*60 + score;break;
case 250 : score = (rows*2-1)*70 + score;break;
case 125 : score = (rows*2-1)*80 + score;break;
default : break;
}
}
}
//游戲重新開始時進行的一些處理
public void gameRestart() {
timer.stop();
Table.init();
firstShape = randomShape();
nextShape = randomShape();
drawNextShape.repaint();
nextShape.beforeBegin();
firstShape.begin();
drawTable.repaint();
isKeyEnable = true;
score = 0;
scoreLabel.setText("分數: "+new Integer(score).toString());
timer.start();
}
//時間監聽器
public class TimeListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
boolean b = firstShape.down();
if(b == false)
afterDownFalse();
drawTable.repaint();
drawNextShape.repaint();
}
}
//開始按鈕監聽器
public class StartButtonListener implements ActionListener{
if(startButton.getLabel().equals("開始")){
noticContentLabel.setText("游戲進行中...");
startButton.setLabel("暫停");
timer.start();
isKeyEnable = true;
speedControl.setEnabled(false);
}
else if(startButton.getLabel().equals("暫停")){
noticContentLabel.setText("游戲暫停...");
startButton.setLabel("繼續");
timer.stop();
isKeyEnable = false;
speedControl.setEnabled(false);
}
else if(startButton.getLabel().equals("繼續")){
noticContentLabel.setText("游戲進行中...");
startButton.setLabel("暫停");
timer.start();
isKeyEnable = true;
speedControl.setEnabled(false);
}
else if(startButton.getLabel().equals("我還要玩")){
noticContentLabel.setText("游戲進行中...");
startButton.setLabel("暫停");
speedControl.setEnabled(false);
gameRestart();
}
}
}
//鍵盤監聽器 ,利用isKeyEnable標識是否響應鍵盤事件
public class MyKeyListener implements KeyListener{
public void keyPressed(KeyEvent e) {
if(isKeyEnable){
switch( e.getKeyCode()){
case KeyEvent.VK_UP: firstShape.change();break;
case KeyEvent.VK_DOWN:
{
if(firstShape.down() == false)
afterDownFalse();
};break;
case KeyEvent.VK_LEFT: firstShape.left();break;
case KeyEvent.VK_RIGHT: firstShape.right();break;
}
drawTable.repaint();
drawNextShape.repaint();
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
//速度選擇監聽器
public class SpeedControlListener implements ItemListener{
public void itemStateChanged(ItemEvent e) {
if(e.getItem().equals("1")){
delayTime = speedTime;
timer.setDelay(delayTime);
}
else if(e.getItem().equals("2")){
delayTime = speedTime*7/8;
timer.setDelay(delayTime);
}
else if(e.getItem().equals("3")){
delayTime = speedTime*6/8;
timer.setDelay(delayTime);
}
else if(e.getItem().equals("4")){
delayTime = speedTime*5/8;
timer.setDelay(delayTime);
}
else if(e.getItem().equals("5")){
delayTime = speedTime*4/8;
timer.setDelay(delayTime);
}
else if(e.getItem().equals("6")){
delayTime = speedTime*3/8;
timer.setDelay(delayTime);
}
else if(e.getItem().equals("7")){
delayTime = speedTime*2/8;
timer.setDelay(delayTime);
}
else if(e.getItem().equals("8")){
delayTime = speedTime*1/8;
timer.setDelay(delayTime);
}
}
}
//關閉主窗口
public class WinClose extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
//主函數
public static void main(String args[]){
Game g = new Game();
g.inint();
g.setVisible(true);//這句必須要,要不看不見游戲桌!
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -