gpt4 book ai didi

java - 如何让角色跳跃,无论是在物体上还是普通跳跃?

转载 作者:行者123 更新时间:2023-12-02 00:51:23 24 4
gpt4 key购买 nike

对于 Java 编程,我是一个初学者,我在学校有一个项目,我将在其中创建一个类似于 Icy Tower 的游戏。 。我的问题是,我该如何写才能让角色站在地面上并能够跳到物体上?

这是我到目前为止的代码:

第一部分

package Sprites;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class jumper {

private String jump = "oka.png";

private int dx;
private int dy;
private int x;
private int y;
private Image image;

public jumper() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(jump));
image = ii.getImage();
x = 50;
y = 100;
}

public void move() {
x += dx;
y += dy;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public Image getImage() {
return image;
}



public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();



if (key == KeyEvent.VK_LEFT) {
dx = -5;

ImageIcon ii = new ImageIcon(this.getClass().getResource("oki.png"));
image = ii.getImage();
}

if (key == KeyEvent.VK_RIGHT){
dx = 5;

ImageIcon ii = new ImageIcon(this.getClass().getResource("oka.png"));
image = ii.getImage();
}

if (key == KeyEvent.VK_SPACE) {
dy = -5;
}



if (key == KeyEvent.VK_DOWN) {
dy = 5;
}


}

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_SPACE) {
dy = 0;
}



if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}

}

第二部分

package Sprites;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;

public class board extends JPanel implements ActionListener {

private Timer klocka;
private jumper jumper;

public board() {

addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.WHITE);
setDoubleBuffered(true);

jumper = new jumper();

klocka = new Timer(5, this);
klocka.start();
}

public void paint(Graphics g) {
super.paint(g);

Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(jumper.getImage(), jumper.getX(), jumper.getY(), this);

Toolkit.getDefaultToolkit().sync();
g.dispose();
}

public void actionPerformed(ActionEvent e) {
jumper.move();
repaint();
}

private class TAdapter extends KeyAdapter {

public void keyReleased(KeyEvent e) {
jumper.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
jumper.keyPressed(e);
}
}
}

第三部分

package Sprites;

import javax.swing.JFrame;

public class RType extends JFrame {
public RType() {

add(new board());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setTitle("R - type");
setResizable(false);
setVisible(true);
}

public static void main(String[] args) {
new RType();

}

}

我真的很感谢我能得到的所有帮助!

最佳答案

This might help.这是一组旨在帮助人们制作基于图 block 的游戏的教程。包括横向平台游戏。请参阅http://www.tonypa.pri.ee/tbw/tut07.html 。顺便说一句,您在角色移动方法中进行了相当密集的图像加载工作。不要那样做。首先缓存图像。另外,您可以对 Canvas 进行双缓冲以使其平滑。查看代码here了解详情。

关于java - 如何让角色跳跃,无论是在物体上还是普通跳跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2943315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com