gpt4 book ai didi

java - 在java乒乓球游戏中,如何使桨的一侧成为机器人?

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

我正在制作一个java乒乓球游戏,我想让 Racket 的一侧成为一个机器人,这样玩家就可以与机器人竞争。我已经输入了机器人,但我想对其进行改进。我尝试创建一个线程来每隔 x 秒暂停机器人,但失败了。请帮忙找出解决方案。

我已经对 Racket 和球进行了预编程,包括检查碰撞之类的东西。游戏可以算是完整了。我只想添加一个机器人,这样它将成为 PVE

这是桨代码

import processing.core.PApplet;
import processing.core.PFont;

public class Paddle {
private char upKey, downKey;
private boolean upDown, downDown;
private float x,y,width,height,speed;
private int r,g,b; //colors
private int score;
private PFont scoreFont;

public Paddle(float x, float y, float w, float h, float s, char up,
char down, int r, int g, int b, int score, PFont f) {
scoreFont = f;
this.x = x;
this.y = y;
upKey = up;
downKey = down;
width = w;
height = h;
speed = s;
this.r = r;
this.g = g;
this.b = b;
score = 0;
}

public void draw(PApplet p) {
p.rectMode(PApplet.CENTER);
p.noStroke();
p.fill(r, g, b);
p.rect(x, y, width, height);

p.textFont(scoreFont, 46);
p.fill(255);
p.text(score, PongMain.SCR_W-x-15, 50);
}

public void handlePress(char key) {
if(key == upKey)
upDown = true;
else if (key == downKey)
downDown = true;
}

public void handleRelease(char key) {
if(key == upKey) {
upDown = false;
speed = 15;
}
else if (key == downKey) {
downDown = false;
speed = 15;
}
}

public void update() {
if(upDown) {
y = (float) (y - speed * 1.1);
}else if(downDown) {
y = (float) (y + speed * 1.1);
}
if(this.y >= PongMain.SCR_H - this.height/2) {
this.y = PongMain.SCR_H - this.height/2;
}
if(this.y <= 0 + this.height/2) {
this.y = 0 + this.height/2;
}
}

public void bot(Ball theBall){
if( theBall.getY() < y){
y = y - speed;
}else{
y = y + speed;
}
}

public float getX() {
return x;
}

public float getY() {
return y;
}

这是球代码:

package ball;
import processing.core.PApplet;

public class Ball {
float x, y, diameter, speed;
float direction;
int color[] = new int [3];

public Ball(float x,float y,float diameter,float speed,float direction, int red, int green, int blue) {
this.x = x;
this.y = y;
this.diameter = diameter;
this.speed = speed;
this.direction = direction;
color[0] = red;
color[1] = green;
color[2] = blue;
}

public void draw(PApplet p) {
p.noStroke();
p.fill(color[0],color[1],color[2]);
p.ellipse(x, y, diameter, diameter);
}

public void update() {
x+= speed * PApplet.cos(direction);
y+= speed * PApplet.sin(direction);
speed += 0.01;
}

private void reset() {
y = PongMain.SCR_H/2;
x = PongMain.SCR_W/2;

int n = (int)(Math.random() * 4);
if (n == 0) {
direction = ((float)Math.random()*70 +100);
}else if (n == 1) {
direction = (float)Math.random()*70 +100;
}else if (n == 2) {
direction = (float)Math.random()*70 +190;
}else if (n == 3) {
direction = (float)Math.random()*70 +280;
}
}

public void checkCollision(Paddle lPad, Paddle rPad) {
//if Bleft to the left of LpadRight
//if Bally <ytop >ybottom
if(x <= diameter/2 ) {
lPad.increScore();
reset();
}

if(x + diameter/2 >= PongMain.SCR_W) {
rPad.increScore();
reset();
}

if (y <= diameter/2 || y+diameter/2 >= PongMain.SCR_H) {
direction = -direction;
}

if(x-diameter/2 <= lPad.getX() + PongMain.PAD_W/2) {
if(y<=lPad.getY()+ PongMain.PAD_H/2 && y>=lPad.getY()-PongMain.PAD_H/2) {
direction = PApplet.PI - direction;
}
}

if(x+diameter/2 >= rPad.getX() - PongMain.PAD_W/2) {
if(y<=rPad.getY()+ PongMain.PAD_H/2 && y>=rPad.getY()-PongMain.PAD_H/2) {
direction = PApplet.PI - direction;
}
}
}

}

这是主要代码:

   import processing.core.PApplet;
import processing.core.PFont;
import java.awt.event.KeyEvent;

public class PongMain extends PApplet {
final static public int SCR_W = 1000;
final static public int SCR_H = 800;
final static public int frameRate = 50;
public static final int PAD_H = 150;
public static final int PAD_W = 20;
public static final int PADDING = 30;
public static final int PAD_Speed = 15;
public static final int Ball_Speed = 10;
public static final int Winningscore = 5;

Ball theBall;
Paddle lPad;
Paddle rPad;

public static void main(String[] args) {
PApplet.main("PongMain");
/**PongMain run = new PongMain();
Thread thread = new Thread(run);
thread.start();**/
}

public void settings(){
size(SCR_W, SCR_H);
}

public void setup(){
frameRate(frameRate);
theBall = new Ball(SCR_W/2, SCR_H/2,30,Ball_Speed,PI/6,255,45,60);
PFont f= createFont("Verdana", 16, true);
lPad = new Paddle(PADDING, SCR_H/2,PAD_W, PAD_H,
PAD_Speed,'w','s',255,43,80,0,f);
rPad = new Paddle(SCR_W-PADDING,SCR_H/2, PAD_W, PAD_H,
PAD_Speed,'i','k',50,79,80,0,f);
}



/* Run once for every frame */
public void draw(){
if(lPad.getScore() < Winningscore && rPad.getScore() < Winningscore) {
lPad.update();
rPad.update();
background(0);
theBall.draw(this);
theBall.update();
theBall.checkCollision(lPad, rPad);
lPad.draw(this);
rPad.draw(this);
}else {
PFont f= createFont("Verdana", 16, true);
this.textFont(f, 46);
this.fill(255);
this.text("Game is ended! Try again?", SCR_W/2-300, SCR_H/2);
this.text("Press R to restart!", SCR_W/2-200, SCR_H/2+100);
}
rPad.bot(theBall);
}

//The keyPressed() function is called once every time a key is pressed.
//The key that was pressed is stored in the key variable.
public void keyPressed() {
lPad.handlePress(key);
rPad.handlePress(key);
if(key == 'r') {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setup();
}
}

public void keyReleased() {
lPad.handleRelease(key);
rPad.handleRelease(key);
}

/**@Override
public void run() {
rPad.bot(theBall);
// TODO Auto-generated method stub

}**/
}

我希望右侧的 Racket 能够与球一起垂直移动,这样它就可以接住球并始终将其弹回来。

最佳答案

听起来您有点过于复杂了。你不需要很多花哨的逻辑来让你的游戏由机器人控制。像这样的事情可能会让你走得很远:

if(playerTwoY < ballY){
// player two is above the ball, move player two down
playerTwoY++;
}
else if(playerTwoY > ballY){
// player two is below the ball, move player two up
playerTwoY--;
}

如果您每帧都运行此代码,您的玩家二将不断更新自身。

我强烈建议您在尝试做更高级的事情之前先让这样的事情开始工作。但在您开始工作后,您可以尝试以下一些操作:

  • 每 X 秒更新一次桨的运动。这会给机器人带来更真实(更糟糕)的 react 时间。
  • 为桨的运动添加一些随机性。这会让机器人变得更不可预测。
  • 计算球的路径并用它来控制机器人。 (这是非常先进的,可能不需要获得机器人的工作版本。)

关于java - 在java乒乓球游戏中,如何使桨的一侧成为机器人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56471315/

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