我正在尝试制作一个非常简单的游戏,如果您向上/向右/向左/向下按键,向上/向右/向左/向下移动球。我抬头看着不同的地方,这就是我所做的:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game extends JPanel implements KeyListener {
static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
static int width = (int)screenSize.getWidth();
static int height = (int)screenSize.getHeight();
static int x = width/2;
static int y = height/2;
boolean a=true;
boolean b=true;
public void keyTyped(KeyEvent e){
//nothing here
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_UP:
x=x+10;
case KeyEvent.VK_DOWN:
x=x-10;
case KeyEvent.VK_LEFT:
y=y+10;
case KeyEvent.VK_RIGHT :
y=y-10;
}
}
public void keyReleased(KeyEvent e){
//nothing here
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(x, y, 30, 30);
}
public static void main(String[] args)throws InterruptedException {
JFrame frame = new JFrame("Sample");
Game game = new Game();
frame.add(game);
frame.setSize(width,height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.keyPressed(null);
game.repaint();
}
}
}
但是如何“运行”keyPressed程序?我看到一些YouTube视频说“addKeyListener”或“addActionListener”,但这意味着添加一个我不想要的文本字段,文本框或文本区域。毕竟这可能与游戏类似。感谢
看完答案后的编辑版本:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game extends JPanel implements KeyListener {
static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
static int width = (int)screenSize.getWidth();
static int height = (int)screenSize.getHeight();
static int x = width/2;
static int y = height/2;
boolean a=true;
boolean b=true;
static Game game;
public Game(){
addKeyListener(this);
}
public void keyTyped(KeyEvent e){
//nothing here
}
@Override
public void keyReleased(KeyEvent e) {
// nothing here
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_UP:
x=x+100;
case KeyEvent.VK_DOWN:
x=x-100;
case KeyEvent.VK_LEFT:
y=y+100;
case KeyEvent.VK_RIGHT :
y=y-1000;
}
game.repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(x, y, 30, 30);
}
public static void main(String[] args)throws InterruptedException {
JFrame frame = new JFrame("Sample");
game = new Game();
frame.add(game);
frame.setSize(width,height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:3)
在构造函数中,添加
Game()
{
/*your code*/
addKeyListener(this);
}
答案 1 :(得分:0)
您可以在构建期间将keylistener添加到Game对象中。
addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
myKeyEvt(e, "keyPressed");
}
private void myKeyEvt(KeyEvent e, String text)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_KP_UP || key == KeyEvent.VK_UP)
{
//up
}
else if (key == KeyEvent.VK_KP_DOWN || key == KeyEvent.VK_DOWN)
{
//down
}
else if (key == KeyEvent.VK_KP_LEFT || key == KeyEvent.VK_LEFT)
{
//left
}
else if (key == KeyEvent.VK_KP_RIGHT|| key == KeyEvent.VK_RIGHT)
{
//right
}
}
});
此外,您还需要更新while循环,以便有时间暂停和刷新
while (alive)
{
update();
repaint();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}