加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Lobster 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
erido-8 提交于 2021-04-23 21:26 . add Lobster.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Lobster here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lobster extends Actor
{
/**
* Act - do whatever the Lobster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int count = 0;
public void act()
{
// Add your action code here.
move(4);
turnAtEdge();
int sj = Greenfoot.getRandomNumber(20);
turn(sj-10);
eat();
stop();
randomTurn();
}
public void eat () {
if (isTouching(Crab.class)) {
Actor actor = getOneIntersectingObject(Crab.class);
World world = getWorld();
world.removeObject(actor);
removeTouching(Crab.class);
Greenfoot.playSound("au.wav");
count++;
}
}
public void turnAtEdge() {
if(isAtEdge()) {
turn(47);
}
}
public void stop() {
if (count==1) {
Greenfoot.stop();
World w = getWorld();
GreenfootImage bg = w.getBackground();
Font font = bg.getFont();
font = font.deriveFont(150);
bg.setFont(font);
bg.setColor(Color.RED);
bg.drawString("失败",180,280);
}
}
public void randomTurn() {
if (Greenfoot.getRandomNumber(100)<40){
turn(Greenfoot.getRandomNumber(30)-15);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化