Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 蛇游戏。如何使蛇在吃烂苹果时失去一条尾巴?_Java_Linked List - Fatal编程技术网

Java 蛇游戏。如何使蛇在吃烂苹果时失去一条尾巴?

Java 蛇游戏。如何使蛇在吃烂苹果时失去一条尾巴?,java,linked-list,Java,Linked List,我已经在游戏中添加了苹果,并添加了一个功能,使苹果在经过蛇的某些步骤后腐烂。但是我想让蛇在吃烂苹果时失去一条尾巴 我已经有了每次蛇吃东西都会加一条尾巴的方法。我只是需要一些帮助。我试过使用pollLast()和removeLast(),但我想不出来 *包Game.Entities.Dynamic; 导入Main.Handler; 导入资源、图像; 导入java.awt.*; 导入java.awt.event.KeyEvent; 导入java.io.BufferedReader; 导入java.i

我已经在游戏中添加了苹果,并添加了一个功能,使苹果在经过蛇的某些步骤后腐烂。但是我想让蛇在吃烂苹果时失去一条尾巴

我已经有了每次蛇吃东西都会加一条尾巴的方法。我只是需要一些帮助。我试过使用pollLast()和removeLast(),但我想不出来

*包Game.Entities.Dynamic;
导入Main.Handler;
导入资源、图像;
导入java.awt.*;
导入java.awt.event.KeyEvent;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.Random;
导入javax.swing.JOptionPane;
导入Display.DisplayScreen;
导入Game.GameStates.State;
/**
*由AlexVR于2018年7月2日创建。
*/
公开课选手{
公共内部长度;
公共公正;
私人经办人;
公共国际合作;
公共国际合作;
公众智力得分;
公共int步骤;
公共计数器;
公共字符串方向;
私人字符串高分;
私人彩色播放器颜色;
私用彩色appleColor;
私人色彩;
私人int速度调整;
私人尾巴;
私有字符串的声音效果;
私有字符串死亡声音效应;
私有布尔声音循环;
公共玩家(处理者){
this.handler=handler;
xCoord=0;
yCoord=0;
得分=0;
步长=0;
highscore=this.getHighScore();
moveCounter=0;
direction=“Right”;
正义=虚假;
长度=1;
playerColor=Color.green;
appleColor=Color.red;
ROTTNAPPLECOLOR=颜色为黑色;
速度调整=20;
eatSoundEffect=“res/music/bite.wav”;
deathSoundEffect=“res/music/sound frogger dead.wav”;
soundLoop=true;
}
公共空白勾号(){
移动计数器+=2;
如果(移动计数器>=速度调整){
选中CollisionAndMove();
检查步骤();
moveCounter=0;
}
if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_UP)| handler.getKeyManager().keyJustPressed(KeyEvent.VK_W)){
方向=“向上”;
}
if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_DOWN)| | handler.getKeyManager().keyJustPressed(KeyEvent.VK_S)){
方向=“向下”;
}
if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_左)| handler.getKeyManager().keyJustPressed(KeyEvent.VK_左){
方向=“左”;
}
if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_RIGHT)| handler.getKeyManager().keyJustPressed(KeyEvent.VK_D)){
direction=“Right”;
}
if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_EQUALS)){
speedAdjust-=2;
}
if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_减号)){
速度调整+=2;
}
if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_N)){
此.setjuste(false);
EatAndAddTail();
}   
}
公共无效检查碰撞和移动(){
handler.getWorld();
int x=xCoord;
int y=yCoord;
steps++;
开关(方向){
案例“左”:
如果(xCoord==0){
xCoord=handler.getWorld().GridWidthHeightPixelCount-1;
}否则{
xCoord--;
}
打破
案例“权利”:
if(xCoord==handler.getWorld().GridWidthHeightPixelCount-1){
xCoord=0;
}否则{
xCoord++;
}
打破
案例“Up”:
如果(yCoord==0){
yCoord=handler.getWorld().GridWidthHeightPixelCount-1;
}否则{
yCoord——;
}
打破
案例“向下”:
if(yCoord==handler.getWorld().GridWidthHeightPixelCount-1){
yCoord=0;
}否则{
yCoord++;
}
打破
}
handler.getWorld();
if(handler.getWorld().body.size()>0){
for(int i=0;i0 | | yCoord>0)| | justAte==true){
DisplayScreen.setMessage(String.format(“当前分数:%d;%s”,分数,高分));
}
如果(!handler.getWorld().body.isEmpty()){
handler.getWorld().playerLocation[handler.getWorld().body.getLast().x][handler.getWorld().body.getLast().y]=false;
handler.getWorld().body.removeLast();
handler.getWorld().body.addFirst(新尾部(x,y,handler));
}
System.out.println(步骤);
}
//如果蛇走了60步或更多步,那么苹果就会腐烂。
公共无效检查步骤(){
如果(步骤>=80){
handler.getWorld().apple.setIsGood(false);
}
}
公共void渲染(图形g,布尔[][]playeLocation){
//改变蛇的颜色
对于(int i=0;iif (justAte == true && handler.getWorld().apple.isGood == false){
lenght--;
check(lenght);
size--;
}

else if (justAte == true){
lenght++;
check(lenght);
size++;
}

private void check (int l){
    if (lenght != size){
         if (lenght > size)
         {
             removeTail();
         }
         else
         {  
             addTail();
         }
     }
}