2013-10-09 651 views
1

我正在爲一個學校項目製作一個遊戲,並且在這個部分停留了幾天。我將如何去重新啓動下面的小程序?我試圖做一個restart()方法,但缺乏必要的技能。如何重新啓動Java小程序?

package main; 

import game.framework.Animation; 

import java.applet.Applet; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Frame; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.lang.Thread.State; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Random; 

import javax.swing.JButton; 
import javax.swing.JPanel; 


//add speedboost 
public class Main extends Applet implements Runnable, KeyListener{ 

    enum GameState { 
     Running, Dead 
    } 


    static GameState state = GameState.Running; 



private Background bg2, bg1; 
private boolean restart = false;  
private Player player; 
private SpeedBoost sb1; 
private Image image; 
private Image backsprite1, backsprite2; 
private Image player1, dad1, dad2, dad3; 
private Image player2; 
private Image player3; 
private Image back1; 
private Image back2, endscreen; 
private Image back3, coin, bigcoin; 
private Image back4, sbimg; 
public Image tilebase, platform; 
private Block block1, block2, block3, block4; 
private Coin coin1, coin2, coin3, coin4, coin5; 
private LargeCoin bc1; 
//use for the base... 
private Dad dad; 
private Graphics second; 
private Animation playwalk, dadwalk; 
private URL base; 
private Platform plat1, plat2, plat3, plat4, plat5, plat6, plat7; 
public static int score = 1, coinsint = 1; 
public static String score2, coins = "1"; 
public static int scorespeed = 1; 
public int hs1, hs2, hs3, hs4, hs5, totcoins; 
public String hs1s, hs2s, hs3s, hs4s, hs5s, totcoinss; 
public Ends ends; 
    @Override 
     public void init() { 
     setSize(800, 480); 
     setBackground(Color.WHITE); 
     setFocusable(true); 
     addKeyListener(this); 
     Frame frame = (Frame) this.getParent().getParent(); 
     frame.setTitle("Game v1.2.2"); 
     try { 
      base = getDocumentBase(); 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 

     endscreen = getImage(base, "data/endscreen.png"); 

     coin = getImage(base, "data/coin.png"); 
    bigcoin = getImage(base, "data/bigcoin.png");  

tilebase = getImage(base, "data/tilebase.png"); 
platform = getImage(base, "data/blocksmall.png"); 
     back1 = getImage(base, "data/back1.png"); 
     back2 = getImage(base, "data/back2.png"); 
     back3 = getImage(base, "data/back3.png"); 
     back4 = getImage(base, "data/back4.png");`enter code here` 

     sbimg = getImage(base, "data/speedboost.png"); 
     player1 = getImage(base, "data/player1.png"); 
     player2 = getImage(base, "data/player2.png"); 
     player3 = getImage(base, "data/player3.png"); 

     dad1 = getImage(base, "data/dad1.png"); 
     dad2 = getImage(base, "data/dad2.png"); 
     dad3 = getImage(base, "data/dad3.png"); 

     backsprite1 = back1; 
     backsprite2 = back2; 

     playwalk = new Animation(); 
     playwalk.addFrame(player1, 80); 
     playwalk.addFrame(player2, 80); 
     playwalk.addFrame(player3, 80); 

dadwalk = new Animation(); 
dadwalk.addFrame(dad1, 80); 
dadwalk.addFrame(dad2, 80); 
dadwalk.addFrame(dad3, 80); 
    } 
public void animate() { 
    playwalk.update(15); 
dadwalk.update(15); 


    } 








@Override 
     public void start() { 
    state = state.Running; 
      ends = new Ends(-3, 0); 
       //create objects at positions 

     coin1 = new Coin(-100, -10); 
     coin2 = new Coin(-10, -10); 
     coin3 = new Coin(-10, -10); 
     coin4 = new Coin(-10, -10); 
     coin5 = new Coin(-10, -10); 

      player = new Player(); 



      dad = new Dad(); 

      bg1 = new Background(1, 0); 
     bg2 = new Background(2099, 0); 

     plat1 = new Platform(0,0); 
     plat2 = new Platform(-100,0); 
     plat3 = new Platform(-200,0); 
     plat4 = new Platform(-300,0); 
     plat5 = new Platform(-400,0); 
     plat6 = new Platform(-500,0); 
     plat7 = new Platform(-600,0); 

     sb1 = new SpeedBoost(-100, 0); 
     bc1 = new LargeCoin(-99, 0); 

     block1 = new Block(0, 0); 
     block2 = new Block(0, 0); 
     block3 = new Block(0, 0); 
     block4 = new Block(0, 0); 



     Thread thread = new Thread(this); 
      thread.start(); 
     } 




      @Override 
       public void stop() { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void destroy() { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void run() { 
       if (state == GameState.Running) { 
       while (true) { 
        //selecting sprites for the background 

        //updates go here 
if(bg1.getBgX() < -2100){ 
    bg1.setBgX(2100); 
} 
if(bg2.getBgX() < -2100){ 
    bg2.setBgX(2100); 
} 

//backgrounds arent working 
if(bg1.getBgX() > 2000){ 
          Random rand = new Random(); 

          int nel = rand.nextInt(4) + 1; 
          if(nel == 1){ 
           backsprite1 = back1; 
          }if(nel == 2){ 
           backsprite1 = back2; 
          }if(nel == 3){ 
           backsprite1 = back3; 
          }if(nel == 4){ 
           backsprite1 = back4; 
          } 
        } 

       if(bg2.getBgX() > 2000){ 
          Random rand = new Random(); 

           int n1 = rand.nextInt(4) + 1; 
           if(n1 == 1){ 
            backsprite2 = back1; 
           }if(n1 == 2){ 
            backsprite2 = back2; 
           }if(n1 == 3){ 
            backsprite2 = back3; 
           }if(n1 == 4){ 
            backsprite2 = back4; 
           } 
       } 

       block4.update(); 
       block3.update(); 
       block2.update(); 
        block1.update(); 

        sb1.update(); 

        bc1.update(); 

        player.update(); 

        dad.update(); 

        bg1.update(); 
        bg2.update(); 

        coin1.update(); 
        coin2.update(); 
        coin3.update(); 
        coin4.update(); 
        coin5.update(); 


        plat1.update(); 
        plat2.update(); 
        plat3.update(); 
        plat4.update(); 
        plat5.update(); 
        plat6.update(); 
        plat7.update(); 

        if(Block.lives < 0){ 
         state = GameState.Dead; 

} 



        animate(); 
        repaint(); 
        try { 
         Thread.sleep(17); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 

      } 
      @Override 
      public void update(Graphics g) { 
       if (image == null) { 
        image = createImage(this.getWidth(), this.getHeight()); 
        second = image.getGraphics(); 
       } 


      //other updates go here 
       block1.update(); 
       block2.update(); 
       block3.update(); 
       block4.update(); 



       sb1.update(); 

       bc1.update(); 

       coin1.update(); 
       coin2.update(); 
       coin3.update(); 
       coin4.update(); 
       coin5.update(); 

       plat1.update(); 
       plat2.update(); 
       plat3.update(); 
       plat4.update(); 
       plat5.update(); 
       plat6.update(); 
       plat7.update(); 

       bg1.update(); 
       bg2.update(); 

       player.update(); 
       dad.update(); 


       second.setColor(getBackground()); 
       second.fillRect(0, 0, getWidth(), getHeight()); 
       second.setColor(getForeground()); 
       paint(second); 

       g.drawImage(image, 0, 0, this); 


      } 


      @Override 
      public void paint(Graphics g) { 
       if (state == GameState.Running) { 
       //main graphics identifyers here 
      // first are drawn below last 
       //need to make the platform lower down, see class for explanation 

        g.drawImage(backsprite1, bg1.getBgX(), bg1.getBgY(), this); 
        g.drawImage(backsprite2, bg2.getBgX(), bg2.getBgY(), this); 


       g.drawImage(playwalk.getImage(), player.getCenterX(), player.getCenterY(), this); 

       g.drawImage(platform, plat1.getCenterX(), plat1.getCenterY(), this); 
        g.drawImage(platform, plat2.getCenterX(), plat2.getCenterY(), this); 
        g.drawImage(platform, plat3.getCenterX(), plat3.getCenterY(), this); 
        g.drawImage(platform, plat4.getCenterX(), plat4.getCenterY(), this); 
        g.drawImage(platform, plat5.getCenterX(), plat5.getCenterY(), this); 
        g.drawImage(platform, plat6.getCenterX(), plat6.getCenterY(), this); 
        g.drawImage(platform, plat7.getCenterX(), plat7.getCenterY(), this); 

        g.drawImage(sbimg, sb1.getCenterX(), sb1.getCenterY(), this); 

        g.drawImage(bigcoin, bc1.centerX, bc1.centerY, this); 

        g.drawImage(dadwalk.getImage(), dad.centerX, dad.centerY, this); 

      g.drawImage(tilebase, block1.getCenterX(), block1.getCenterY(), this); 
      g.drawImage(tilebase, block2.getCenterX(), block2.getCenterY(), this); 
      g.drawImage(tilebase, block3.getCenterX(), block3.getCenterY(), this); 
      g.drawImage(tilebase, block4.getCenterX(), block4.getCenterY(), this); 

      g.drawImage(coin, coin1.getCenterX(), coin1.getCenterY(), this); 
      g.drawImage(coin, coin2.getCenterX(), coin2.getCenterY(), this); 
      g.drawImage(coin, coin3.getCenterX(), coin3.getCenterY(), this); 
      g.drawImage(coin, coin4.getCenterX(), coin4.getCenterY(), this); 
      g.drawImage(coin, coin5.getCenterX(), coin5.getCenterY(), this); 



      g.setColor(Color.MAGENTA); 
      g.drawString(score2, 700, 25); 
      Font font = new Font("Serif", Font.PLAIN, 36); 
      g.setFont(font); 

      g.drawString(coins, 20, 25); 


      }else if (state == GameState.Dead) { 
       totcoins += coinsint; 
       coinsint = 0; 
       hs1s = String.valueOf(hs1); 
       hs2s = String.valueOf(hs2); 
       hs3s = String.valueOf(hs3); 
       hs4s = String.valueOf(hs4); 
       hs5s = String.valueOf(hs5); 
       totcoinss = String.valueOf(totcoins); 




       if(score > hs1){ 
        hs1 = score; 
       }else if(score > hs2){ 
        hs2 = score; 
       }else if(score > hs3){ 
         hs3 = score; 
        }else if(score > hs4){ 
         hs4 = score; 
        }else if(score > hs5){ 
         hs5 = score; 
        } 
       scorespeed = 0; 
       //format this shit up in here 
       //when restart is init, then this will be tested 


       g.setColor(Color.BLACK); 
       g.fillRect(0, 0, 800, 480); 
      g.setColor(Color.WHITE); 
      g.drawString(hs1s, 230, 220); 
      g.drawString(hs2s, 230, 250); 
      g.drawString(hs3s, 230, 280); 
      g.drawString(hs4s, 230, 310); 
      g.drawString(hs5s, 230, 340); 
      g.drawString(totcoinss, 700, 100); 
      g.drawImage(endscreen, ends.cornX, ends.cornY, this);  

      } 

      } 
    //fix the android stuff 







@Override 
    public void keyPressed(KeyEvent e) { 

    switch (e.getKeyCode()) { 
     case KeyEvent.VK_UP: 

      player.jump(); 
      Player.jumps += 1; 
      break; 


     }    
    switch (e.getKeyCode()) { 
    case KeyEvent.VK_SPACE: 
if(state == state.Dead){ 

} 
    } 


} 

@Override 
    public void keyReleased(KeyEvent e) { 
     // TODO Auto-generated method stub 
     switch (e.getKeyCode()) { 
     case KeyEvent.VK_UP: 

      break; 
     } 
    } 

    @Override 
    public void keyTyped(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 
    public Background getBg1() { 
     return bg1; 
    } 
    public void setBg1(Background bg1) { 
     this.bg1 = bg1; 
    } 
    public Background getBg2() { 
     return bg2; 
    } 
    public void setBg2(Background bg2) { 
     this.bg2 = bg2; 
    } 
    public Player getPlayer() { 
     return player; 
    } 
    public void setPlayer(Player player) { 
     this.player = player; 
    } 
    public Image getImage() { 
     return image; 
    } 
    public void setImage(Image image) { 
     this.image = image; 
    } 
    public Image getBacksprite() { 
     return backsprite1; 
    } 
    public void setBacksprite(Image backsprite) { 
     this.backsprite1 = backsprite; 
    } 
    public Image getBacksprite1() { 
     return backsprite1; 
    } 
    public void setBacksprite1(Image backsprite1) { 
     this.backsprite1 = backsprite1; 
    } 
    public Image getBacksprite2() { 
     return backsprite2; 
    } 
    public void setBacksprite2(Image backsprite2) { 
     this.backsprite2 = backsprite2; 
    } 
    public Image getPlayer1() { 
     return player1; 
    } 
    public void setPlayer1(Image player1) { 
     this.player1 = player1; 
    } 
    public Image getPlayer2() { 
     return player2; 
    } 
    public void setPlayer2(Image player2) { 
     this.player2 = player2; 
    } 
    public Image getPlayer3() { 
     return player3; 
    } 
    public void setPlayer3(Image player3) { 
     this.player3 = player3; 
    } 
    public Image getBack1() { 
     return back1; 
    } 
    public void setBack1(Image back1) { 
     this.back1 = back1; 
    } 
    public Image getBack2() { 
     return back2; 
    } 
    public void setBack2(Image back2) { 
     this.back2 = back2; 
    } 
    public Image getBack3() { 
     return back3; 
    } 
    public void setBack3(Image back3) { 
     this.back3 = back3; 
    } 
    public Image getBack4() { 
     return back4; 
    } 
    public void setBack4(Image back4) { 
     this.back4 = back4; 
    } 
    public Graphics getSecond() { 
     return second; 
    } 
    public void setSecond(Graphics second) { 
     this.second = second; 
    } 
    public Animation getPlaywalk() { 
     return playwalk; 
    } 
    public void setPlaywalk(Animation playwalk) { 
     this.playwalk = playwalk; 
    } 
    public URL getBase() { 
     return base; 
    } 
    public void setBase(URL base) { 
     this.base = base; 
    } 
    public static Image getTilebase() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public GameState getState() { 
     return state; 
    } 

    public void setState(GameState state) { 
     this.state = state; 
    } 

    public SpeedBoost getSb1() { 
     return sb1; 
    } 

    public void setSb1(SpeedBoost sb1) { 
     this.sb1 = sb1; 
    } 

    public Image getCoin() { 
     return coin; 
    } 

    public void setCoin(Image coin) { 
     this.coin = coin; 
    } 

    public Image getSbimg() { 
     return sbimg; 
    } 

    public void setSbimg(Image sbimg) { 
     this.sbimg = sbimg; 
    } 

    public Image getPlatform() { 
     return platform; 
    } 

    public void setPlatform(Image platform) { 
     this.platform = platform; 
    } 

    public Block getBlock1() { 
     return block1; 
    } 

    public void setBlock1(Block block1) { 
     this.block1 = block1; 
    } 

    public Block getBlock2() { 
     return block2; 
    } 

    public void setBlock2(Block block2) { 
     this.block2 = block2; 
    } 

    public Block getBlock3() { 
     return block3; 
    } 

    public void setBlock3(Block block3) { 
     this.block3 = block3; 
    } 

    public Block getBlock4() { 
     return block4; 
    } 

    public void setBlock4(Block block4) { 
     this.block4 = block4; 
    } 

    public Coin getCoin1() { 
     return coin1; 
    } 

    public void setCoin1(Coin coin1) { 
     this.coin1 = coin1; 
    } 

    public Coin getCoin2() { 
     return coin2; 
    } 

    public void setCoin2(Coin coin2) { 
     this.coin2 = coin2; 
    } 

    public Coin getCoin3() { 
     return coin3; 
    } 

    public void setCoin3(Coin coin3) { 
     this.coin3 = coin3; 
    } 

    public Coin getCoin4() { 
     return coin4; 
    } 

    public void setCoin4(Coin coin4) { 
     this.coin4 = coin4; 
    } 

    public Coin getCoin5() { 
     return coin5; 
    } 

    public void setCoin5(Coin coin5) { 
     this.coin5 = coin5; 
    } 

    public Platform getPlat1() { 
     return plat1; 
    } 

    public void setPlat1(Platform plat1) { 
     this.plat1 = plat1; 
    } 

    public Platform getPlat2() { 
     return plat2; 
    } 

    public void setPlat2(Platform plat2) { 
     this.plat2 = plat2; 
    } 

    public Platform getPlat3() { 
     return plat3; 
    } 

    public void setPlat3(Platform plat3) { 
     this.plat3 = plat3; 
    } 

    public Platform getPlat4() { 
     return plat4; 
    } 

    public void setPlat4(Platform plat4) { 
     this.plat4 = plat4; 
    } 

    public Platform getPlat5() { 
     return plat5; 
    } 

    public void setPlat5(Platform plat5) { 
     this.plat5 = plat5; 
    } 

    public Platform getPlat6() { 
     return plat6; 
    } 

    public void setPlat6(Platform plat6) { 
     this.plat6 = plat6; 
    } 

    public Platform getPlat7() { 
     return plat7; 
    } 

    public void setPlat7(Platform plat7) { 
     this.plat7 = plat7; 
    } 

    public static int getScore() { 
     return score; 
    } 

    public static void setScore(int score) { 
     Main.score = score; 
    } 

    public static int getCoinsint() { 
     return coinsint; 
    } 

    public static void setCoinsint(int coinsint) { 
     Main.coinsint = coinsint; 
    } 

    public static String getScore2() { 
     return score2; 
    } 

    public static void setScore2(String score2) { 
     Main.score2 = score2; 
    } 

    public static String getCoins() { 
     return coins; 
    } 

    public static void setCoins(String coins) { 
     Main.coins = coins; 
    } 

    public static int getScorespeed() { 
     return scorespeed; 
    } 

    public static void setScorespeed(int scorespeed) { 
     Main.scorespeed = scorespeed; 
    } 

    public void setTilebase(Image tilebase) { 
     this.tilebase = tilebase; 
    } 
} 
+1

*「製作學校項目」* 1)爲什麼選擇AWT而不是Swing?參見[Swing extras over AWT](http://stackoverflow.com/a/6255978/418556)有很多很好的理由放棄使用AWT組件。如果您需要支持較老的基於AWT的API,請參閱[混合重量級和輕量級組件](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html)。 2)爲什麼編寫一個小程序?如果這是由於規格。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 –

+0

TL/DR;您需要弄清楚如何將程序狀態重置爲開始。考慮首先嚐試通過使用數組或集合來簡化代碼,因爲存在大量不必要的冗餘,這會讓您或我們更難調試。此外,您還需要改進代碼格式,因爲這樣做會使我們和其他人更容易閱讀和理解。 –

+1

1)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 (沒有第三方API,使用合成圖像,*短*)。 2)'setSize(800,480);'不要在applet中。永遠。 3)*「(對不起,我不知道html)」*要完成這個作業,您需要熟悉它,至少有一點。 HTML設置小程序的大小並定義可以找到類的位置。 –

回答

3

一個很好的辦法可能是使用這樣的方法:

  • init(應該叫一次,並通過瀏覽器只)。設置任何常用的GUI元素並調用startNewGame()方法。
  • start(當用戶恢復瀏覽器時調用)。取消暫停動作(在stop中暫停)並繼續遊戲。
  • stop暫停當前的遊戲。
  • startNewGame()設置初始生命,件/字符佈局,比分等

當用戶想要開始新遊戲,也叫startNewGame()

+2

我想買一美元... + 1 – MadProgrammer

+0

@MadProgrammer嘿,呃..愛這個表達,它背後的概念,以及創造它的電影。 :) –

2

我知道這是一個老問題。

但上面的小程序的格式似乎是問題。我會使用一些基本的東西來控制applet。

「即用,而(真)是從來沒有在我看來一個不錯的選擇」

public class TestAppletCycle extends JApplet implements Runnable { 

    private Thread thread; 
    private boolean running; 

    @Override 
    public void init() { 
     // Init Game 
    } 

    @Override 
    public void start() { 
     if(!running) { 
      running = true; 
      thread = new Thread(this); 
      thread.start(); 
     } 
    } 

    @Override 
    public void run() { 
     while(running) { 
      // Game Loop 
     } 
    } 

    @Override 
    public void stop() { 
     logger.log(Level.INFO, "TestAppletCycle.stop();"); 
     if(running) { 
      running = false; 
      try { 
       thread.join(); 
      } catch(Exception e) { 
      } 
     } 
    } 

    @Override 
    public void destroy() { 
     // Clean Up Game 
    } 
} 

現在重新啓動您只需要停止和啓動applet的小程序。

相關問題