?? gameframe.java
字號:
package SnakeGame;
/**
*整個游戲的邏輯控制。。。。
*/
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.awt.Toolkit;
import java.awt.color.*;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.Properties;
public class GameFrame
extends JFrame {
private Toolkit tempKit;
//private int horizontalGrid = 60;
public javax.swing.Timer snakeTimer;
//private int verticalGrid = 40;
private int direction = Global.RIGHT; //貪食蛇初始時的方向為向右
private int horizontalGrid,verticalGrid;
private int singleWidthX ;//= this.getWidth()/horizontalGrid;
private int singleHeightY ;//= this.getHeight()/verticalGrid;
private int cooPos ;//= 5; //畫貪食蛇時,單個小圓點的arc大小
//private boolean flag = true;
private int Level = 0; //待擴展功能部分,設置關卡,初始時游戲為第一關,分數達到一定值時進入下一關。
private HashMap delay = new HashMap(); //待擴展功能部分, 設置每一關對應的游戲中貪食蛇移動的速度。
//此處為每一關固定為多少的速度。。
//其實也可以在游戲中 設置加速度這個概念。。。如一直按向右鍵可以向右加速。
//在此程序沒有實現這個功能。。。有時間了考慮擴展一下該功能。
{
delay.put(0,75);
delay.put(1,70);
delay.put(2,65);
delay.put(3,60);
delay.put(4,55);
delay.put(5,10);
}
public static ControlMenu configMenu;
public boolean paused = false; //標記游戲“暫停”或“進行”狀態的boolean值。
private int score; //游戲中 得分記錄變量
private JLabel scoreLabel; //游戲中 得分記錄牌
{
scoreLabel = new JLabel();
}
Snake mainSnake = new Snake(); //游戲中的 貪食蛇
private LinkedList eatedBean; //游戲中 被吃過的小Bean,并且此時該Bean還在蛇身中,
//單獨將其記錄下來 是為了游戲的過程中,當貪食蛇吃到Bean
//并且蛇身還沒有穿過該Bean時,用不同于貪食蛇自身的顏色顯示出來。
//數據結構采用LinkedList來實現。
// 可以便于遍歷。
{
eatedBean = new LinkedList();
}
/* // 待擴展部分,增加障礙物。
private LinkedList wallBean;
{
wallBean = new LinkedList();
}*/
private Point bean,eatBean; //生成的食物
{
bean = new Point();
}
private Iterator iterator ;
public GameFrame() {
this.paused = false;
this.tempKit = this.getToolkit();
System.out.println(tempKit.getScreenSize().getWidth());
System.out.println(tempKit.getScreenSize().getHeight());
this.setSize(tempKit.getScreenSize());
this.setGrid(Global.GameScreenWidthGrid,Global.GameScreenHeightGrid,Global.SnakePieceCooPos);//(85,63,5);
this.getContentPane().setBackground(Global.COLOR_BACK);
this.setUndecorated(true);
this.setResizable(false);
this.addKeyListener( new KeyHandler());
GameFrame.this.snakeTimer = new javax.swing.Timer( 80, new TimerHandler() );
this.getContentPane().add(scoreLabel,BorderLayout.SOUTH);
this.configMenu = new ControlMenu(this);
this.setVisible(true);
}
public void setGrid(int temp1,int temp2,int temp3)
{
this.horizontalGrid = temp1;
this.verticalGrid = temp2;
this.singleWidthX = this.getWidth()/temp1;
this.singleHeightY = this.getHeight()/temp2;
this.cooPos = temp3;
}
public class KeyHandler extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ESCAPE) //如果是ESC鍵時,隱藏的菜單欄出現,
// Timer停止計時
{
snakeTimer.stop();
if(paused)
{
configMenu.menuItem[2].setEnabled(false); //如果此時不是暫停狀態,則菜單欄中“暫停”項可用
configMenu.menuItem[3].setEnabled(true); //而菜單欄中的“開始”項不可用
}
else
{
configMenu.menuItem[3].setEnabled(false); //如果此時不是進行狀態,則菜單欄中“開始”項可用
configMenu.menuItem[2].setEnabled(true); //而菜單欄中的“暫停”項不可用
}
configMenu.setVisible(true); //隱藏的菜單應在相關狀態設置完后,才出現。
/*
if (!paused){
snakeTimer.stop();
configMenu.setVisible(true);
//GameFrame.this.changePausedState();
}
else {
snakeTimer.start();
configMenu.setVisible(false);
//GameFrame.this.changePausedState();
}*/
}
/** 上,下,左,右移動。。 **/
else if(e.getKeyCode() == KeyEvent.VK_LEFT && mainSnake.snakeDirection != Global.RIGHT)
{
direction = Global.LEFT;
}
else if (e.getKeyCode() == KeyEvent.VK_UP && mainSnake.snakeDirection != Global.DOWN)
{
direction = Global.UP;
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT && mainSnake.snakeDirection != Global.LEFT)
{
direction = Global.RIGHT;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN && mainSnake.snakeDirection != Global.UP)
{
direction = Global.DOWN;
}
else if(e.getKeyCode() == KeyEvent.VK_SPACE ) //空格鍵, 只有暫停與繼續游戲的作用。
{
if(!paused) //暫停游戲時,要將菜單欄與之相關的狀態改變。
{
snakeTimer.stop();
GameFrame.this.changePausedState();
configMenu.menuItem[2].setEnabled(false);
configMenu.menuItem[3].setEnabled(true);
}else //繼續游戲時,要將菜單欄與之相關的狀態改變。
{
snakeTimer.start();
GameFrame.this.changePausedState();
configMenu.menuItem[3].setEnabled(false);
configMenu.menuItem[2].setEnabled(true);
}
//System.exit(1);
}
}
}
public class TimerHandler implements ActionListener // 監聽器
{
public void actionPerformed(ActionEvent e)
{
Point temp = (Point) mainSnake.getLast();
iterator = mainSnake.iterator();
while(iterator.hasNext()) //計算蛇是否自身相遇算法
{ //先取出蛇的頭部,
//再取蛇的iterator。然后循環一個一個
//與頭部比較,真到取到頭部時,如果有重合的。
//則有判斷蛇與自身相遇了。。
// 接下來提示游戲結束。。
Point tempPoint = (Point)iterator.next();
if(temp.equals(tempPoint) && iterator.hasNext()!=false)
{
snakeTimer.stop();
stopGame();
JOptionPane.showMessageDialog(null,"Your score is"+score+"\nYou Lost");
GameFrame.this.resultRecord(score); // 積分榜 游戲中 成績的記錄 (見該方法詳細說明)
System.exit(1);
}
}
//System.out.println(temp.x+"--"+temp.y); 游戲中上,下,左,右邊界的判定
if( (temp.x==0 && direction ==4) || // 左邊界
(temp.y==0 && direction ==1) || // 上邊界
(temp.x==horizontalGrid-1 && direction ==2)|| //右邊界
(temp.y==verticalGrid-1 && direction ==3)) //下邊界
{
snakeTimer.stop();
stopGame();
JOptionPane.showMessageDialog(null,"Your score is"+score+"\nYou Lost");
/*積分榜*/
GameFrame.this.resultRecord(score);
System.exit(1);
}
if(direction != mainSnake.snakeReDirection) //當此時方向與原來蛇移動的反方向不同時,
//移動蛇。。
{
moveSnake(direction);
}
//drawBackGround(getGraphics());
mainSnake.drawSnake(getGraphics(),singleWidthX,singleHeightY,cooPos); //在屏幕上畫出蛇
//drawWall(getGraphics(),Level);
//drawWall(getGraphics());
drawBeanAndEBean(getGraphics()); //畫出生成的Bean(食物)和吃過的Bean
}
}
public void resultRecord(int score) // 積分榜。。。只實現的記錄第一名的成績。。
// 如果當前玩家所得的分數比文件中記錄的以前所有玩家中
// 最高得分還高的話,提示當前玩家輸入姓名。。
// 并同時記錄下玩家的姓名與分數。寫入一個文件中。。。。持久保存
{
Properties pp = new Properties();
try
{
pp.load(new FileInputStream("Miracle.ini"));
Enumeration enumt= pp.propertyNames();
if(!enumt.hasMoreElements())
{
String inputName=JOptionPane.showInputDialog("Please Input your Name");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -