gpt4 book ai didi

Java - 按键时更改图像

转载 作者:行者123 更新时间:2023-12-01 15:06:18 25 4
gpt4 key购买 nike

Java新手来了!

我正在开发一款小游戏,它只需在屏幕上移动即可攻击我正在创建的小 AI 矩形。

我有两个 GIF 文件 pikachu.gif(是的,它是一个口袋妖怪游戏)和 pikready.gif。

我已经实现了 KeyListener 和所有内容,并且可以使用箭头键让 Pikachu.gif 在框架中移动。

我想要的是,当我按下箭头键时将图像更改为 pikready.gif,我在网上查了一下,似乎找不到答案。

代码如下,任何注释掉的内容都是我尝试过的不同内容。

正如我所说,我是一名 Java 新手,所以对我要宽容一些!

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class PikachuAttack extends JFrame implements Runnable{

Graphics dbg;
Image dbImage;
Image Pik1;
static ImageIcon active;

int x, y, xDirection, yDirection;

public void run(){
try{
while(true){
move();
Thread.sleep(10);
}
}catch(Exception e){
System.out.println("Uh-oh, something went wrong!.");
}
}

private void move() {
x += xDirection;
y += yDirection;

}

public void setXDirection(int xdir) {
xDirection = xdir;
}

public void setYDirection(int ydir) {
yDirection = ydir;
}


// KEY COMMANDS //
public class AL extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();

if(keyCode == e.VK_LEFT) {
setXDirection(-1);
}
if(keyCode == e.VK_RIGHT) {
setXDirection(+1);
}
if(keyCode == e.VK_UP) {
setYDirection(-1);
}
if(keyCode == e.VK_DOWN) {
setYDirection(+1);
}
/*(if(keyCode == e.VK_LEFT | keyCode == e.VK_RIGHT | keyCode == e.VK_UP | keyCode == e.VK_DOWN){
active = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikready.gif");
}else{
active = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikachu.gif");
} */

}

@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode== e.VK_LEFT){
setXDirection(0);
}
if(keyCode== e.VK_RIGHT){
setXDirection(0);
}
if(keyCode== e.VK_UP){
setYDirection(0);
}
if(keyCode== e.VK_DOWN){
setYDirection(0);
}

}

@Override
public void keyTyped(KeyEvent e) {


}

}

// CONSTRUCTOR //
public PikachuAttack(){

//Image Import
ImageIcon still = new ImageIcon("C:\\Java\\GIFS\\Pikachu.gif");
Pik1 = still.getImage();
//ImageIcon ready = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikready.gif");
//Pik2 = ready.getImage(); */
// Pik1 = active.getImage();

//JFrame properties
addKeyListener(new AL());
setTitle("Pikachu Attack");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);

x = 15;
y = 15;
}



public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);

}

public void paintComponent(Graphics g){
g.setColor(Color.red);
g.fillRect(200, 190, 12, 20);
g.drawImage(Pik1, x, y, this);
g.setColor(Color.red);



repaint();
}


public static void main(String[] args) {
PikachuAttack game = new PikachuAttack();
Thread t1 = new Thread(game);
t1.start();
}

}

最佳答案

为什么不创建对当前Image的新引用,说currentImage并按向下键执行以下操作:

if (keyCode == e.VK_DOWN) {
setYDirection(+1);
currentImage = Pik2; // the ready image
}

然后在paintComponent中,您可以绘制当前图像:

g.drawImage(currentImage, x, y, this);

关于Java - 按键时更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902250/

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