2016-06-21 15 views
-1

Wie füge ich ein Icon zu diesem Snake Game hinzu und wo stelle ich es hin, auch wie erhöhe ich die Geschwindigkeit des Spiels nach so vielen Punkten? Der folgende Code ist die Klasse, in die ich glaube, dass diese zwei Teile des Codes gehen sollten.Wie man ein Icon zu JFrame hinzufügt

import java.awt.BorderLayout; 
import java.awt.Point; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.io.IOException; 
import java.util.LinkedList; 
import java.util.Random; 

import javax.imageio.ImageIO; 
import javax.swing.JFrame; 


public class SnakeGame extends JFrame { 


    private static final long FRAME_TIME = 1000L/50L; 

    private static final int MIN_SNAKE_LENGTH = 5; 

    private static final int MAX_DIRECTIONS = 3; 

    private BoardPanel board; 

    private SidePanel side; 

    private Random random; 

    private Clock logicTimer; 

    private boolean isNewGame; 

    private boolean isGameOver; 

    private boolean isPaused; 

    private LinkedList<Point> snake; 

    private LinkedList<Direction> directions; 

    private int score; 

    private int foodsEaten; 

    private int nextFoodScore; 

    private SnakeGame() { 
     super("Snake"); 
     setLayout(new BorderLayout()); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setResizable(false); 

     this.board = new BoardPanel(this); 
     this.side = new SidePanel(this); 

     add(board, BorderLayout.CENTER); 
     add(side, BorderLayout.EAST); 

     addKeyListener(new KeyAdapter() { 

      @Override 
      public void keyPressed(KeyEvent e) { 
       switch(e.getKeyCode()) { 

       case KeyEvent.VK_W: 
       case KeyEvent.VK_UP: 
        if(!isPaused && !isGameOver) { 
         if(directions.size() < MAX_DIRECTIONS) { 
          Direction last = directions.peekLast(); 
          if(last != Direction.South && last != Direction.North) { 
           directions.addLast(Direction.North); 
          } 
         } 
        } 
        break; 


       case KeyEvent.VK_S: 
       case KeyEvent.VK_DOWN: 
        if(!isPaused && !isGameOver) { 
         if(directions.size() < MAX_DIRECTIONS) { 
          Direction last = directions.peekLast(); 
          if(last != Direction.North && last != Direction.South) { 
           directions.addLast(Direction.South); 
          } 
         } 
        } 
        break; 

       case KeyEvent.VK_A: 
       case KeyEvent.VK_LEFT: 
        if(!isPaused && !isGameOver) { 
         if(directions.size() < MAX_DIRECTIONS) { 
          Direction last = directions.peekLast(); 
          if(last != Direction.East && last != Direction.West) { 
           directions.addLast(Direction.West); 
          } 
         } 
        } 
        break; 

       case KeyEvent.VK_D: 
       case KeyEvent.VK_RIGHT: 
        if(!isPaused && !isGameOver) { 
         if(directions.size() < MAX_DIRECTIONS) { 
          Direction last = directions.peekLast(); 
          if(last != Direction.West && last != Direction.East) { 
           directions.addLast(Direction.East); 
          } 
         } 
        } 
        break; 

       case KeyEvent.VK_P: 
        if(!isGameOver) { 
         isPaused = !isPaused; 
         logicTimer.setPaused(isPaused); 
        } 
        break; 

       case KeyEvent.VK_ENTER: 
        if(isNewGame || isGameOver) { 
         resetGame(); 
        } 
        break; 
       } 
      } 

     }); 


     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 


    private void startGame() { 

     this.random = new Random(); 
     this.snake = new LinkedList<>(); 
     this.directions = new LinkedList<>(); 
     this.logicTimer = new Clock(10.0f); 
     ////////////////////////////////////////////////////////////////////////////////////////////////// 
     this.isNewGame = true; 


     logicTimer.setPaused(true); 

     while(true) { 
      long start = System.nanoTime(); 

      logicTimer.update(); 

      if(logicTimer.hasElapsedCycle()) { 
       updateGame(); 
      } 

      board.repaint(); 
      side.repaint(); 

      long delta = (System.nanoTime() - start)/1000000L; 
      if(delta < FRAME_TIME) { 
       try { 
        Thread.sleep(FRAME_TIME - delta); 
       } catch(Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 


    private void updateGame() { 

     TileType collision = updateSnake(); 

     if(collision == TileType.Food) { 
      foodsEaten++; 
      score += nextFoodScore; 
      spawnFood(); 
     } else if(collision == TileType.SnakeBody) { 
      isGameOver = true; 
      logicTimer.setPaused(true); 
     } else if(nextFoodScore > 10) { 
     } 
    } 


    private TileType updateSnake() { 


     Direction direction = directions.peekFirst();    

     Point head = new Point(snake.peekFirst()); 
     switch(direction) { 
     case North: 
      head.y--; 
      break; 

     case South: 
      head.y++; 
      break; 

     case West: 
      head.x--; 
      break; 

     case East: 
      head.x++; 
      break; 
     } 


     if(head.x < 0 || head.x >= BoardPanel.COL_COUNT || head.y < 0 || head.y >= BoardPanel.ROW_COUNT) { 
      return TileType.SnakeBody; 
     } 


     TileType old = board.getTile(head.x, head.y); 
     if(old != TileType.Food && snake.size() > MIN_SNAKE_LENGTH) { 
      Point tail = snake.removeLast(); 
      board.setTile(tail, null); 
      old = board.getTile(head.x, head.y); 
     } 


     if(old != TileType.SnakeBody) { 
      board.setTile(snake.peekFirst(), TileType.SnakeBody); 
      snake.push(head); 
      board.setTile(head, TileType.SnakeHead); 
      if(directions.size() > 1) { 
       directions.poll(); 
      } 
     } 

     return old; 
    } 


    private void resetGame() { 

     this.score = 0; 
     this.foodsEaten = 0; 


     this.isNewGame = false; 
     this.isGameOver = false; 

     Point head = new Point(BoardPanel.COL_COUNT/2, BoardPanel.ROW_COUNT/2); 


     snake.clear(); 
     snake.add(head); 

     board.clearBoard(); 
     board.setTile(head, TileType.SnakeHead); 

     directions.clear(); 
     directions.add(Direction.North); 

     logicTimer.reset(); 

     spawnFood(); 
    } 


    public boolean isNewGame() { 
     return isNewGame; 
    } 


    public boolean isGameOver() { 
     return isGameOver; 
    } 


    public boolean isPaused() { 
     return isPaused; 
    } 


    private void spawnFood() { 
     this.nextFoodScore = 10; 

     int index = random.nextInt(BoardPanel.COL_COUNT * BoardPanel.ROW_COUNT - snake.size()); 

     int freeFound = -1; 
     for(int x = 0; x < BoardPanel.COL_COUNT; x++) { 
      for(int y = 0; y < BoardPanel.ROW_COUNT; y++) { 
       TileType type = board.getTile(x, y); 
       if(type == null || type == TileType.Food) { 
        if(++freeFound == index) { 
         board.setTile(x, y, TileType.Food); 
         break; 
        } 
       } 
      } 
     } 
    } 


    public int getScore() { 
     return score; 
    } 


    public int getFoodsEaten() { 
     return foodsEaten; 
    } 


    public int getNextFoodScore() { 
     return nextFoodScore; 
    } 


    public Direction getDirection() { 
     return directions.peek(); 
    } 

    public static void main(String[] args) { 
     SnakeGame snake = new SnakeGame(); 
     snake.startGame(); 
    } 

} 
+0

* "Wie füge ich diesem Snake-Spiel ein Icon hinzu und wo gebe ich es hin ..." * Was haben Sie beim Betrachten der Java-Docs für 'JFrame' gefunden? * ".. auch, wie kann ich die Geschwindigkeit erhöhen .." * SO ist kein Helpdesk, sondern eine Q & A-Seite, wo jeder Thread eine einzige Frage mit mindestens einer klaren Antwort haben sollte. Wenn Sie zwei Fragen haben, starten Sie zwei Fragethreads. –

+1

[So ändern Sie das JFrame-Symbol] (http://stackoverflow.com/q/1614772/4290096) –

Antwort

0

Erstellen Sie ein neues ImageIcon Objekt wie folgt:

ImageIcon img = new ImageIcon(pathToFileOnDisk); 

dann mit setIconImage(), um Ihre JFrame stellte es:

myFrame.setIconImage(img.getImage()); 

Auch Kasse setIconImages(), die stattdessen eine Liste nimmt .

How to change JFrame icon

Es ist nicht meine Antwort !!!!

Verwandte Themen