gpt4 book ai didi

Java 小程序 : Cant figure out how to display rectangle on keypressed

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

我基本上正在编写这个基本的街机游戏,我需要圆圈来发射看起来像子弹或导弹的小矩形,以便在按下空格键时击中坏人,但我不知道如何实现。

这是我到目前为止的代码:

   import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class main extends Applet implements Runnable, KeyListener {

private Image i;
private Graphics doubleG;
// x and y are used to set (x,y) positions
// dx and dy are the changes in position
private int x = 850;
private int y = 850;
private int x2 = 100;
private int y2 = 100;
private int dx = 50;
private int radius = 30;
private int dx2 = 4;
private int dy2 = 4;
private int x3 = 100;
private int y3 = 200;
private int dx3 = 5;
private int dy3 = 5;
private int x4 = 100;
private int y4 = 300;
private int dx4 = 3;
private int dy4 = 3;

public void init(){
setSize(1920,1080);
setBackground(Color.black);
addKeyListener(this);
}

public void start(){
Thread thread = new Thread(this);
thread.start();

}

public void run() {
while(true){
//Enemy
if(x2 + dx2 > this.getWidth() - radius - 1){
x2 = this.getWidth() - radius - 1;
dx2 = -dx2;
}
if(x2 + dx2 < 0 + radius){
x2 = 0 + radius;
dx2 = -dx2;
}
x2 += dx2;
// Enemy
if(x3 + dx3 > this.getWidth() - radius - 1){
x3 = this.getWidth() - radius -1;
dx3 = -dx3;
}
if(x3 + dx3 < 0 + radius){
x = 0 + radius;
dx3 = -dx3;
}
x3 += dx3;
// Enemy
if(x4 + dx4 > this.getWidth() - radius - 1){
x4= this.getWidth() - radius -1;
dx4 = -dx4;
}
if(x4 + dx4 < 0 + radius){
x4 = 0 + radius;
dx4 = -dx4;
}
x4 += dx4;
// EVERYTHING ABOVE KEEPS ASTEROIDS IN THE SCREEN ALLOWING IT TO BOUNCE OFF WALLS
repaint();
try{
Thread.sleep(17);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
public void stop(){

}

public void update(Graphics g){
// this function stops the flickering problem every time the ball moves by copying the image instead of repainting it
if(i == null){
i = createImage(this.getSize().width, this.getSize().height);
doubleG = i.getGraphics();
}
doubleG.setColor(getBackground());
doubleG.fillRect(0,0,this.getSize().width, this.getSize().height);
doubleG.setColor(getForeground());
paint(doubleG);
g.drawImage(i,0,0,this);
}

public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillOval(x, y, radius*2, radius*2);
g.setColor(Color.RED);
g.fillOval(x2, y2, radius + 10, radius + 10);
g.setColor(Color.RED);
g.fillOval(x3,y3, radius + 10, radius + 10);
g.setColor(Color.RED);
g.fillOval(x4, y4, radius + 10, radius + 10);
}

public void moveRight(){
if (dx-1 > -20){
dx += 1;
}
if(x + dx > this.getWidth() - radius - 1){
x = this.getWidth() - radius - 1;
dx = -dx;
}
x += dx;
}

public void moveLeft(){
if(dx - 1 > -20){
dx -= 1;
}
if(x + dx < 0 + radius){
x = 0 + radius;
dx = -dx;
}
x -= dx;

}

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
moveLeft();
break;
case KeyEvent.VK_RIGHT:
moveRight();
break;
}
}

public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}

}

最佳答案

  1. KeyListener 仅在注册的组件可聚焦并且具有焦点时才会引发 KeyEvent
  2. 您永远不会调用 super.paint,预计会出现一些严重的绘画伪影
  3. 避免覆盖顶级容器(例如Applet)的paint
  4. 考虑使用基于 Swing 的组件而不是 AWT,除了迄今为止更新更多且使用更广泛之外,Swing 组件默认情况下也是双缓冲的。使用 JAppletJPanel 的组合作为主绘图表面,覆盖它的 paintComponent 方法。在这种情况下,还可以考虑在 Thread 上使用 javax.swing.Timer,除非您想尝试在更新之间保持可变的延迟。这也将允许您使用 key bindings API克服与 KeyListener
  5. 相关的焦点问题

关于Java 小程序 : Cant figure out how to display rectangle on keypressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23643679/

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