我正在用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),因此AI永远不会移动。您需要将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);
}