2016-03-08 38 views
0

我一直在寫蛇遊戲(復古街機之一),並在實現碰撞檢測時,我意識到它甚至都沒有工作。這不是我第一次使用碰撞檢測,所以我不認爲問題在那裏(但它可以!)。相反,我認爲它與外部類有關,而關於座標不在更新的內容,但它也可能是其他內容。這裏是代碼:碰撞檢測不能在Snake遊戲中工作

注意:這不是完整的代碼,但爲了避免混淆......這裏是所有類的列表(是的,我知道,有很多......但我喜歡組織)

主要 查看 型號 MoveAction 實體(接口) 食品 段 蛇 方向(ENUM)

型號

import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.util.List; 

import javax.swing.Timer; 

public class Model { 
    List<Entity> entities; 
    List<Direction> direction; 
    Snake snake; 
    Segment snakeHead; 
    Food food; 
    View view; 
    int timeSpent = 0; 

    public Model() { 
     entities = new ArrayList<Entity>(3); 
     direction = new ArrayList<Direction>(); 
     direction.add(Direction.RIGHT); 
     snake = new Snake(new Point(100, 100)); 
     food = new Food(new Point(300, 200)); 
     entities.add(snake); 
     entities.add(food); 

     Timer timer = new Timer(5, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       update(); 
       view.repaint(); 
      } 
     }); 
     timer.start(); 
    } 

    public void update() { 
     Segment snakeHead = snake.getSegments()[0]; 
     changeHeadLoc(snakeHead); 
     changeSegmentLoc(); 

    } 

    public void checkFoodCol(Segment snakeHead) { 
     if (snakeHead.getLocation().x + snakeHead.getSize().width > food.getLocation().x) { 
      System.out.println("1"); 
      // snake.increaseSegmentCount(1); 
     } 
     if (snakeHead.getLocation().x < food.getLocation().x + food.getSize().width) { 
      System.out.println("2"); 
      // snake.increaseSegmentCount(2); 
     } 
     if (snakeHead.getLocation().y + snakeHead.getSize().height > food.getLocation().y) { 
      System.out.println("3"); 
      // snake.increaseSegmentCount(3); 
     } 
     if (snakeHead.getLocation().y < food.getLocation().y + food.getSize().height) { 
      System.out.println("4"); 
      // snake.increaseSegmentCount(4); 
     } 
    } 

    public void changeHeadLoc(Segment snakeHead) { 
     if (direction.contains(Direction.UP)) { 
      snakeHead.setLocation(new Point(snakeHead.getLocation().x, snakeHead.getLocation().y - 1)); 
     } 
     if (direction.contains(Direction.DOWN)) { 
      snakeHead.setLocation(new Point(snakeHead.getLocation().x, snakeHead.getLocation().y + 1)); 
     } 
     if (direction.contains(Direction.LEFT)) { 
      snakeHead.setLocation(new Point(snakeHead.getLocation().x - 1, snakeHead.getLocation().y)); 
     } 
     if (direction.contains(Direction.RIGHT)) { 
      snakeHead.setLocation(new Point(snakeHead.getLocation().x + 1, snakeHead.getLocation().y)); 

     } 
    } 

    public void changeSegmentLoc() { 
     if (!direction.isEmpty()) { 
      for (int i = 1; i <= snake.getSegmentCount(); i++) { 
       Segment currentSegment = snake.getSegments()[i]; 
       Segment previousSegment = snake.getSegments()[i - 1]; 
       if (currentSegment != null) { 
        if (direction.contains(Direction.UP)) { 
         currentSegment.setLocation(
           new Point(previousSegment.getLocation().x, previousSegment.getLocation().y - 1)); 
        } 
        if (direction.contains(Direction.DOWN)) { 
         currentSegment.setLocation(
           new Point(previousSegment.getLocation().x, previousSegment.getLocation().y + 1)); 
        } 
        if (direction.contains(Direction.LEFT)) { 
         currentSegment.setLocation(
           new Point(previousSegment.getLocation().x - 1, previousSegment.getLocation().y)); 
        } 
        if (direction.contains(Direction.RIGHT)) { 
         currentSegment.setLocation(
           new Point(previousSegment.getLocation().x + 1, previousSegment.getLocation().y)); 
        } 
       } 
      } 
     } 
    } 

    public Entity[] getEntities() { 
     return entities.toArray(new Entity[0]); 
    } 

    public List<Direction> getDirection() { 
     return direction; 
    } 

    public void setView(View view) { 
     this.view = view; 
    } 
} 

食品

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Point; 

public class Food implements Entity { 
    Point location; 
    Dimension size = new Dimension(15, 15); 

    public Food(Point location) { 
     this.location = location; 
    } 

    public Dimension getSize() { 
     return size; 
    } 

    public Point getLocation() { 
     return location; 
    } 

    @Override 
    public void paint(Graphics g) { 
     g.setColor(Color.RED); 
     g.fillRect(getLocation().x, getLocation().y, getSize().width, getSize().height); 
    } 
} 

import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.Graphics; 
    import java.awt.Point; 

    public class Segment { 
     Point location; 
     Point oldLocation; 

     public Segment(Point location) { 
      this.location = location; 
     } 

     public Dimension getSize() { 
      return new Dimension(20, 20); 
     } 

     public void setLocation(Point location) { 
      oldLocation = location; 
      this.location = location; 
     } 

     public Point getLocation() { 
      return location; 
     } 

     public Point getOldLocation() { 
      return oldLocation; 
     } 

     public void paint(Graphics g) { 
      g.setColor(Color.WHITE); 
      g.fillRect(location.x, location.y, 20, 20); 
     } 
    } 

Snake 
import java.awt.Graphics; 
import java.awt.Point; 

public class Snake implements Entity { 
    Point location; 
    Segment[] segments; 
    int segmentCount = 0; 

    public Snake(Point location) { 
     this.location = location; 
     segments = new Segment[25]; 
     segments[segmentCount] = new Segment(new Point(location.x, location.y)); 

     segmentCount++; 
    } 

    public void increaseSegmentCount(int relativeTo) { 
     // TODO Add code to make it add the new Segment correctly 

     segments[segmentCount] = new Segment(
       new Point(segments[segmentCount - 1].getLocation().x + segments[segmentCount - 1].getSize().width, 
         segments[segmentCount - 1].getLocation().y + segments[segmentCount - 1].getSize().height)); 

     if (relativeTo == 1) { 

     } 

     segmentCount++; 

    } 

    public Segment[] getSegments() { 
     return segments; 
    } 

    public int getSegmentCount() { 
     return segmentCount; 
    } 

    @Override 
    public void paint(Graphics g) { 
     for (Segment segment : segments) { 
      if (segment != null) { 
       segment.paint(g); 
      } 
     } 
    } 
} 
+0

我沒有看到任何調用上述checkFoodCol的代碼。這應該是你的更新方法嗎? – ManoDestra

+0

Omg ...我怎麼可能錯過了?我很抱歉浪費你的時間,我仍然是一個新手程序員,所以這種事情發生了很多。 – Eames

+0

沒問題。作爲答案添加。很高興能幫上忙 :) – ManoDestra

回答

1

你需要的地方例如你打電話update方法裏面你checkFoodCol方法

public void update() { 
    Segment snakeHead = snake.getSegments()[0]; 
    checkFoodCol(snakeHead); 
    changeHeadLoc(snakeHead); 
    changeSegmentLoc(); 
}