singlegameface.java
来自「软件工程实践课程的答案哦」· Java 代码 · 共 1,209 行 · 第 1/3 页
JAVA
1,209 行
themesMenuGroup.add(mi);
mi.addActionListener(new ChangeThemeAction(this, theme));
return mi;
}
/**Coloring the specified bar on the main playing field
* @param bar the specific bar to be colored
*/
public synchronized void set(SingleBar bar)
{
for(int i=0;i<3;i++)
{
lovList.add(new SingleLOV(bar.squares[i].x,bar.squares[i].y,bar.squares[i].color));
}
canvas.repaint();
}
/**Coloring the specified location on the main playing field
* @param location the location to be colored
*/
public synchronized void set(SingleLocation location)
{
lovList.add(new SingleLOV(location.x,location.y,location.color));
canvas.repaint();
}
/**init the game for restarting
* */
public synchronized void initGame()
{
if(this.gameOver)
{
SingleGamePool.init();
score = 0;
this.setScore(0);
this.setMessage(" WELCOME ");
lovList.clear();
canvas.repaint();
}
}
/**Clearing the specified bar on the main playing field
* @param bar the bar to be cleared
*/
public synchronized void clear(SingleBar bar)
{
for(int i=0;i<3;i++)
{
lovList.del(new SingleLOV(bar.squares[i].x,bar.squares[i].y,bar.squares[i].color));
}
canvas.repaint();
}
/**Clearing the specified location on the main playing field
* @param location the location to be cleared
*/
public synchronized void clear(SingleLocation location)
{
lovList.del(new SingleLOV(location.x,location.y,location.color));
canvas.repaint();
}
/**Set the speed of the bar
* @param speed the speed
*/
public void setSpeed(int speed)
{
this.tSpeed.setText(speed+"");
}
/**set the score of the player
* @param score the score player gets
*/
public void setScore(int score)
{
this.score=score*(speed+1);
this.tScore.setText(this.score+"");
}
public void setMessage(String s)
{
this.message.setText(s);
}
/**
* Things to do when game is over*/
public void gameOver()
{
if(SingleGamePool.isFull())
{
//canvas.setStart(false);
this.bar = null;
this.gameOver = true;
this.gameStart = false;
canvas.repaint();
// this.pNextBar.repaint();
this.timer = null;
this.numCreator = null;
this.setMessage("Don't lose heart");
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Start"||e.getActionCommand().equals("StartGame"))
{
if(!gameStart)
{
begin.begin.stop();
ReadyGo readyGo = new ReadyGo();
readyGo.init();
readyGo.start();
back1=new Back2();
back1.init();
back1.start();
this.lovList = new SingleLOVList();
this.numCreator = new SingleNumberCreator();
this.numCreator.numCreatorStart();
this.initGame();//gameface, gamepool,lovList all init
timer = new SingleTimer(this);
this.gameStart = true;
this.gameOver = false;
timer.start();
canvas.requestFocus();
}
else
{
canvas.requestFocus();
}
}
else if(e.getActionCommand()=="Pause")
{
if(this.isPaused==0&&this.gameStart)
{
if(speed!=0)
this.timer.suspend();
this.isPaused = 1;
this.canvas.repaint();
}
else if(this.isPaused==1&&this.gameStart)
{
if(speed!=0)
this.timer.resume();
this.isPaused = 0;
this.canvas.repaint();
}
}
else if(e.getActionCommand()=="Exit")
{
System.exit(0);
}
}
/**Create a bar according to a ColorSet object
* @param colors the colored bar to be created
* @param next the next bar to be created
*/
public synchronized void createBar()
{
SingleColorSet colors = numCreator.getColorSet();
pNextBar.repaint();
// System.out.println("NEXT:"+ this.numCreator.getNext().getColor0()+","+this.numCreator.getNext().getColor1()+","+this.numCreator.getNext().getColor2());
SingleLocation[] locations = new SingleLocation[3];
locations[0] = new SingleLocation(0,0,colors.getColor0());
locations[1] = new SingleLocation(1,0,colors.getColor1());
locations[2] = new SingleLocation(2,0,colors.getColor2());
bar = new SingleBar(locations);
canvas.fallTag =0;
canvas.fallTag2 = 0;
this.set(bar);
canvas.repaint();
}
/*public static void main(String[] args)
{
SingleGameFace face = new SingleGameFace();
begin=new Begin();
begin.init();
begin.start();
SingleGamePool gamePool = new SingleGamePool(face);
}*/
public void run()
{
begin=new Begin();
begin.init();
begin.start();
}
private void updateThisInterface() {
SwingUtilities.updateComponentTreeUI(this);
}
class StyleListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String str=e.getActionCommand();
String style="";
if(str.equals("Metal")) {style="javax.swing.plaf.metal.MetalLookAndFeel";
try{
UIManager.setLookAndFeel(style);
SwingUtilities.updateComponentTreeUI(SingleGameFace.this);
}catch(Exception ex){
ex.printStackTrace();
System.out.println(ex.toString());
}
}
if(str.equals("Windows")){
style="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try{
UIManager.setLookAndFeel(style);
SwingUtilities.updateComponentTreeUI(SingleGameFace.this);
}catch(Exception ex){
ex.printStackTrace();
System.out.println(ex.toString());
}
}
if(str.equals("CED/Motif")) {
try{
style="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
UIManager.setLookAndFeel(style);
SwingUtilities.updateComponentTreeUI(SingleGameFace.this);
}catch(Exception ex){
ex.printStackTrace();
System.out.println(ex.toString());
}
}
}
}
/**
* Sets the current L&F on each demo module
*/
public void updateLookAndFeel() {
try {
UIManager.setLookAndFeel(currentLookAndFeel);
this.updateThisInterface();
} catch (Exception ex) {
System.out.println("Failed loading L&F: " + currentLookAndFeel);
System.out.println(ex);
}
}
// Turns on all possible auditory feedback
class OnAudioAction extends AbstractAction {
SingleGameFace mainInterface;
protected OnAudioAction(SingleGameFace mainInterface) {
super("Audio On");
this.mainInterface = mainInterface;
}
public void actionPerformed(ActionEvent e) {
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.allAuditoryCues"));
mainInterface.updateLookAndFeel();
}
}
// Turns on the default amount of auditory feedback
class DefaultAudioAction extends AbstractAction {
SingleGameFace mainInterface;
protected DefaultAudioAction(SingleGameFace mainInterface) {
super("Audio Default");
this.mainInterface = mainInterface;
}
public void actionPerformed(ActionEvent e) {
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.defaultCueList"));
mainInterface.updateLookAndFeel();
}
}
// Turns off all possible auditory feedback
class OffAudioAction extends AbstractAction {
SingleGameFace mainInterface;
protected OffAudioAction(SingleGameFace mainInterface) {
super("Audio Off");
this.mainInterface = mainInterface;
}
public void actionPerformed(ActionEvent e) {
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.noAuditoryCues"));
mainInterface.updateLookAndFeel();
}
}
class ChangeThemeAction extends AbstractAction {
SingleGameFace mainInterface;
MetalTheme theme;
protected ChangeThemeAction(SingleGameFace mainInterface, MetalTheme theme) {
super("ChangeTheme");
this.mainInterface = mainInterface;
this.theme = theme;
}
public void actionPerformed(ActionEvent e) {
MetalLookAndFeel.setCurrentTheme(theme);
mainInterface.updateLookAndFeel();
}
}
}
/**The thread controling the dropping process of the bar
*
*
*/
class SingleTimer extends Thread
{
SingleGameFace gameFace;
SingleTimer(SingleGameFace gameFace)
{
super();
this.gameFace = gameFace;
}
public void run()
{
while(!SingleGamePool.isFull())
{
if(gameFace.gameStart)
{//the bar can't all inside the ground,game is over
while(!SingleGamePool.isFull())
{
gameFace.createBar();
try
{
if(gameFace.speed==0)
this.suspend();
}catch(Exception e){}
// bar drop until it reaches the bottom
while(!gameFace.bar.checkBottom())
{
gameFace.bar.dropOneStep();
gameFace.canvas.repaint();
try
{
if(gameFace.speed==0)
{
this.sleep(10);
}
else
{
this.sleep((4-gameFace.speed)*100);
}
}catch(InterruptedException e){}
}
gameFace.canvas.requestFocus();
for(int i=0; i<3; i++)
SingleGamePool.elimination.enQueue(gameFace.bar.squares[i]);
// try
// {Thread.sleep(1);
// }
// catch(InterruptedException e){}
//
while(!SingleGamePool.eliminate())
try
{
Thread.sleep(200);
}
catch(InterruptedException e){}
gameFace.setMessage("");
}
// gameFace.gameOver();
}
}
SingleGameFace.back1.back2.stop();
Lost lost=new Lost();
lost.init();
lost.start();
lost=null;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?