2016-08-19 47 views
-3

我想做一个蛇游戏,我想在继续之前解决这个问题。我有问题的食物。当蛇头的坐标与食物坐标相匹配时,得分不会递增。我怀疑存在于'食物物理()'方法中。由于我是初学者,很好的解释可能会有所帮助。对食物没有影响Collison与蛇

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; 
    } 

} 
+0

您可以重新格式化代码,以便可以阅读它吗? – xenteros

+1

我已经做了一个编辑并重新设置了它们的代码。它应该很快出现。 @xenteros – byxor

回答

3

我导致相信你的代码现在不会编译。你似乎设置了一个头变量等于“new Point(x,y)”,但是你没有在代码中声明的头变量。那边。

在你的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); 

如果我是正确的,这将使得头部=新的点(X,Y)大致位于绘制的矩形的中心。但我实际上认为,如果你能完美地击中像素,那么你此时的代码应该正常运行。

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

+0

非常感谢!我不知道在Rectangle类中有一个名为contains()的方法。对于Point声明,我声明了它,但我只是没有将它添加到代码中,只是忘记了。再次感谢您的帮助和详细解释。 –

+0

我将编辑我的文章,以便您了解我将如何执行此操作。 –

+0

感谢您的努力。但是在食物类中我没有看到任何setRemove()方法,并且render方法只能在paintComponent(Graphics g)方法中使用,而不是在main()方法中使用。所以不能将主要方法的整个代码(从你的声明的时间变量)传递给paintComponent()方法? –

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; 
    } 

} 

一个,我放慢你的蛇那么容易测试。 二,我改变了你的检查碰撞方法。

+0

但是不会让蛇太慢?我想要它快一点.. –

+0

如果你愿意,可以增加你的蛇,我让它变慢,所以很容易看到效果。为了提高你的速度,请向上计算整型速度变量。 – Xiaoerge