gpt4 book ai didi

java - AffineTransform 不会在按键时移动图像

转载 作者:行者123 更新时间:2023-12-01 14:37:02 25 4
gpt4 key购买 nike

我在使用图像而不是 vector 图形开发我的第一个 Java 游戏时遇到了一些麻烦。目前,我正在尝试使用 AffineTransform 对象在按键时移动图像。只是它不会动。我究竟做错了什么?我还没有尝试过使用线程,但我不知道如何做到这一点。如果您认为这有帮助,请将其包含在您的答案中。

这是我的代码:

import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.applet.*;
public class DisplayImage extends Applet implements KeyListener{
AffineTransform identity = new AffineTransform();
boolean left = false;
boolean right = false;
public URL getURL(String filename)
{
URL url = null;
try
{
url = this.getClass().getResource(filename);
}
catch(Exception e){e.printStackTrace();}
return url;
}
private Image image;
public void init()
{
image = getImage(getURL("spaceship.png"));
addKeyListener(this);
}
public void update(Graphics g){paint(g);}
public void paint(Graphics g)
{
AffineTransform trans = new AffineTransform();
trans.setTransform(identity);

Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0,0,getSize().width,getSize().height);

if(left==true)
{
trans.translate(-10,0);
left = false;
repaint();
}
if(right==true)
{
trans.translate(10,0);
right = false;
repaint();
}
g2d.drawImage(image,0,0,this);
}
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
switch(keycode)
{
case KeyEvent.VK_RIGHT:
right = true;
repaint();
case KeyEvent.VK_LEFT:
left = true;
repaint();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}

}

有人可以评论或回答吗?我感觉有一个“风滚草”徽章正朝我走来。

最佳答案

总的来说,您从书中学到的方法对于游戏来说远非理想,但无论如何我还是修改了代码。

import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.applet.*;
public class DisplayImage extends Applet implements KeyListener{

int coordX=0;//ship x axis position
int coordY=0;//ship y axis position
/*
0-------------------------x
0|
|
|
|
|
|
y
*/
public URL getURL(String filename)
{
URL url = null;
try
{
url = this.getClass().getResource(filename);
}
catch(Exception e){e.printStackTrace();}
return url;
}
private Image image;
public void init()
{
image = getImage(getURL("spaceship.png"));
addKeyListener(this);
}
public void update(){repaint();}//let update control the calls to repaint
public void paint(Graphics g)
{

Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0,0,getSize().width,getSize().height);

g2d.drawImage(image,coordX,coordY,this);//paint the spaceship to the updated coordinates
}
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
//37=left arrow. 39=right arrow. 38=up & 40=down
if(keycode==37){coordX-=1;}else if(keycode==39){coordX+=1;}
if(keycode==38){coordY-=1;}else if(keycode==40){coordY+=1;}

update();//request repaint when logic has updated
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}

}

干杯

编辑:: 这是您想要研究的基本概念。

private void mainGameLoop(){
while(pause==false){
updateLogic();//process movement, detect collisions etc
renderGraphics();//draw a new frame
try{Thread.sleep(25);}catch(InterruptedException e){e.printStackTrace();}//sleep the thread to control the frame rate. (there are more efficient ways to sleep a thread but this is the most simplistic)
}
}

关于java - AffineTransform 不会在按键时移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16394850/

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