gpt4 book ai didi

java - 在一个简单的游戏中创建子弹

转载 作者:行者123 更新时间:2023-11-29 05:03:02 25 4
gpt4 key购买 nike

我正在创建一个简单的游戏,其中形状落下,玩家射击它们,但我在每次单击鼠标时都无法创建子弹。我在没有帮助的情况下尝试了各种逻辑,所以我只是将代码放在这里,以便你们看一下并帮助我。

我创建的项目符号不是在每次点击时创建的,只是创建了一个项目符号并且它在每次点击时移动这是错误的......我希望每次点击都创建一个项目符号。

// My main class: mousework2

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;

public class mousework2 extends JFrame
{
public static int Width = 300;
public static int Height = 400;
private JPanel p1;
private Image pixMage,gunMage;

public mousework2()
{
super("shoot-em-up");

this.setSize(Width, Height);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Dimension pos = Toolkit.getDefaultToolkit().getScreenSize();
int x = (pos.width - Width) / 2;
int y = (pos.height - Height) / 2;

this.setLocation(x, y);

p1 = new CreateImage();
this.add(p1);
this.getContentPane();

Thread t = new recMove(this);
t.start();
}

class recMove extends Thread
{
JFrame b;

public recMove(JFrame b)
{
this.b = b;
}

public void run()
{
while (true) {
b.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}

class CreateImage extends JPanel implements MouseListener
{
ArrayList<fallShape> rect = new ArrayList<fallShape>();
int x_pos = mousework.Width / 2;
int y_pos = mousework.Height - 50;
int bx_pos = mousework.Width / 2;
int by_pos = mousework.Height;
int y_speed = -10;
boolean clicked;

public CreateImage()
{
for (int i = 0; i < 10; i++) {
rect.add(new fallShape(15, 15, rect));
}

Toolkit picx = Toolkit.getDefaultToolkit();
gunMage = picx.getImage("gunner.jpg");
gunMage = gunMage.getScaledInstance(200, -1, Image.SCALE_SMOOTH);

Toolkit pic = Toolkit.getDefaultToolkit();
pixMage = pic.getImage("ballfall3.jpg");
pixMage = pixMage.getScaledInstance(200, -1, Image.SCALE_SMOOTH);

addMouseListener(this);
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e)
{
x_pos = e.getX() - 5;
}
});
}

public void mousePressed(MouseEvent e)
{
if (e.getButton() == 1) {
clicked = true;
}
}

public void mouseReleased(MouseEvent e)
{
if (e.getButton() == 1) {
clicked = false;
}
}

public void mouseExited(MouseEvent e)
{
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseClicked(MouseEvent e)
{
}

public void paint(Graphics g)
{
super.paint(g);

g.drawImage(pixMage, 0, 0, Width, Height, null);

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(gunMage,x_pos,y_pos,10,20,null);

if (clicked) {
by_pos += y_speed;
Shape bullet = new Rectangle2D.Float(bx_pos, by_pos, 3, 10);
g2.setColor(Color.BLACK);
g2.fill(bullet);
g2.draw(bullet);
}

g2.setColor(Color.RED);

for (fallShape b : rect) {
b.move();
g2.fill(b);
}
}
}

public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run()
{
new mousework2().setVisible(true);
}
});
}
}

和:

// My falling shapes class: fallShape

import java.awt.geom.*;
import java.util.*;

public class fallShape extends Rectangle2D.Float
{
public int x_speed, y_speed;
public int l, b;
public int height = mousework.Height;
public int width = mousework.Width;
public ArrayList<fallShape> fall;

public fallShape(int breadth, int length, ArrayList<fallShape> fall)
{
super((int) (Math.random() * (mousework.Width - 20) + 1), 0, breadth, length);
this.b = breadth;
this.l = length;
this.x_speed = (int) Math.random() * (10) + 1;
this.y_speed = (int) Math.random() * (10) + 1;
this.fall = fall;
}

public void move()
{
Rectangle2D rec = new Rectangle2D.Float(super.x, super.y, b, l);

for (fallShape f : fall) {
if (f != this && f.intersects(rec)) {
int rxspeed = x_speed;
int ryspeed = y_speed;
x_speed = f.x_speed;
y_speed = f.y_speed;
f.x_speed = rxspeed;
f.y_speed = ryspeed;
}
}

if (super.x < 0) {
super.x =+ super.x;
//super.y =+ super.y;
x_speed = Math.abs(x_speed);
}

if (super.x> mousework.Width - 30) {
super.x =+ super.x;
super.y =+ super.y;
x_speed =- Math.abs(x_speed);
}

if (super.y < 0) {
super.y = 0;
y_speed = Math.abs(y_speed);
}

super.x += x_speed;
super.y += y_speed;
}
}

最佳答案

if(clicked){
by_pos+=y_speed;

此代码仅在鼠标按下时绘制子弹。这是因为您在 mouseReleased 方法中将 clicked 设置为 false:

public void mouseReleased(MouseEvent e){
if(e.getButton()==1)
clicked=false;
}

如果您要删除 mouseReleased 方法的主体,您的子弹将正确移动。


但是,假设您想要的不仅仅是一颗子弹。目前,您的 paint 方法一次只能绘制一颗子弹。要绘制多个项目符号,您需要创建一个项目符号坐标列表,并在您单击时向列表中添加一个新的坐标对。然后,在 paint 方法中,只需更新 for 循环中的每个位置。

ArrayList<Integer> by_poss = new ArrayList<>();

by_poss 是项目符号所有 y 位置的列表。

public void mousePressed(MouseEvent e){
if(e.getButton() == 1)
by_poss.add(mousework.Height);
}

mousePressed 方法以 y 位置的形式向坐标添加一个新的“子弹”。

public void mouseReleased(MouseEvent e){
//do nothing
}

mouseReleased 方法中不需要发生任何事情。

//update the bullets
public void paint(Graphics g){
...
g2.setColor(Color.BLACK);
Shape bullet;
for(int i = 0; i < by_poss.size(); i++){
by_poss.set(i, by_poss.get(i) + y_speed); //move the bullet
bullet = new Rectangle2D.Float(bx_pos, by_poss.get(i), 3, 10);
g2.fill(bullet);
g2.draw(bullet);
}
...
}

paint 方法中的 for 循环使用 by_poss 列表中的 y 位置一个接一个地绘制所有项目符号.

关于java - 在一个简单的游戏中创建子弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31415544/

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