gpt4 book ai didi

java - 使用 JPanel/JFrame 的 2D 射击游戏。以自己的速度在屏幕上绘制图像(子弹)

转载 作者:行者123 更新时间:2023-12-01 23:50:28 27 4
gpt4 key购买 nike

这是我所拥有的:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements ActionListener, KeyListener, ImageObserver {
public Image ship;
public Image enemy;
private int xLoc;
private int yLoc = 180;
private int xLoc2 = 700;
private int yLoc2 = 180;
private int xVel;
private int yVel;
private int xVel_en;
private int xVel_sh = 6;
private ImageObserver observer = null;
private ArrayList<Base> shield = new ArrayList<Base>();
private Image shieldPiece;
private Shot shot;

public GamePanel() {
ship = Toolkit.getDefaultToolkit().createImage("ship3.png");
enemy = Toolkit.getDefaultToolkit().createImage("alien-ship-1.png");
shieldPiece = Toolkit.getDefaultToolkit().createImage("block2.png");

for(int x = 500; x < 680; x = x + 18)
for(int y = 60; y < 370; y = y + 18) {
Base temp = new Base(x,y);
shield.add(temp);
}
}

public void paint(Graphics g){
super.paint(g);
g.drawImage(ship, xLoc, yLoc, null);
g.drawImage(enemy, xLoc2, yLoc2, null);

for(Base b: shield)
g.drawImage(shieldPiece, b.getShieldX(), b.getShieldY(), null);
}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

move();
setBackground(Color.BLACK);
repaint();

}

private void move() {

int endOfWindow = getWidth();
int endOfWindow1 = getHeight();
if(xLoc>=(endOfWindow - ship.getWidth(null))|| xLoc<0)
xVel = -xVel;
xLoc +=xVel;
if(xLoc>=(endOfWindow - ship.getWidth(null)))
xVel = -xVel;
xLoc +=xVel;
if(yLoc>=(endOfWindow1 - ship.getHeight(null)) || yLoc<0)
yVel = -yVel;
yLoc +=yVel;
if(yLoc>=(endOfWindow1 - ship.getHeight(null)))
yVel = -yVel;
yLoc +=yVel;

}

@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub

if(arg0.getKeyCode() == KeyEvent.VK_D)
xVel = 3;
if(arg0.getKeyCode() == KeyEvent.VK_A)
xVel = -3;
if(arg0.getKeyCode() == KeyEvent.VK_W)
yVel = -3;
if(arg0.getKeyCode() == KeyEvent.VK_S)
yVel = 3;

if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
xVel = 3;
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
xVel = -3;
if(arg0.getKeyCode() == KeyEvent.VK_UP)
yVel = -3;
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
yVel = 3;

if(arg0.getKeyCode() == KeyEvent.VK_SPACE) {
Shot shoot = new Shot(xVel_sh, xLoc, yLoc);
shoot.paint();

}


}

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

if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_A)
xVel = 0;
if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_S)
yVel = 0;

if(arg0.getKeyCode() == KeyEvent.VK_RIGHT || arg0.getKeyCode() == KeyEvent.VK_LEFT)
xVel = 0;
if(arg0.getKeyCode() == KeyEvent.VK_UP || arg0.getKeyCode() == KeyEvent.VK_DOWN)
yVel = 0;

}

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

}
}

和 Shot 类:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;


public class Shot{
private int xVel_sh;
private int xCoord;
private int yCoord;
private Image shot;
private Graphics g;

public Shot(int vel, int xLoc, int yLoc) {
vel = xVel_sh;
xCoord = xLoc;
yCoord = yLoc;
shot = Toolkit.getDefaultToolkit().createImage("shot4.png");
}

public void setShotVel(int shotVel) {
xVel_sh = shotVel;
}

public void paint() {
g.drawImage(shot, xCoord, yCoord, null);
}
}

我已将其设置为当我点击空格时,它应该创建一个镜头对象,然后调用 Shot 类中的 Paint() 方法。射击对象将在 xLoc 和 yLoc(来自 GamePanel 类 - 这是“船”或玩家的位置)处创建。

但是,我遇到了很多错误,从当我点击空格时出现 NullPointerException 开始。游戏不会崩溃,只是不断出现空指针异常。”随后还出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Shot.paint(Shot.java:25)
at GamePanel.keyPressed(GamePanel.java:104)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

不太确定为什么它没有被绘制......任何人都可以帮忙吗?另外,我需要最终实现对象碰撞(此处未显示屏蔽类)...所以如果有人对此有任何指导方向吗? :P

最佳答案

g 从未在 Shot 中初始化,因此当您调用 g.drawImage 时,它会抛出 NullPointerException

关于java - 使用 JPanel/JFrame 的 2D 射击游戏。以自己的速度在屏幕上绘制图像(子弹),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16379164/

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