代码拉取完成,页面将自动刷新
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Crab here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Crab extends Actor
{
GreenfootImage image1 = new GreenfootImage("crab.png");
GreenfootImage image2 = new GreenfootImage("crab2.png");
int count = 0;
public Crab() {
setImage(image1);
}
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
move(4);
keyControl();
eat();
switchImage();
stop();
}
public void switchImage() {
if (getImage()==image1) {
setImage(image2);
}else{
setImage(image1);
}
}
public void eat ()
{
if (isTouching(Worm.class)) {
removeTouching(Worm.class);
Greenfoot.playSound("slurp.wav");
count++;
World w = getWorld();
w.showText("吃虫数"+count,500,500);
}
}
public void keyControl()
{
if(Greenfoot.isKeyDown("left")) {
turn(-15);
}
if(Greenfoot.isKeyDown("right")) {
turn(15);
}
}
public void stop() {
if (count==3){
Greenfoot.stop();
World w = getWorld();
GreenfootImage bg = w.getBackground();
Font font = bg.getFont();
font = font.deriveFont(150);
bg.setFont(font);
bg.setColor(Color.GREEN);
bg.drawString("成功",180,280);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。