gpt4 book ai didi

Java pong 不能同时移动两个 Racket

转载 作者:行者123 更新时间:2023-11-30 07:17:34 27 4
gpt4 key购买 nike

试图在 java 中制作乒乓球但不能同时移动两个 Racket 。您可以移动其中一个,但不能同时移动两者。我是否需要创建具有 2 个不同面板的 2 个线程?

这里是我指定关键事件的地方

public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_QUOTE || e.getKeyCode() == KeyEvent.VK_SEMICOLON){
if(e.getKeyCode() == KeyEvent.VK_A){
y-=10;
}

if(e.getKeyCode() == KeyEvent.VK_S){
y+=10;
}

if(e.getKeyCode() == KeyEvent.VK_QUOTE){
ytwo-=10;
}
if(e.getKeyCode() == KeyEvent.VK_SEMICOLON){
ytwo+=10;
}

}
}

完整代码如下

import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Pong extends JFrame implements ActionListener{

//implement constants

PongPanel pongPanel = new PongPanel();

//JFrame pong x and y coordinates
static final int jfpX = 150;
static final int jfpY = 20;

// JFrame pong width and height
static final int jfpW = 800;
static final int jfpH = 600;

Thread thrd;

public static void main(String[] args) {
Pong jfp = new Pong();
jfp.setVisible(true);

}

public Pong(){
setBounds(jfpX,jfpY,jfpW,jfpH);
setTitle("Pong");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(Color.black);


add(pongPanel);
addKeyListener(pongPanel);
thrd = new Thread (pongPanel);
thrd.start();
}

public void actionPerformed(ActionEvent e) {

}



}

class PongPanel extends JPanel implements Runnable, KeyListener{
Random random = new Random();
static final int jpW = 800;
static final int jpH = 600;
int paddleStart = (jpH/2)-35;
int paddleStarttwo = (jpH/2)-35;
int ballStartX = (jpW/2)-20;
int ballStartY = (jpH/2)-20;
int ytwo,x,y;
int ballD = 30;
int paddleW1 = 20;
int paddleH1 = 100;
int paddleW2 = 20;
int paddleH2 = 100;
int min = -2;
int max = 2;
int randomBallx, randomBally;
// int randomBallx = random.nextInt(max-min+1)+min;
// int randomBally = random.nextInt(max-min+1)+min;

int rand1 = random.nextInt(2-1 + 1)+1; // random for function to determine ballx and bally
int rand2 = random.nextInt(2-1+2)+1;
int dx = 4;
int dy = 4; //direction of y

public void ballNotZero(){// makes sure the ball doesnt go straight up and down
if (randomBallx ==0){
randomBallx = random.nextInt(max-min+1)+min;
}
if(randomBally == 0){
randomBally=random.nextInt(max-min+1)+min;
}
// if(rand1 ==1){
// randomBallx=-1;
// }
// if(rand1 ==2){
// randomBallx=1;
// }
// if(rand2 ==1){
// randomBally =-1;
// }
// if(rand2==2){
// randomBally = 1;
// }

}


public PongPanel(){

}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);

Color ball;
Color paddleOne;
Color paddleTwo;
ball = new Color(255,0,255);
paddleOne = new Color(255,0,0);
paddleTwo = new Color(0,0,255);


g.setColor(ball);
g.fillOval(ballStartX+randomBallx,ballStartY+randomBally,ballD,ballD);

g.setColor(paddleOne);
g.fillRect(20,paddleStart+y,paddleW1,paddleH1);

g.setColor(paddleTwo);
g.fillRect(760,paddleStarttwo+ytwo,paddleW2,paddleH2);



}
public void run() {
while(true){
ballNotZero();
detectPaddle();
randomBall();
ballMove();
repaint();
try {Thread.sleep(75); } catch(Exception e){

};

}
}
public static boolean intervallContains(int low, int high, int n) { //determines if something is in a certain range
return n >= low && n <= high;
}
public void detectPaddle(){ //determines if ball is close enough to paddle for detection
int withinY = (paddleStart+y) -(ballStartY+randomBally);
int withinY1 = (paddleStarttwo+ytwo)-(ballStartY+randomBally);

if (ballStartX+randomBallx <=20 && intervallContains(-50,50,withinY)){
dx = -dx;
}
if(ballStartX+randomBallx >=760 && intervallContains(-50,50,withinY1)){
dx = -dx;
}
}

public void randomBall(){
if(randomBallx >=0 ){
randomBallx+=dx;
}
if(randomBallx<0){
randomBallx-=dx;
}
if(randomBally>=0){
randomBally+=dy;
}
if(randomBally<0){
randomBally-=dy;
}
// randomBallx+=randomBallx;
// randomBally+=randomBally;
}
public void ballMove(){
if(ballStartY+randomBally > jpH-60){
dy= -dy;

}
if(ballStartY+randomBally <0){
dy = -dy;
}
}

public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_QUOTE || e.getKeyCode() == KeyEvent.VK_SEMICOLON){
if(e.getKeyCode() == KeyEvent.VK_A){
y-=10;
}

if(e.getKeyCode() == KeyEvent.VK_S){
y+=10;
}

if(e.getKeyCode() == KeyEvent.VK_QUOTE){
ytwo-=10;
}
if(e.getKeyCode() == KeyEvent.VK_SEMICOLON){
ytwo+=10;
}

}
}



public void keyTyped(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

}

最佳答案

  • 不要使用 while (true) 而是使用 Swing 计时器。
  • 不要使用 KeyListeners,而是使用 Key Bindings。
  • 考虑打开一个 Swing 计时器(它可以充当您的“后台线程”),当绑定(bind)检测到按下了正确的键时,它会移动一个桨,
  • 并在释放同一个键时停止移动桨的计时器。
  • 另一个桨也一样,但当然要让它的 Action 响应不同键对的绑定(bind)。
  • 当尝试让这样的东西运行时,首先尝试让它在一个非常简单的程序中运行,一个没有主程序所需的所有其他垃圾的程序,但是一个可以让您测试和证明您的概念的程序。然后,如果你在你的小程序中运行,很好,你可以将功能添加到你的主程序中。如果您的小程序代码不工作,需要我们的帮助,您可以在这里发布自包含的小程序(sscce),供我们测试和修改。

关于Java pong 不能同时移动两个 Racket ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16025533/

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