gpt4 book ai didi

Java游戏: Shooting Bullets

转载 作者:行者123 更新时间:2023-11-30 03:32:40 26 4
gpt4 key购买 nike

我对使用 Java 制作游戏(有点)陌生。我之前创建了一些简单的游戏,例如包收集游戏,但现在我想制作一个自上而下的僵尸射击游戏。我已经有了一个可以移动的球员,但现在我想实现射击。 问题是我不确定如何制作一个新的子弹,根据玩家面对屏幕的哪一部分,从玩家向右/向上/向下/向左射击到屏幕末端。 我已经粘贴了下面的所有代码(4 个类):

package me.mateo226.main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import me.mateo226.entities.Player;
import me.mateo226.guns.Bullet;

public class GamePanel extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private static final int PWIDTH = 720;
private static final int PHEIGHT = 480;
private static Thread game;
private static volatile boolean running = false;
public static volatile boolean gameOver = false;
public static volatile boolean paused = false;
public static Graphics g;
public static Image gImage;
public static long lastLoopTime = System.currentTimeMillis();
public static long delta;
public static volatile boolean upPressed = false;
public static volatile boolean downPressed = false;
public static volatile boolean leftPressed = false;
public static volatile boolean rightPressed = false;
public BufferedImage backgroundImage;
public Player player;
Bullet bullet;

public GamePanel() {

setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
setBackground(Color.white);

setFocusable(true);
requestFocus();
waitForTermination();

}

public void addNotify() {
super.addNotify();
startGame();
}

public void waitForTermination() {
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ESCAPE) {
GamePanel.stopGame();
}
if (keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP) {
upPressed = true;
}
if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) {
downPressed = true;
}
if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT) {
leftPressed = true;
}
if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
rightPressed = true;
}

if (keyCode == KeyEvent.VK_ESCAPE) {
System.exit(0);
}

}

@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP) {
upPressed = false;
}
if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) {
downPressed = false;
}
if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT) {
leftPressed = false;
}
if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
rightPressed = false;
}

}

@Override
public void keyTyped(KeyEvent e) {

}

});

}

@Override
public void run() {
running = true;
while (running) {
delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();

gameUpdate();
gameRender();
checkMovement();
paintpauseScreen();

try {
Thread.sleep(5);
} catch (Exception e) {
System.out.println("The thread couldn't sleep! Error info: "
+ e);
}

}
System.exit(0);

}

private void checkMovement() {
if (!paused && !gameOver) {

}
}

private void paintpauseScreen() {
Graphics g;
try {
g = this.getGraphics();
if ((g != null) && (gImage != null))
g.drawImage(gImage, 0, 0, null);
g.dispose();
} catch (Exception e) {
System.out.println("Graphics context error: " + e);
}
}

private void gameRender() {
if (gImage == null) {
gImage = createImage(PWIDTH, PHEIGHT);
if (gImage == null) {
System.out
.println("image null after creating it??? Please check the code for any errors!");
} else {
g = gImage.getGraphics();
}
}
if (!paused) {
g.setColor(Color.white);
g.fillRect(0, 0, PWIDTH, PHEIGHT);
g.setColor(Color.blue);
}
try {
backgroundImage = ImageIO.read(new File("res\\background.png"));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(backgroundImage, 0, 0, Color.white, null);

if (player != null) {
player.drawPlayer(g);
}
if (bullet != null) {
bullet.drawBullet(g);
}

}

private void gameUpdate() {
if (!paused && !gameOver) {
movePlayer();
if (bullet != null){
bullet.shootBullet(g, "right");
}
}
}

public void startGame() {
if (game == null) {
game = new Thread(this);
if (game == null) {
System.out.println("Couldn't create the thread!");
} else {
System.out.println("Thread created!");
game.start();
}
}
if (g == null) {
g = this.getGraphics();
if (g == null) {
System.out.println("The graphics were not created!");
} else {
System.out.println("The graphics are successfully created!");
}
}

player = new Player(32, 32, "res\\player.png");
bullet = new Bullet("res\\grassTile.png", "right", player.x + 32,
player.y);
running = true;

}

public void movePlayer() {
if (upPressed) {
player.y -= player.moveSpeed * delta;
}
if (downPressed) {
player.y += player.moveSpeed * delta;
}
if (leftPressed) {
player.x -= player.moveSpeed * delta;
}
if (rightPressed) {
player.x += player.moveSpeed * delta;
}

}

public static void stopGame() {
running = false;
}

}

这是我的 GamePanel 类。这是我的主要类(class):

package me.mateo226.main;

import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Main extends JFrame implements WindowListener {
private static final long serialVersionUID = 1L;
private static GamePanel panel;
public static boolean DEBUGGING = false;

public Main(){
super("The Gun Reactor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(this);
Container c = getContentPane();
panel = new GamePanel();
c.add(panel, "Center");
setResizable(false);
pack();
setLocationRelativeTo(null);
if(JOptionPane.showConfirmDialog(null, "Enable debugging?") == 1){
DEBUGGING = true;
} else {
DEBUGGING = false;
}
setVisible(true);

}

@Override
public void windowActivated(WindowEvent arg0) {


}

@Override
public void windowClosed(WindowEvent arg0) {

}

@Override
public void windowClosing(WindowEvent arg0) {

}

@Override
public void windowDeactivated(WindowEvent arg0) {

}

@Override
public void windowDeiconified(WindowEvent arg0) {

}

@Override
public void windowIconified(WindowEvent arg0) {

}

@Override
public void windowOpened(WindowEvent arg0) {

}

public static void main(String args[]){
new Main();
}


}

这是我的 Player 类:

package me.mateo226.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Player {

public int x, y;
public float moveSpeed = 0.1f;
private BufferedImage playerTexture;;

public Player(int x, int y, String texturePath){
this.x = x;
this.y = y;
try {
playerTexture = ImageIO.read(new File(texturePath));
} catch (IOException e){
e.printStackTrace();
}
}

public void drawPlayer(Graphics g){

g.setColor(Color.white);
g.drawImage(playerTexture, x, y, null);

}



}

最后,这是我的子弹类,我真的不知道如何使用,甚至不知道如何正确制作它:

package me.mateo226.guns;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import me.mateo226.main.GamePanel;

public class Bullet {

private int x, y;
private BufferedImage bulletTexture;
private float bulletSpeed = 0.1f;

public Bullet(String bulletTexturePath, String dir, int x, int y) {
this.x = x;
this.y = y;
try {
bulletTexture = ImageIO.read(new File(bulletTexturePath));
} catch (IOException e) {
e.printStackTrace();
}


}

public void drawBullet(Graphics g) {
g.setColor(Color.white);
g.drawImage(bulletTexture, x, y, null);
}

public void shootBullet(Graphics g, String dir) {
switch (dir) {
case "left":
while (x > -32) {
x -= bulletSpeed * GamePanel.delta;
drawBullet(g);
}
break;
case "right":
while (x < 700) {
x += bulletSpeed * GamePanel.delta;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case "up":
while (y > -32) {
y -= bulletSpeed * GamePanel.delta;
drawBullet(g);
}
break;
case "down":
while (y < 480) {
y += bulletSpeed * GamePanel.delta;
drawBullet(g);
}
break;
}
}

}

任何帮助都会很棒!非常感谢!

最佳答案

编辑我刚刚读到你只有四个方向。在这种情况下,您不需要方向 vector 。只需设定一次方向即可。好的,代码示例。

首先是游戏循环。在游戏循环中添加要更新的已发射子弹。每发发射的子弹都会在其方向 vector 上移动。

private void gameUpdate() {
if (!paused && !gameOver) {
movePlayer();
foreach(Bullet bullet : player.getFiredBullets(){
bullet.moveInDirection();
}
}
}

还有你的 Bullet 类:

public class Bullet {
private Direction direction;
private float speed = 1.2f;
private int x;
private int y;

public Bullet(int x, int y){
this.x =x;
this.y=y;
}

public void launchBullet(Direction direction){
this.direction=direction;
}

public void moveInDirection() {
//move the bullet with speed in the set direction. Same as you already have but without the while loop.
}
}

所以玩家应该有一个方法 fire。这将创建带有玩家位置的子弹。子弹的方向与玩家面对的方向相同。并且项目符号被添加到列表中,因此它将在每个游戏循环中更新。

public class Player {
private List<Bullet> firedBullets = new ArrayList<Bullet>();

public void fire(){
Bullet bullet = new Bullet(playerX, playerY);
firedbullets.add(bullet);
bullet.launch(direction); //this should be calculated by which direction the player is facing.
}
}

因此,当子弹发射时,方向就会设定一次。每次游戏更新时,子弹都会按照子弹的速度朝这个方向移动。此逻辑可用于游戏中必须移动的所有内容。例如,如果您想要一颗子弹在半空中改变方向,您只需在半空中改变其方向即可。

关于Java游戏: Shooting Bullets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28656799/

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