gpt4 book ai didi

java - 导致游戏和计时器停止的碰撞检测

转载 作者:太空宇宙 更新时间:2023-11-04 13:00:16 25 4
gpt4 key购买 nike

因此,对于游戏,我试图拥有一个用户控制的图像,该图像远离CPU控制的图像。当它们发生碰撞时,我希望游戏结束并显示它们存活了多长时间。我似乎无法让图像在碰撞时停止,并且计时器没有显示。

这是我的用户控制图像。

package parthap;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Start {

private int dx;
private int dy;
private int x;
private int y;
private Image image;
protected int width;
protected int height;
protected boolean vis;

public Start() {

initCraft();
}

private void initCraft() {

ImageIcon ii = new ImageIcon("donald.jpg");
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
x = 600;
y = 400;
}

public void move() {
x += dx;
y += dy;
checkOOB();
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public Image getImage() {
return image;
}

private void checkOOB() {
if(x < 0) x = 0;
if(x > 1200) x = 1200;
if(y < 0) y = 0;
if(y > 800) y = 800;
}

public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT) {
dx = -5;
}

if (key == KeyEvent.VK_RIGHT) {
dx = 5;
}

if (key == KeyEvent.VK_UP) {
dy = -5;
}

if (key == KeyEvent.VK_DOWN) {
dy = 5;
}
}

public void keyReleased(KeyEvent e) {

int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT) {
dx = 0;
}

if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}

if (key == KeyEvent.VK_UP) {
dy = 0;
}

if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}

public boolean isVisible() {
return vis;
}

public void setVisible(Boolean visible) {
vis = visible;
}

public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}

}

这是我的人工智能

package parthap;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.ImageIcon;
public class AI_1 {
Random rand = new Random();

// encapsulated data: instance data
private int dx;
private int dy;
private int x;
private int y;
private Image image;
protected int width;
protected int height;
protected boolean vis;

// constructor
public AI_1() {
// initialize all instance data

}
// constructor ... overloaded because signature changed
public AI_1(int xpos, int ypos) {
// initialize all instance data
ImageIcon ii = new ImageIcon("kim (1).jpg");
image = ii.getImage();
x = xpos;
y = ypos;
}

protected void getImageDimensions() {

width = image.getWidth(null);
height = image.getHeight(null);
}

public void move() {
x += dx;
y += dy;
checkOOB();
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public Image getImage() {
return image;
}

private void checkOOB() {
if(x < 0) x = 0;
if(x > 1200) x = 1200;
if(y < 0) y = 0;
if(y > 800) y = 800;
}

/* the number generator for movement
* one parameter max to indicate the maximum number of case
* statements representing the change in x and y, neg. and pos directions*/
public void numbergen(int max, int speed) {

int num = rand.nextInt(max)+1;

switch (num){
case 1:
dx=-speed;
break;
case 2:
dx=speed;
break;
case 3:
dy=-speed;
break;
case 4:
dy=speed;
break;
default:
break;
}
}
public boolean isVisible() {
return vis;
}

public void setVisible(Boolean visible) {
vis = visible;
}

public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}

}

这是我的董事会。

public class Board extends JPanel implements ActionListener {

private Timer timer;
private Start start;
private final int DELAY = 10;
private final int B_WIDTH = 400;
private final int B_HEIGHT = 300;
private AI_1 sprite;
private AI_2 sprite2;
private AI_3 sprite3;
private AI_4 sprite4;
private Image image;
private Image bg;
private boolean ingame;

public Board() {
initBoard();
}

private void initBoard() {

addKeyListener(new TAdapter());
setFocusable(true);

start = new Start();
ImageIcon bgi = new ImageIcon("back.jpg");
bg = bgi.getImage();
sprite = new AI_1(1200, 800);// passing actual parameter..or arguments
sprite2 = new AI_2(0, 0);
sprite3 = new AI_3(1200, 0);
sprite4 = new AI_4(0, 800);
timer = new Timer(DELAY, this);
timer.start();
}


@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

if (ingame) {

doDrawing(g);

} else {

drawGameOver(g);
}

Toolkit.getDefaultToolkit().sync();
}


private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bg, 0, 0, this);
g2d.drawImage(start.getImage(), start.getX(), start.getY(), this);
g2d.drawImage(sprite.getImage(), sprite.getX(), sprite.getY(), this);
g2d.drawImage(sprite2.getImage(), sprite2.getX(), sprite2.getY(), this);
g2d.drawImage(sprite3.getImage(), sprite3.getX(), sprite3.getY(), this);
g2d.drawImage(sprite4.getImage(), sprite4.getX(), sprite4.getY(), this);
}

private void drawGameOver(Graphics g) {

String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics fm = getFontMetrics(small);

g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - fm.stringWidth(msg)) / 2,
B_HEIGHT / 2);
}

@Override
public void actionPerformed(ActionEvent e) {

start.move();
sprite.move();
sprite2.move();
sprite3.move();
sprite4.move();
repaint();
}

public void checkCollisions() {

Rectangle r3 = start.getBounds();
Rectangle r2 = sprite.getBounds();
Rectangle r1 = sprite2.getBounds();
Rectangle r4 = sprite3.getBounds();
Rectangle r5 = sprite4.getBounds();

if (r3.intersects(r2)) {
start.setVisible(false);
sprite.setVisible(false);
ingame = false;
}
if (r3.intersects(r1)) {
start.setVisible(false);
sprite2.setVisible(false);
ingame = false;
}
if (r3.intersects(r4)) {
start.setVisible(false);
sprite3.setVisible(false);
ingame = false;
}
if (r3.intersects(r5)) {
start.setVisible(false);
sprite4.setVisible(false);
ingame = false;
}
}

private class TAdapter extends KeyAdapter {

@Override
public void keyReleased(KeyEvent e) {
start.keyReleased(e);
sprite.numbergen(4, 15); // direction and speed
sprite2.numbergen(4, 15);
sprite3.numbergen(4, 15);
sprite4.numbergen(4, 15);
}

@Override
public void keyPressed(KeyEvent e) {
start.keyPressed(e);
sprite.numbergen(4, 10); // direction and speed
sprite2.numbergen(4, 10);
sprite3.numbergen(4, 10);
sprite4.numbergen(4, 10);
}
}
}

最佳答案

你需要在某个地方调用 checkCollisions() 否则将不会有 checkCollisions

关于java - 导致游戏和计时器停止的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34995661/

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