加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Crab.java 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
erido-8 提交于 2021-04-23 21:24 . update Crab.java.
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);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化