食物对蛇的影响没有影响

时间:2016-08-19 12:26:36

标签: java

我正在尝试制作蛇游戏,我想在继续之前解决这个问题。我对食物有问题。当蛇头的坐标与食物坐标相匹配时,得分不会增加。我怀疑的是'foodPhysics()'方法。由于我是初学者,好的解释可能会有所帮助。

public class Game2 extends JComponent implements ActionListener, KeyListener {

    public int x = 350, y = 350;
    public static Game2 game;
    public boolean up, down, left, right;
    public int vx, vy, fx, fy, score;

    public Random generator;

    public static void main(String[] args) {
        game = new Game2();
    }

    public Game2() {
        generator = new Random();
        fx = 7 + generator.nextInt(780);
        fy = 7 + generator.nextInt(780);
        JFrame window = new JFrame("Game");
        window.setSize(700, 700);
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Timer t = new Timer(10, this);

        window.add(this);

        window.addKeyListener(this);
        window.setVisible(true);
        head = new Point(x, y);

        t.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (up) {
            vy = -10;
            y = y + vy;
        }
        if (down) {
            vy = 10;
            y = y + vy;
        }
        if (left) {
            vx = -10;
            x = x + vx;
        }
        if (right) {
            vx = 10;
            x = x + vx;
        }
        if (x == 0 && vx == -10) {
            x = 800;
            vy = 0;
        }
        if (x == 800 && vx == 10) {
            x = 0;
            vy = 0;
        }
        if (y == 0 && vy == -10) {
            y = 800;
            vx = 0;
        }
        if (y == 800 && vy == 10) {
            y = 0;
            vx = 0;
        }

        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int c = e.getKeyCode();
        if (c == KeyEvent.VK_UP) {
            up = true;
            down = false;
            left = false;
            right = false;
        }
        if (c == KeyEvent.VK_DOWN) {
            up = false;
            down = true;
            left = false;
            right = false;
        }
        if (c == KeyEvent.VK_LEFT) {
            up = false;
            down = false;
            left = true;
            right = false;
        }
        if (c == KeyEvent.VK_RIGHT) {
            up = false;
            down = false;
            left = false;
            right = true;
        }
        if (foodPhysics() == true) {
            score++;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 15, 15);
        foodRender(g);
        g.setFont(new Font("Arial", 25, 25));
        g.drawString(Integer.toString(score), 500, 500);
    }

    public void foodRender(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(fx, fy, 15, 15);
    }

    public boolean foodPhysics() {
        if (x == fx && y == fy) {
            return true;
        }
        return false;
    }

}

2 个答案:

答案 0 :(得分:3)

我会相信你的代码目前无法编译。你似乎设置了一个head变量等于" new Point(x,y)"但是你没有在代码中声明一个head变量。除此之外。

在foodPhysics()方法中。

public boolean foodPhysics(){
    Rectangle rect = new Rectangle(fx,fy,15,15) //the size of your food.
    if(rect.contains(head)){                    //you'll need to create this variable 
       return true;
    }
    return false;
}

这会给你一些区域,因为在你的代码中,只有当它完全通过相同的像素时,该点才会递增。一定要在actionPerformed()方法中重置头部。

@Override
public void actionPerformed(ActionEvent e) {

    if (up) {
        vy = -10;
        y = y + vy;
    }
    if (down) {
        vy = 10;
        y = y + vy;
    }
    .
    .
    .

    head = new Point(x,y);

    repaint();
}

如果将蛇头的渲染更改为:

,那可能会更好
g.fillRect(x-7,y-7,15,15);

如果我正确,这将使head = new point(x,y)大致位于绘制矩形的中心。但我实际上认为,如果你能完美地击中像素,那么你现在拥有的代码应该能正常工作。

如果您需要更多帮助,请告诉我们,我们很乐意为您提供帮助。

答案 1 :(得分:0)

如果按箭头键放开,你的蛇会移动。这导致蛇“跳过”一段距离。因此导致您的支票不匹配。

如果您尝试下面粘贴的示例代码,“按住箭头键将蛇与食物碰撞”会增加分数。 试试这个

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Game2 extends JComponent implements ActionListener, KeyListener {

    public int x = 350, y = 350;
    public static Game2 game;
    public boolean up, down, left, right;
    public int vx, vy, fx, fy, score;

    public Point head;
    public Random generator;

    public static void main(String[] args) {
        game = new Game2();
    }

    public Game2() {
        generator = new Random();
        fx = 7 + generator.nextInt(780);
        fy = 7 + generator.nextInt(780);
        JFrame window = new JFrame("Game");
        window.setSize(700, 700);
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Timer t = new Timer(10, this);

        window.add(this);

        window.addKeyListener(this);
        window.setVisible(true);
        head = new Point(x, y);

        t.start();

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        int speed = 1;
        if (up) {
            vy = -speed;
            y = y + vy;
        }
        if (down) {
            vy = speed;
            y = y + vy;
        }
        if (left) {
            vx = -speed;
            x = x + vx;
        }
        if (right) {
            vx = speed;
            x = x + vx;
        }
        if (x == 0 && vx == -speed) {
            x = 800;
            vy = 0;
        }
        if (x == 800 && vx == speed) {
            x = 0;
            vy = 0;
        }
        if (y == 0 && vy == -speed) {
            y = 800;
            vx = 0;
        }
        if (y == 800 && vy == speed) {
            y = 0;
            vx = 0;
        }

        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int c = e.getKeyCode();
        if (c == KeyEvent.VK_UP) {
            up = true;
            down = false;
            left = false;
            right = false;
        }
        if (c == KeyEvent.VK_DOWN) {
            up = false;
            down = true;
            left = false;
            right = false;
        }
        if (c == KeyEvent.VK_LEFT) {
            up = false;
            down = false;
            left = true;
            right = false;
        }
        if (c == KeyEvent.VK_RIGHT) {
            up = false;
            down = false;
            left = false;
            right = true;
        }
        if (foodPhysics() == true) {
            score++;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 15, 15);
        foodRender(g);
        g.setFont(new Font("Arial", 25, 25));
        g.drawString(Integer.toString(score), 500, 500);
    }

    //improve this by limiting the fx, fy in bound
    public void foodRender(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(fx, fy, 15, 15);
    }

    public boolean foodPhysics() {

        return Math.abs(x-fx) < 30 && Math.abs(y-fy) < 30;
    }

}

一,我放慢你的蛇的速度,这样更容易测试。 二,我改变了你的检查碰撞方法。