上學時新任務從頭開始編寫的貪吃蛇遊戲,並需要使用數組中添加一個新的蛇身體部位時,蛇頭吃的食物,並在一開始的蛇需要有1個頭和2個身體部位..我不知道如何做到這一點..有人在那裏幫助我正確的方向..如何開始?這是我到目前爲止的代碼:需要幫助做一個數組的貪吃蛇遊戲在Greenfoot中
世界:
public class WorldofSnake extends World
{
/**
* Constructor for objects of class WorldofSnake.
*
*/
public WorldofSnake()
{
super(600, 400, 1);
addObject (new Snake(),70 , 50);
addObject (new Food(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
}
}
類蛇:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Snake here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Snake extends Actor
{
private int foodEaten;
public Snake()
{
foodEaten = 0;
}
public void act()
{
checkKeypress();
lookForFood();
}
public void checkKeypress()
{ if(Greenfoot.isKeyDown("left")){
setLocation(getX() - 2,(getY()));
}
if(Greenfoot.isKeyDown("right")){
setLocation(getX() + 2,(getY()));
}
if(Greenfoot.isKeyDown("up")){
setLocation(getX() ,(getY() - 2));
}
if(Greenfoot.isKeyDown("down")){
setLocation(getX() ,(getY() + 2));
}
}
public void lookForFood()
{
Actor a = getOneIntersectingObject(Food.class);
if (a !=null)
{
getWorld().removeObject(a);
getWorld().addObject (new Food(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
foodEaten = foodEaten + 1;
}
}
}
我不確定你的問題在這裏。數組如何工作? –
NEJfråganAR許JAG斯卡GA直到瓦加FÖRATT skapa烯陣列索姆亞達烯NY kroppsdel varje綱分鐘蛇亞特mat..jag AR helt NY直到programmering OCH HAR INTE FATT在DETrättatänketännu –