greenfoot主要api

World 类常用方法一览表

|-------------------------------------------------------|-------------------------------------------------------------------------------------------------|
| 方法名 | 作用说明 |
| World(int width, int height, int cellSize) | ​​构造方法​​,用于创建一个新的 World 对象。参数分别是世界的宽度(格子数)、高度(格子数)和每个格子的像素大小。通常在 Greenfoot 提供的子类构造器中调用父类构造器时使用。 |
| void addObject(Actor object, int x, int y) | 在世界的指定位置 (x, y) 添加一个 Actor 对象。x 和 y 是格子坐标,不是像素坐标。 |
| void removeObject(Actor object) | 从世界中移除指定的 Actor 对象。 |
| Actor getObjectsAt(int x, int y, Class cls) | 返回位于格子坐标 (x, y) 的所有属于指定类(cls)的 Actor 对象,返回一个 List。常用于检测某个位置是否存在某类对象。 |
| List<Actor> getObjects(Class cls) | 返回世界中​​所有​​属于指定类(cls)的 Actor 对象,返回一个 List。比如获取世界中所有的敌人、子弹等。 |
| int getWidth() | 返回世界在格子单位的宽度(不是像素)。 |
| int getHeight() | 返回世界在格子单位的高度(不是像素)。 |
| int getCellSize() | 返回每个格子的像素大小(比如默认是 1 像素 = 1 格子,通常为 16x16 或 32x32 像素)。 |
| void showText(String text, int x, int y) | 在指定的格子坐标 (x, y) 显示一段文字,用于调试或提示信息。文字显示在格子中心位置。 |
| void removeObjectAt(int x, int y) | 移除位于格子坐标 (x, y) 的​​任意一个对象​​(不指定类型,移除其中一个,慎用)。 |
| Actor getOneObjectAtOffset(int dx, int dy, Class cls) | 获取当前对象​​偏移位置 (dx, dy)​​ 上的第一个属于类 cls 的对象。常用于检测相邻格是否有某类对象。 |
| void setPaintOrder(Class<?>... classes) | 设置绘制顺序(即哪些类的对象先画,哪些后画)。参数是多个 Class 类型,按顺序优先绘制。 |
| void setActOrder(Class<?>... classes) | 设置 act()方法的执行顺序,即哪些类的对象先执行 act(),哪些后执行。 |
| void pause() | 暂停世界的运行(停止调用所有 Actor 的 act() 方法)。 |
| void resume() | 恢复世界的运行(重新开始调用 act() 方法)。 |
| boolean isPaused() | 判断世界当前是否处于暂停状态。 |
| void delay(int milliseconds) | 让世界暂停运行指定的毫秒数(一般很少直接在 World 中调用)。 |

Actor 类的主要方法一览表

以下是 Actor 类中最常用和重要的方法,包括方法名、作用说明及简单用法提示:

|-----------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
| 方法名 | 作用说明 |
| void act() | ​​最重要的方法!​​ 每一帧(或每次世界更新时)被自动调用。你通常需要​​重写此方法​​,编写对象每一帧要执行的逻辑,比如移动、检测碰撞等。 |
| void setLocation(int x, int y) | 设置该 Actor 在世界中的位置(格子坐标 x, y)。 |
| int getX() | 获取当前 Actor 所在的 X 坐标(格子单位)。 |
| int getY() | 获取当前 Actor 所在的 Y 坐标(格子单位)。 |
| void move(int steps) | 让 Actor 向当前朝向方向移动指定的格子数(steps)。比如 move(1)表示向前走一格。 |
| void turn(int degrees) | 让 Actor 旋转指定的角度(单位是度)。比如 turn(90)是向右转 90 度,turn(-90)或 turn(270)是向左转 90 度。 |
| void turnRight() | 向右转 90 度(等同于 turn(90))。 |
| void turnLeft() | 向左转 90 度(等同于 turn(-90)或 turn(270))。 |
| void setRotation(int degrees) | 直接设置 Actor 的朝向角度(0 表示向上/北,90 表示向右/东,180 表示向下/南,270 表示向左/西)。 |
| int getRotation() | 获取当前 Actor 的朝向角度。 |
| void setImage(Image image) | 设置该 Actor 显示的图像(Image 对象)。 |
| void setImage(String filename) | 直接通过图片文件名设置图像(图片需放在项目的 "images" 文件夹中)。比如 setImage("player.png")。 |
| Image getImage() | 获取当前 Actor 使用的 Image 对象。 |
| World getWorld() | 获取当前 Actor 所在的 World 对象,用于添加其他对象、获取世界信息等。 |
| Actor getOneObjectAtOffset(int dx, int dy, Class cls) | 检测当前 Actor 偏移 (dx, dy) 格子位置的某个类(cls)的对象,常用于检测相邻格是否有敌人、道具等。 |
| List<Actor> getIntersectingObjects(Class cls) | 返回与当前 Actor ​​发生重叠(相交)​​ 的所有指定类(cls)的 Actor 列表,常用于碰撞检测。 |
| boolean isTouching(Class cls) | 判断当前 Actor 是否与某个类(cls)的对象​​接触/重叠​​,返回 true/false,是 getIntersectingObjects(cls).size() > 0的简化方法。 |
| void removeTouching(Class cls) | 移除所有与当前 Actor 接触的指定类(cls)的对象。 |
| void removeFromWorld()或 remove()(已弃用,推荐使用 World 的 removeObject) | ​​从世界中移除自己​​(不推荐直接使用,建议通过 getWorld().removeObject(this)实现)。 |

Greenfoot 类的主要方法一览表

下面是 Greenfoot 类中最常用的一些静态方法,包括方法名、作用说明和简单用法提示:

|------------------------------------------------------------------|----------------|---------------------------------------------------------------------------------------|
| 方法名 | 返回类型 | 作用说明 |
| static boolean isKeyDown(String key) | boolean | 检测某个键盘按键当前是否被按下。例如 isKeyDown("left")检测左方向键是否被按住。 |
| static String getKeyName(int keyCode) | String | 根据键盘的键码(keyCode)返回对应的键名,较少使用。 |
| static int getMouseInfo()相关方法: • static MouseInfo getMouseInfo() | MouseInfo | 获取当前鼠标的信息(比如位置、是否点击等)。返回一个 MouseInfo对象,你可以从中获取鼠标坐标、点击的 Actor 等。 |
| static void delay(int milliseconds) | void | 让程序暂停运行指定的毫秒数(一般很少在 Greenfoot 中直接使用,因为会影响整个世界运行)。 |
| static World getWorld() | World | ​​获取当前世界的引用​​(在某些特殊情况下使用,一般很少直接调用,因为通常你已经在 World 或 Actor 中了)。 |
| static void setSpeed(int speed) | void | 设置世界的运行速度,范围通常是 1(慢)~ 10(快),默认可能是 5 或 6。数值越大,act()方法调用越频繁,动画越快。 |
| static int getSpeed() | int | 获取当前世界设置的运行速度值。 |
| static void showText(String text, int x, int y) | void | 在世界中的指定格子坐标 (x, y) 显示一段文本,用于调试或提示信息。 |
| static void removeObject(Actor actor) | void | 从世界中移除指定的 Actor 对象。(一般更常用的是在 World 类里调用,或者让 Actor 自己调用 getWorld().removeObject(this)) |
| static void pause() | void | 暂停整个世界的运行(所有 Actor 的 act() 方法都停止执行)。 |
| static void resume() | void | 恢复世界的运行(继续调用所有 Actor 的 act() 方法)。 |
| static boolean isPaused() | boolean | 判断当前世界是否处于暂停状态。 |
| static GreenfootImage getBackground()(不常用) | GreenfootImage | 获取当前世界的背景图像(一般很少直接调用)。 |
| static void setBackground(GreenfootImage image)(不常用) | void | 设置当前世界的背景图像(一般通过 World 的构造方法或方法设置)。 |

MouseInfo 类的主要方法

以下是 MouseInfo类的常用方法及其功能说明:

|------------------|-------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 方法名 | 返回类型 | 功能说明 |
| int getX() | int | 获取当前鼠标指针所在的 ​​X 坐标(格子单位)​​ |
| int getY() | int | 获取当前鼠标指针所在的 ​​Y 坐标(格子单位)​​ |
| Actor getActor() | Actor | 获取当前鼠标​​点击位置下的 Actor 对象​​(如果有的话,比如你点击了一个角色)。如果没有点击对象或在空白处,返回 null。 |
| World getWorld() | World | 获取当前鼠标所在的世界(World)对象 |
| int getButton() | int | 获取当前鼠标按下的​​按钮类型​​,用于判断是左键、右键还是其他。返回的是 Java 标准 AWT 中的鼠标事件常量,常见值: • MouseEvent.BUTTON1(通常是鼠标左键,值为 1) • MouseEvent.BUTTON2(中键,很少用) • MouseEvent.BUTTON3(右键,值为 3) |

GreenfootImage 类 --- 主要方法一览表

|---------------------------------------------------|----------------|----------------------------------------------------|
| 方法名 | 返回类型 | 作用说明 |
| ​​GreenfootImage()​​ | GreenfootImage | 构造方法:创建一个 ​​1×1 像素的透明图像​​。 |
| ​​GreenfootImage(int width, int height)​​ | GreenfootImage | 构造方法:创建一个指定宽度与高度的​​空白图像​​(默认白色或透明背景)。 |
| ​​GreenfootImage(String filename)​​ | GreenfootImage | 构造方法:从项目的 images文件夹加载指定名称的图片文件(如 "player.png")。 |
| ​​setImage(GreenfootImage image)​​ | void | (在 Actor类中使用)设置当前 Actor 显示的图像为指定的 GreenfootImage。 |
| ​​getImage()​​ | GreenfootImage | (在 Actor类中使用)获取当前 Actor 所使用的 GreenfootImage对象。 |
| ​​setBackground(String filename)​​ | void | (在 World类中使用)设置 World 的背景图片(通过文件名,图片在 images文件夹中)。 |
| ​​setBackground(GreenfootImage image)​​ | void | (在 World类中使用)设置 World 的背景为指定的 GreenfootImage。 |
| ​​setColor(Color color)​​ | void | 设置当前绘图颜色,用于后续绘制操作(如画矩形、文字等)。 |
| ​​drawString(String text, int x, int y)​​ | void | 在图像上绘制一串文字,坐标 (x, y) 是文字的​​左下角位置​​。 |
| ​​setFont(java.awt.Font font)​​ | void | 设置绘制文字时使用的字体。 |
| ​​fillRect(int x, int y, int width, int height)​​ | void | 绘制一个指定位置和大小的​​实心矩形​​。 |
| ​​drawRect(int x, int y, int width, int height)​​ | void | 绘制一个指定位置和大小的​​矩形边框​​。 |
| ​​fillOval(int x, int y, int width, int height)​​ | void | 绘制一个指定位置和大小的​​实心椭圆/圆​​。 |
| ​​drawOval(int x, int y, int width, int height)​​ | void | 绘制一个指定位置和大小的​​椭圆/圆边框​​。 |
| ​​drawImage(GreenfootImage image, int x, int y)​​ | void | 在当前图像上的 (x, y) 位置绘制另一张图片。 |
| ​​clear()​​ | void | 清空图像内容(通常变成透明或默认背景色)。 |
| ​​scale(int newWidth, int newHeight)​​ | void | ​​直接修改当前图像​​,将其缩放到新的宽度和高度(可能会变形)。 |
| ​​scaledCopy(int newWidth, int newHeight)​​ | GreenfootImage | ​​创建并返回一个新的缩放后的图像副本​​,原图不变(推荐使用)。 |
| ​​getWidth()​​ | int | 获取当前图像的宽度(单位:像素)。 |
| ​​getHeight()​​ | int | 获取当前图像的高度(单位:像素)。 |
| ​​setTransparency(int alpha)​​ | void | 设置图像的透明度,范围是 0(完全透明)到 255(完全不透明)。 |

GreenfootSound 类 --- 主要方法一览表

|-------------------------------------|----------------|-----------------------------------------------------------------------------------------------------|
| 方法名 | 返回类型 | 作用说明 |
| ​​GreenfootSound(String filename)​​ | GreenfootSound | ​​构造方法​​:创建一个 GreenfootSound 对象,并加载指定文件名(如 "sound.wav"或 "music.mp3")的声音文件。文件需放在项目的 ​​sounds​​ 文件夹中。 |
| ​​play()​​ | void | 播放声音。如果是音效,从头开始播放一次;如果是音乐,也是从头播放一次(不会循环)。 |
| ​​playLoop()​​ | void | 播放声音并​​循环​​(一般用于背景音乐)。会持续重复播放,直到调用 stop()。 |
| ​​stop()​​ | void | 停止当前声音的播放(无论是单次播放还是循环播放)。 |
| ​​pause()​​ | void | 暂停当前正在播放的声音。可以后续通过 play()恢复播放(从暂停位置继续,但实际行为可能因系统而异)。 |
| ​​setVolume(int volume)​​ | void | 设置音量。参数范围是 0(静音)~ 100(最大音量)。例如:setVolume(50)表示设为 50% 音量。 |
| ​​getVolume()​​ | int | 获取当前音量设置,返回值为 0~100 的整数。 |
| ​​isPlaying()​​ | boolean | 判断该声音当前是否正在播放,返回 true或 false。可用于检测背景音乐是否在播。 |


Color 类 --- 主要方法一览表

|---------------------------------------|-------|--------------------------------------------------------------------------------|
| 方法名 | 返回类型 | 作用说明 |
| ​​Color(int r, int g, int b)​​ | Color | ​​构造方法​​:使用 RGB 值创建一个颜色对象。参数范围是 0~255,分别代表红、绿、蓝分量。例如:new Color(255, 0, 0)是红色。 |
| ​​Color(int r, int g, int b, int a)​​ | Color | 构造方法:使用 RGBA 值创建颜色,a 表示透明度(Alpha),范围也是 0~255(0 完全透明,255 完全不透明)。 |
| ​​Color(int rgb)​​ | Color | 构造方法:通过一个整数(打包的 RGB 值,通常是十六进制形式)创建颜色。例如 0xFF0000表示红色。一般较少直接使用。 |
| ​​static Color red​​ | Color | ​​预定义颜色常量​​:红色,等同于 new Color(255, 0, 0)。 |
| ​​static Color green​​ | Color | 预定义颜色:绿色,等同于 new Color(0, 255, 0)。 |
| ​​static Color blue​​ | Color | 预定义颜色:蓝色,等同于 new Color(0, 0, 255)。 |
| ​​static Color yellow​​ | Color | 黄色:new Color(255, 255, 0) |
| ​​static Color black​​ | Color | 黑色:new Color(0, 0, 0) |
| ​​static Color white​​ | Color | 白色:new Color(255, 255, 255) |
| ​​static Color gray/ grey​​ | Color | 灰色:new Color(128, 128, 128) |
| ​​static Color lightGray​​ | Color | 浅灰色 |
| ​​static Color darkGray​​ | Color | 深灰色 |
| ​​static Color cyan​​ | Color | 青色:new Color(0, 255, 255) |
| ​​static Color magenta​​ | Color | 洋红/品红:new Color(255, 0, 255) |
| ​​static Color orange​​ | Color | 橙色:new Color(255, 165, 0) |
| ​​int getRed()​​ | int | 获取该颜色的 ​​红色分量值​​(范围 0~255)。 |
| ​​int getGreen()​​ | int | 获取该颜色的 ​​绿色分量值​​(0~255)。 |
| ​​int getBlue()​​ | int | 获取该颜色的 ​​蓝色分量值​​(0~255)。 |
| ​​int getAlpha()​​ | int | 获取该颜色的 ​​透明度分量值​​(0~255,仅当使用带 Alpha 的构造方法时有效)。 |
| ​​int getRGB()​​ | int | 获取该颜色的 ​​打包 RGB 值(包含 Alpha,如果适用)​​,是一个整数,可用于构造其他 Color 对象。 |

常用预定义颜色常量速查表

|------------------------|----|-----------------|
| 颜色常量 | 说明 | 等价 RGB 值 |
| Color.red | 红色 | (255, 0, 0) |
| Color.green | 绿色 | (0, 255, 0) |
| Color.blue | 蓝色 | (0, 0, 255) |
| Color.yellow | 黄色 | (255, 255, 0) |
| Color.black | 黑色 | (0, 0, 0) |
| Color.white | 白色 | (255, 255, 255) |
| Color.gray或 Color.grey | 灰色 | (128, 128, 128) |
| Color.lightGray | 浅灰 | (192, 192, 192) |
| Color.darkGray | 深灰 | (64, 64, 64) |
| Color.cyan | 青色 | (0, 255, 255) |
| Color.magenta | 洋红 | (255, 0, 255) |
| Color.orange | 橙色 | (255, 165, 0) |

Font 类 --- 主要方法总结表

|--------------------------------------------|---------|---------------------------------|
| 方法名 | 返回类型 | 功能简述 |
| ​​Font(String name, int style, int size)​​ | Font | 构造方法:创建字体对象,指定字体名称、样式和大小 |
| ​​String getName()​​ | String | 获取字体名称,如 "Arial" |
| ​​int getSize()​​ | int | 获取字体大小(单位:磅,如 16、20) |
| ​​int getStyle()​​ | int | 获取字体样式(如 Font.BOLD、Font.ITALIC) |
| ​​boolean isBold()​​ | boolean | 判断是否为粗体 |
| ​​boolean isItalic()​​ | boolean | 判断是否为斜体 |
| ​​boolean isPlain()​​ | boolean | 判断是否为普通字体 |
| ​​Font deriveFont(int style)​​ | Font | 返回新字体,仅修改样式 |
| ​​Font deriveFont(float size)​​ | Font | 返回新字体,仅修改大小 |
| ​​Font deriveFont(int style, float size)​​ | Font | 返回新字体,同时修改样式与大小 |

常用字体样式常量

|----------------------------|---|------------|
| 常量 | 值 | 含义 |
| Font.PLAIN | 0 | 普通(无加粗/斜体) |
| Font.BOLD | 1 | 加粗 |
| Font.ITALIC | 2 | 斜体 |
| 组合:Font.BOLD + Font.ITALIC | 3 | 加粗并斜体 |