gpt4 book ai didi

Java-Pong AI 不动

转载 作者:行者123 更新时间:2023-12-02 04:25:52 25 4
gpt4 key购买 nike

我正在用java制作乒乓球,但敌人的AI没有动。它应该向球移动的方向移动。请问有人可以帮助我吗?

主类:

import javax.swing.*;

public class Main extends JFrame{
final static int WW = 800;
final static int WH = 600;
public Main(){
setSize(WW,WH);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Pong");
add(new GamePanel());
setVisible(true);
}
public static void main(String[] args){
new Main();
}
}

GamePanel 类:

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

public class GamePanel extends JPanel implements ActionListener,KeyListener{
Player player = new Player();
Computer computer = new Computer();
Ball ball = new Ball();

public GamePanel(){
Timer time = new Timer(20, this);
time.start();
this.addKeyListener(this);
this.setFocusable(true);
}

private void update(){
player.update();
ball.update();
ball.Collision(player);
computer.update();
ball.Collision(computer);
}

public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, Main.WW, Main.WH);
player.paint(g);
ball.paint(g);
computer.paint(g);
}
public void actionPerformed(ActionEvent e){
update();
repaint();
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP){
player.setyv(-10);
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN){
player.setyv(10);
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN)
player.setyv(0);
}
public void keyTyped(KeyEvent e){

}

}

玩家等级:

import java.awt.*;

public class Player {
private int y = 300;
private int w = 20;
private int h = 120;
private int yv = 0;
private int x = 700;

public Player(){
}

public void update(){
y = y + yv;
}

public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(x,y,w,h);
}

public void setyv(int speed){
yv = speed;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public int getW(){
return this.w;
}
public int getH(){
return this.h;
}
}

球类:

import java.awt.*;

public class Ball {
private int x = 400;
private int y = 300;
private int xv = -10;
private int yv = 10;

public void update(){
x = x += xv;
y = y += yv;
if(x < 10){
xv = 10;
}
if(x > 770){
xv = -10;
}
if(y < 10){
yv = 5;
}
if(y > 550){
yv = -5;
}
}
public int getY(){
return this.y;
}
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillOval(x, y, 20, 20);
}
public void Collision(Player player){
if(this.x > (player.getX()-10)){
if(this.y > player.getY() && (this.y < player.getY() + player.getH())){
xv = -10;
}
}
}
public void Collision(Computer cpu){
if(this.x < (cpu.getX()+10)){
if(this.y > cpu.getY() && (this.y < cpu.getY() + cpu.getH())){
xv = 10;
}
}
}
}

计算机类:

import java.awt.*;

public class Computer {
private int y = 300;
private int w = 20;
private int h = 120;
private int yv = 0;
private int x = 100;
Ball ballpos = new Ball();

public Computer(){
}

public void update(){
if(ballpos.getY() < this.y){
yv = -1;
}
else if(ballpos.getY() > this.y){
yv = 1;
}
y = y + yv;
}

public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(x,y,w,h);
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public int getW(){
return this.w;
}
public int getH(){
return this.h;
}
}

如果代码太多,我很抱歉,但如果只发送AI类,我猜你看不到问题出在哪里。请帮助我,我不知道为什么它不起作用。抱歉我的英语不好。

最佳答案

你的计算机类创建了一个不同的球(ballpos),它永远不会更新,因此人工智能永远不会移动。您需要将 Ball 对象从 GamePanel 类传递到 Computers 更新方法:

计算机:

public void update(Ball ball){
if(ball.getY() < this.y){
yv = -1;
}
else if(ball.getY() > this.y){
yv = 1;
}
y = y + yv;
}

游戏面板:

private void update(){
player.update();
ball.update();
ball.Collision(player);
computer.update(ball);
ball.Collision(computer);
}

关于Java-Pong AI 不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190976/

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