- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在编写一个贪吃蛇游戏(复古街机游戏),在实现碰撞检测时,我意识到它根本不起作用。这不是我第一次不得不使用碰撞检测,所以我认为问题不在那里(但它可能!)。相反,我认为这与外部类以及坐标未更新有关,但也可能是其他原因。这是代码:
注意:这不是完整的代码,但为了避免混淆...这是所有类的列表(是的,我知道,有很多...但我喜欢组织起来)
主要看法模型移动 Action 实体(接口(interface))食物部分蛇方向(枚举)
型号
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);
}
}
}
}
最佳答案
您需要在更新方法中的某个地方调用 checkFoodCol 方法,例如
public void update() {
Segment snakeHead = snake.getSegments()[0];
checkFoodCol(snakeHead);
changeHeadLoc(snakeHead);
changeSegmentLoc();
}
关于java - 碰撞检测在贪吃蛇游戏中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873758/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!