gpt4 book ai didi

java - 逐渐加速 Sprite

转载 作者:行者123 更新时间:2023-12-01 23:48:43 24 4
gpt4 key购买 nike

我试图让 Sprite 在按下按钮时逐渐加速,而不是仅以恒定速度移动。还设置最大速度限制。我希望你明白我的意思。

timer = new Timer(5, this);
timer.start();

public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(image, x, y, this); //x,y = position
Toolkit.getDefaultToolkit().sync();
g.dispose();
}

private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
}
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
repaint();
}

最佳答案

您的示例代码(最初)有几个问题......

  1. 您正在重写 paint 方法。建议您重写 paintComponent 方法。如果您要重写顶级容器(例如 JFrame)的 paint 方法,那么建议您不要这样做。相反,使用诸如 JPanel 之类的东西作为自定义绘画的基础...
  2. 您正在处理您过去的Graphics上下文。这是非常危险的,因为这会阻止绘制其他任何东西。 Graphics 上下文是共享资源,在此重绘周期中需要更新的所有内容都将使用相同的 Graphics 上下文。
  3. 您正在使用KeyListenerKeyListener 存在焦点问题。通过使用 Key Binding 可以轻松解决此问题。 API。键绑定(bind)也更加灵活,因为它们将物理键与操作分开,使您可以轻松地将操作与不同的键相关联和/或重用底层操作(例如按钮)。

所以。对于你的问题。你需要知道...

  • 当前速度...
  • 最低允许速度...
  • 最大允许速度...

您还需要保持正在更改的对象的当前位置。

这个例子实际上并没有移动“玩家”,而是移动了背景。背景位置由 xDelta 改变,即变化的速度...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestSpeed {

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

public TestSpeed() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

});
}

public class TestPane extends JPanel {

private BufferedImage background;
// The current position of the background
private int xPos = 0;
// The speed/delta that the xPos is changed...
private int xDelta = 0;

public TestPane() {
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (xPos < -(getWidth())) {
xPos = 0;
}
xPos -= xDelta;
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "slower");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "faster");

ActionMap am = getActionMap();
am.put("slower", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setSpeed(-1);
}
});
am.put("faster", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setSpeed(1);
}
});
}

protected void setSpeed(int delta) {
xDelta += delta;
// Check the change in speed to ensure it's within the appropriate range
if (xDelta < 0) {
xDelta = 0;
} else if (xDelta > 9) {
xDelta = 9;
}
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
public void invalidate() {
background = null;
super.invalidate();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int x = xPos;
g.setColor(Color.DARK_GRAY);
while (x < getWidth()) {
g.drawLine(x, 0, x, getHeight());
x += 15;
}

int width = getWidth();
int height = getHeight();
x = (width / 2) - 5;
int y = (height / 2) - 5;
g.setColor(Color.RED);
g.fillOval(x, y, 10, 10);
}
}
}

关于java - 逐渐加速 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16622630/

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