java - 在 Java 中移动 GObject
问题描述
大家早上好,我是Java新手,因此希望您能帮助我。所以,这就是我面临的问题。顺便说一句,我应该使用 ASM Graphics。
那是我的 Gobject 类
package objects;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import acm.graphics.*;
public class Mario extends GCompound implements ActionListener{
public static double dx;
public static double dy;
public static GImage mario;
private static int x = 40;
private static int y = 350;
private final int min = 350;
private final int gravity = 5;
private final int speed = 5;
private final int max_jump = min - 220;
private int direction;
private int number = 1;
public static boolean standing = true;
public Mario () {
if (standing == true) {
mario = new GImage("Mario_Big_Right_Still.png");
mario.setSize(60, 105);
}
else {
mario = new GImage("Mario_Big_Crouch_Left.png");
mario.setSize(60, 95);
}
mario.setLocation(x + dx, y + dy);
add(mario);
}
public boolean onBottom() {
if (mario.getLocation().getY() >= min ) return true;
return false;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -5;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 5;
}
if (key == KeyEvent.VK_UP) {
dy = -5;
}
if (key == KeyEvent.VK_DOWN) {
standing = false;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 5;
}
if (key == KeyEvent.VK_DOWN) {
standing = true;
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
这是应用程序文件
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import acm.graphics.*;
import acm.program.*;
import objects.Mario;
public class Application extends GraphicsProgram implements ActionListener {
private static final long serialVersionUID = 1L;
public static final int WINDOW_WiDTH = 800;
public static final int WINDOW_HEIGHT = 600;
public static final int REFRESH = 60;
public int dx;
public int dy;
public void run () {
setSize(WINDOW_WiDTH, WINDOW_HEIGHT);
addKeyListeners();
while (true) {
Mario mario = new Mario();
add(mario);
mario.setLocation(mario.getLocation().getX() + dx, mario.getLocation().getY() + dy);
pause(REFRESH);
}
}
}
此外,我还需要能够更改图像。任何建议都非常受欢迎。提前致谢。
解决方案
推荐阅读
- pandas - 在seaborn中绘制控制顺序色调
- android - Tensorflow Lite:糟糕的 GPU 性能
- docusignapi - 生产环境中的Docusign api认证
- python - 即使安装了 tensorflow,keras 也没有运行
- python - 使用 csv.DictReader 时的 datetime.strptime 问题
- python - 如何在蝗虫的每个请求中发送一个新的随机数?
- java - 我们可以用实际文字将网页元素颜色和背景颜色打印为红色或蓝色,而不是 Hex / rgba(2555,0,0,1)
- java - 在 Linux 上寻找 Windows SWT 库的 Java Jar
- angular - 带有 PROD 环境的 Nativescript Angular 构建 APK 不起作用
- python - Python SQLite 数据库结果包含不需要的括号和引号