2017-05-03 99 views
2

我想通過使用java.My觀看教程來製作蛇遊戲問題是,蛇從開始就與自身發生碰撞。貪吃蛇遊戲運動問題

這裏是我的主類 「Main.java」

import java.awt.Color; 
import javax.swing.JFrame; 

public class Main { 
    public static void main(String[] args) { 
     JFrame obj = new JFrame(); 
     Gameplay gameplay = new Gameplay(); 
     obj.setBounds(10, 10, 905, 700); 
     obj.setBackground(Color.DARK_GRAY); 
     obj.setResizable(false); 
     obj.setVisible(true); 
     obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     obj.add(gameplay); 
    } 
} 

這裏是我的遊戲類名爲 「Gameplay.java」

import java.awt.*; 
import java.awt.event.*; 
import java.util.Random; 
import javax.swing.*; 

public class Gameplay extends JPanel implements KeyListener, ActionListener { 
    private int[] snakexlength = new int[750]; 
    private int[] snakeylength = new int[750]; 
    private boolean left = false; 
    private boolean right = false; 
    private boolean up = false; 
    private boolean down = false; 

    private ImageIcon rightmouth; 
    private ImageIcon upmouth; 
    private ImageIcon downmouth; 
    private ImageIcon leftmouth; 

    private int lengthofsnake = 3; 

    private Timer timer; 
    private int delay = 100; 

    private ImageIcon snakeimage; 
    private ImageIcon titleImage; 
    private ImageIcon enemyimage; 

    private int[] enemyxpos = { 
     25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 
     450, 475, 500, 525, 550, 575, 600, 625, 650, 675, 700, 725, 750, 775, 800, 825, 850 }; 
    private int[] enemyypos = { 
     75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 
     500, 525, 550, 575, 600, 625 }; 

    private Random random = new Random(); 

    private int xpos = random.nextInt(34); 
    private int ypos = random.nextInt(23); 

    private int score = 0; 
    private int moves = 0; 

    public Gameplay() { 
     addKeyListener(this); 
     setFocusable(true); 
     setFocusTraversalKeysEnabled(false); 
     timer = new Timer(delay, this); 
     timer.start(); 
    } 

    public void paint(Graphics g) { 
     if (moves == 0) { 
      snakexlength[2] = 50; 
      snakexlength[1] = 75; 
      snakexlength[0] = 100; 

      snakeylength[2] = 100; 
      snakeylength[1] = 100; 
      snakeylength[0] = 100; 
     } 

     // draw title image border 
     g.setColor(Color.WHITE); 
     g.drawRect(24, 10, 851, 55); 

     // draw the title image 
     titleImage = new ImageIcon("snaketitle.jpg"); 
     titleImage.paintIcon(this, g, 25, 11); 

     // draw border for gameplay 
     g.setColor(Color.WHITE); 
     g.drawRect(24, 74, 851, 577); 

     // draw background for the gameplay 
     g.setColor(Color.BLACK); 
     g.fillRect(25, 75, 850, 575); 

     // draw scores 
     g.setColor(Color.red); 
     g.setFont(new Font("arial", Font.BOLD, 20)); 
     g.drawString("Score: " + score, 780, 30); 

     // draw length 
     g.setColor(Color.red); 
     g.setFont(new Font("arial", Font.BOLD, 20)); 
     g.drawString("Length: " + lengthofsnake, 780, 50); 

     rightmouth = new ImageIcon("rightmouth.png"); 
     rightmouth.paintIcon(this, g, snakexlength[0], snakeylength[0]); 

     for (int a = 0; a < lengthofsnake; a++) 

     { 
      if (a == 0 && right == true) { 
       rightmouth = new ImageIcon("rightmouth.png"); 
       rightmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]); 
      } 
      if (a == 0 && left == true) { 
       leftmouth = new ImageIcon("leftmouth.png"); 
       leftmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]); 
      } 
      if (a == 0 && up == true) { 
       upmouth = new ImageIcon("upmouth.png"); 
       upmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]); 
      } 
      if (a == 0 && down == true) { 
       downmouth = new ImageIcon("downmouth.png"); 
       downmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]); 
      } 
      if (a != 0) { 
       snakeimage = new ImageIcon("snakeimage.png"); 
       snakeimage.paintIcon(this, g, snakexlength[a], snakeylength[a]); 
      } 

     } 

     enemyimage = new ImageIcon("enemy.png"); 

     if ((enemyxpos[xpos] == snakexlength[0] && enemyypos[ypos] == snakeylength[0])) { 
      scorelength(); 
      ratrandom(); 
      /* 
      * score++; lengthofsnake++; xpos = random.nextInt(34); ypos = 
      * random.nextInt(23); 
      */ 
     } 
     enemyimage.paintIcon(this, g, enemyxpos[xpos], enemyypos[xpos]); 

     for (int b = 1; b < lengthofsnake; b++) { 
      if (snakexlength[b] == snakexlength[0] && snakeylength[0] == snakeylength[0]) { 
       right = false; 
       left = false; 
       up = false; 
       down = false; 
       g.setColor(Color.white); 
       g.setFont(new Font("arial", Font.BOLD, 50)); 
       g.drawString("Game Overs", 300, 300); 
       g.setFont(new Font("arial", Font.BOLD, 20)); 
       g.drawString("Spacebar to AGAIN play", 350, 340); 
      } 
     } 

     g.dispose(); 
    } 

    public void scorelength() { 
     score += 5; 
     lengthofsnake++; 
    } 

    public void ratrandom() { 
     xpos = random.nextInt(34); 
     ypos = random.nextInt(23); 
    } 

    @Override 
    public void keyPressed(KeyEvent e) { 

     if (e.getKeyCode() == KeyEvent.VK_SPACE) { 
      lengthofsnake = 3; 
      score = 0; 
      moves = 0; 
      repaint(); 
     } 
     if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
      moves++; 
      right = true; 
      if (left == false) { 
       right = true; 
      } else { 
       left = true; 
       right = false; 
      } 
      up = false; 
      down = false; 
     } 
     if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
      moves++; 
      left = true; 
      if (right == false) { 
       left = true; 
      } else { 
       left = false; 
       right = true; 
      } 
      up = false; 
      down = false; 
     } 
     if (e.getKeyCode() == KeyEvent.VK_UP) { 
      moves++; 
      up = true; 
      if (down == false) { 
       up = true; 
      } else { 
       down = true; 
       up = false; 
      } 
      left = false; 
      right = false; 
     } 
     if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
      moves++; 
      down = true; 
      if (up == false) { 
       down = true; 
      } else { 
       up = true; 
       down = false; 
      } 
      left = false; 
      right = false; 
     } 

    } 

    public void actionPerformed(ActionEvent e) { 
     timer.start(); 
     if (right == true) { 
      for (int r = lengthofsnake - 1; r >= 0; r--) { 
       snakeylength[r + 1] = snakeylength[r]; 
      } 

      for (int r = lengthofsnake; r >= 0; r--) { 
       if (r == 0) { 
        snakexlength[r] = snakeylength[r] + 25; 
       } else { 
        snakexlength[r] = snakeylength[r - 1]; 
       } 
       if (snakexlength[r] > 850) { 
        snakexlength[r] = 25; 
       } 
      } 
      repaint(); 
     } 
     if (left == true) { 
      for (int r = lengthofsnake - 1; r >= 0; r--) { 
       snakeylength[r + 1] = snakeylength[r]; 
      } 

      for (int r = lengthofsnake; r >= 0; r--) { 
       if (r == 0) { 
        snakexlength[r] = snakeylength[r] - 25; 
       } else { 
        snakexlength[r] = snakeylength[r - 1]; 
       } 
       if (snakexlength[r] < 25) { 
        snakexlength[r] = 850; 
       } 
      } 
      repaint(); 
     } 
     if (up) { 
      for (int r = lengthofsnake - 1; r >= 0; r--) { 
       snakexlength[r + 1] = snakexlength[r]; 
      } 

      for (int r = lengthofsnake; r >= 0; r--) { 
       if (r == 0) { 
        snakeylength[r] = snakeylength[r] - 25; 
       } else { 
        snakeylength[r] = snakeylength[r - 1]; 
       } 
       if (snakeylength[r] < 75) { 
        snakeylength[r] = 625; 
       } 
      } 
      repaint(); 
     } 
     if (down == true) { 
      for (int r = lengthofsnake - 1; r >= 0; r--) { 
       snakexlength[r + 1] = snakexlength[r]; 
      } 

      for (int r = lengthofsnake; r >= 0; r--) { 
       if (r == 0) { 
        snakeylength[r] = snakexlength[r] + 25; 
       } else { 
        snakeylength[r] = snakeylength[r - 1]; 
       } 
       if (snakeylength[r] > 625) { 
        snakeylength[r] = 75; 
       } 
      } 
      repaint(); 
     } 
    } 

    @Override 
    public void keyReleased(KeyEvent e) { } 

    @Override 
    public void keyTyped(KeyEvent e) { } 
} 

需要幫助找出問題

+1

現在可能是學習如何使用調試器的好時機。 – assylias

+0

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – slim

+0

使用'paintComponent(g)',而不是'paint(g)'...另外,不要重新加載每次繪製圖像。只加載一次。 –

回答

2

這是你的代碼的這一部分,看起來是造成這個問題:

if(snakexlength[b] == snakexlength[0]&& snakeylength[0] == snakeylength[0]) 

,馬上就可以看到,第二部分將始終評估爲真(這看起來像本身就是一個錯誤),所以它顯然有些問題,第一部分:

snakexlength[b] == snakexlength[0] 

它不是立即清除爲什麼發生這種情況,但如果我必須做出一個假設,我會說,這是與你如何「初始化」蛇,因爲這個錯誤顯然是直接發生的。我注意到在幾個地方你設置了snakexlength[r] = snakexlength[r-1],反之亦然,所以在這裏你的程序的控制流程很可能是一個問題,這意味着snakexlength中的兩個索引短暫相等。

+0

https://drive.google.com/open?id=0B7T9GuR-YFfrX19xMU9neHBDb3M這裏是包含文件的源代碼。你可以看看 –