gpt4 book ai didi

java - Java 游戏中的角色没有响应

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

我一直在关注 Java 游戏教程,但被其中一个困住了,那个教程在这里:http://www.youtube.com/watch?v=RolMRqekV3o&list=PL6E90696571998DC2我遇到的问题是我的角色不响应箭头键。如果你能帮忙那就太好了。我的代码:

package theJavaHubGame;

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

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

public class Main extends JFrame implements Runnable{

int x, y, xDirection = 10, yDirection = 10;
private Image dbImage;
private Graphics dbg;
Image face;

public void run(){
try{
while(true){

move();

Thread.sleep(5);
}
}
catch(Exception e){
System.out.println("Error");

}
}

Font font = new Font("Arial", Font.BOLD | Font.ITALIC, 15);

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

if(x <= 0)
x = 0;
if (x >= 200)
x = 200;
if(y <= 50)
y = 50;
if(y >= 250)
y = 250;
}

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

public void setYDirection(int ydir){
yDirection = ydir;
}
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();

if(keyCode == e.VK_LEFT){
setXDirection(-1);
System.out.println("Key Pressed");
}

if(keyCode == e.VK_RIGHT){
setXDirection(+1);

}

if(keyCode == e.VK_UP){
setYDirection(-1);
}

if(keyCode == e.VK_DOWN){
setYDirection(+1);
}

}

public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();

if(keyCode == e.VK_LEFT){
setXDirection(0);
System.out.println("Key Pressed");
}
if(keyCode == e.VK_RIGHT){
setXDirection(0);
}
if(keyCode == e.VK_UP){
setYDirection(0);
}
if(keyCode == e.VK_DOWN){
setYDirection(0);
}
}
}

public Main(){

//Load Images
ImageIcon i = new ImageIcon("F:/Android/Workspace/TheJavaHubGame/src/theJavaHubGame/Face.gif");
face = i.getImage();

//Game Properties
addKeyListener(new AL());
setTitle("Java Game");
setSize(250,250);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(Color.GREEN);

x=150;
y=150;
}

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.setFont(font);
g.setColor(Color.MAGENTA);
g.drawString("Super-Mega Ball Bouncer II", 50, 50);
g.setColor(Color.red);
g.fillOval(x, y, 15, 15);
g.drawImage(face, x, y, this);
repaint();
}


public static void main(String[] args){
Main jg = new Main();
//Threads
Thread t1 = new Thread();
t1.start();
}
}

谢谢!

最佳答案

这可能是罪魁祸首:

Main jg = new Main();
//Threads
Thread t1 = new Thread();
t1.start();

您没有将 Runnable 传递给 Thread,因此该线程实际上没有执行任何操作。如果没有更新线程,游戏中的任何内容都不会移动。试试这个:

Main jg = new Main();
//Threads
Thread t1 = new Thread(jg);
t1.start();

关于java - Java 游戏中的角色没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18197330/

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