我正在计算机科学课上为这个项目创建Snake游戏,注意我对Java没有多少经验。它目前尚未完成,我似乎无法解决这个问题。我正在使用JPanels来制作蛇和环境。我的问题是,当我用蛇急转弯时,它会弄乱并且不会删除一些蛇的身体(这个问题似乎只会在我添加一个额外的身体部件时发生3,所以为此打了一次E)。我使用WASD或箭头键当前移动,并在蛇身上添加一个身体部分我使用钥匙E.我有苹果作为身体,以帮助我在我进行故障排除和摇滚时显示最后一块当我向前推进时被删除的尾巴。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SnakeGame extends JFrame implements KeyListener{
//snake stuff
static ArrayList<Integer> snakeX = new ArrayList<Integer>();
static ArrayList<Integer> snakeY = new ArrayList<Integer>();
static JLabel[][] grid = new JLabel[25][25];
static ImageIcon blacksquare = new ImageIcon("blacksquare.jpg");
static ImageIcon wall = new ImageIcon("wall.jpg");
static ImageIcon snakebody = new ImageIcon("snakebody.jpg");
static ImageIcon snakeheadup = new ImageIcon("snakeheadup.jpg");
static ImageIcon snakeheaddown = new ImageIcon("snakeheaddown.jpg");
static ImageIcon snakeheadleft = new ImageIcon("snakeheadleft.jpg");
static ImageIcon snakeheadright = new ImageIcon("snakeheadright.jpg");
static ImageIcon apple = new ImageIcon("apple.jpg");
static ImageIcon rock = new ImageIcon("rock.jpg");
static boolean up = false;
static boolean down = false;
static boolean left = false;
static boolean right = false;
static int lastDirection = 1;
static int newPartX;
static int newPartY;
public SnakeGame(){
super("Snake Game Frame");//Make a frame
addKeyListener(this);
setSize(500, 500);//Give it a size
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//Make it go away on close
JPanel panel = new JPanel(new GridLayout(25,25));//Make a panel
add(panel);//Add it to your frame
//Set up background
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[i].length; j++){
grid[i][j] = new JLabel(blacksquare);
panel.add(grid[i][j]);
}
}
setVisible(true);//Show the frame
//Set up walls
for(int i = 0; i < grid.length; i++){
grid[0][i].setIcon(wall);
grid[24][i].setIcon(wall);
grid[i][0].setIcon(wall);
grid[i][24].setIcon(wall);
}
//starting position
grid[17][12].setIcon(snakeheadup);
grid[18][12].setIcon(snakebody);
grid[19][12].setIcon(snakebody);
//grid[5][7].setIcon(rock);
//grid[15][14].setIcon(apple);
snakeX.add(12);
snakeX.add(12);
snakeX.add(12);
snakeY.add(17);
snakeY.add(18);
snakeY.add(19);
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_W)
{
up = true;
down = false;
left = false;
right = false;
slither();
}
else if(key == KeyEvent.VK_A)
{
up = false;
down = false;
left = true;
right = false;
slither();
}
else if(key == KeyEvent.VK_S)
{
up = false;
down = true;
left = false;
right = false;
slither();
}
else if(key == KeyEvent.VK_D)
{
up = false;
down = false;
left = false;
right = true;
slither();
}
else if(key == KeyEvent.VK_E)
{
newPartX = snakeX.get(snakeX.size()-1); // add a piece
newPartY = snakeY.get(snakeY.size()-1); // add a piece
slither();
snakeX.add(newPartX);
snakeY.add(newPartY);
grid[snakeY.get(snakeY.size()-1)][snakeX.get(snakeX.size()-1)].setIcon(apple); // add a body part
}
}
public void keyReleased(KeyEvent e) { //I dont really use these!
}
public void keyTyped(KeyEvent e) { //I dont really use these!
}
public static void slither(){ // add last direction can't be opposite
if(up == true){
if(lastDirection != 3){
snakeX.add(snakeX.get(snakeX.size()-1)); // add a temporary element to the arraylist
snakeY.add(snakeY.get(snakeY.size()-1)); // add a temporary element to the arraylist
for (int i = snakeY.size()-1; i > 0; i--){
snakeY.set(i,snakeY.get(i-1)); // save new position of body parts
snakeX.set(i,snakeX.get(i-1)); // save new position of body parts
}
for (int i = 0; i < snakeY.size()-1; i++){
grid[snakeY.get(i)][snakeX.get(i)].setIcon(apple); // replace previous body parts with new ones
}
grid[snakeY.get(snakeY.size()-1)][snakeX.get(snakeX.size()-1)].setIcon(rock); // replace tail with black
snakeX.remove(snakeX.get(snakeX.size()-1)); // remove the added piece
snakeY.remove(snakeY.get(snakeY.size()-1)); // remove the added piece
snakeX.set(0,snakeX.get(0)); // new head placement
snakeY.set(0,snakeY.get(0)-1); // new head placement
grid[snakeY.get(0)][snakeX.get(0)].setIcon(snakeheadup); // add head
lastDirection = 1;
}
}
else if(left == true){
if(lastDirection != 4){
snakeX.add(snakeX.get(snakeX.size()-1)); // add a temporary element to the arraylist
snakeY.add(snakeY.get(snakeY.size()-1)); // add a temporary element to the arraylist
for (int i = snakeY.size()-1; i > 0; i--){
snakeY.set(i,snakeY.get(i-1)); // save new position of body parts
snakeX.set(i,snakeX.get(i-1)); // save new position of body parts
}
for (int i = 1; i < snakeY.size()-1; i++){
grid[snakeY.get(i)][snakeX.get(i)].setIcon(apple); // replace previous body parts with new ones
}
grid[snakeY.get(snakeY.size()-1)][snakeX.get(snakeX.size()-1)].setIcon(rock); // replace tail with black
snakeX.remove(snakeX.get(snakeX.size()-1)); // remove the added piece
snakeY.remove(snakeY.get(snakeY.size()-1)); // remove the added piece
snakeX.set(0,snakeX.get(0)-1); // new head placement
snakeY.set(0,snakeY.get(0)); // new head placement
grid[snakeY.get(0)][snakeX.get(0)].setIcon(snakeheadleft); // add head
lastDirection = 2;
}
}
else if(down == true){
if(lastDirection != 1){
snakeX.add(snakeX.get(snakeX.size()-1)); // add a temporary element to the arraylist
snakeY.add(snakeY.get(snakeY.size()-1)); // add a temporary element to the arraylist
for (int i = snakeY.size()-1; i > 0; i--){
snakeY.set(i,snakeY.get(i-1)); // save new position of body parts
snakeX.set(i,snakeX.get(i-1)); // save new position of body parts
}
for (int i = 1; i < snakeY.size()-1; i++){
grid[snakeY.get(i)][snakeX.get(i)].setIcon(apple); // replace previous body parts with new ones
}
grid[snakeY.get(snakeY.size()-1)][snakeX.get(snakeX.size()-1)].setIcon(rock); // replace tail with black
snakeX.remove(snakeX.get(snakeX.size()-1)); // remove the added piece
snakeY.remove(snakeY.get(snakeY.size()-1)); // remove the added piece
snakeX.set(0,snakeX.get(0)); // new head placement
snakeY.set(0,snakeY.get(0)+1); // new head placement
grid[snakeY.get(0)][snakeX.get(0)].setIcon(snakeheaddown); // add head
lastDirection = 3;
}
}
else if(right == true){
if(lastDirection != 2){
snakeX.add(snakeX.get(snakeX.size()-1)); // add a temporary element to the arraylist
snakeY.add(snakeY.get(snakeY.size()-1)); // add a temporary element to the arraylist
for (int i = snakeY.size()-1; i > 0; i--){
snakeY.set(i,snakeY.get(i-1)); // save new position of body parts
snakeX.set(i,snakeX.get(i-1)); // save new position of body parts
}
for (int i = 1; i < snakeY.size()-1; i++){
grid[snakeY.get(i)][snakeX.get(i)].setIcon(apple); // replace previous body parts with new ones
}
grid[snakeY.get(snakeY.size()-1)][snakeX.get(snakeX.size()-1)].setIcon(rock); // replace tail with black
snakeX.remove(snakeX.get(snakeX.size()-1)); // remove the added piece
snakeY.remove(snakeY.get(snakeY.size()-1)); // remove the added piece
snakeX.set(0,snakeX.get(0)+1); // new head placement
snakeY.set(0,snakeY.get(0)); // new head placement
grid[snakeY.get(0)][snakeX.get(0)].setIcon(snakeheadright); // add head
lastDirection = 4;
}
}
}
public static void main(String[] args){
SnakeGame game = new SnakeGame();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Here是我所谈论的一些例子。
可以找到图像here。
提前致谢。
答案 0 :(得分:1)
问题出在您拨打remove
的电话上。每个坐标列表都可以包含重复项,并且正在删除错误的条目。你真的需要使用remove
的另一个版本 - 你传递位置的那个版本(作为int
)而不是传递你想要删除的对象。例如,
snakeX.remove(snakeX.size()-1);
而不是
snakeX.remove(snakeX.get(snakeX.size()-1));