gpt4 book ai didi

java - 乒乓球不会从墙上弹起

转载 作者:行者123 更新时间:2023-11-29 09:03:57 28 4
gpt4 key购买 nike

当它碰到墙壁时,它只是沿着墙壁滚动,但我正在反转 y 坐标。另外,您不能同时移动桨。有什么建议吗?我应该创建 2 个线程吗?

这是我在 Y 线撞到墙上后更换的功能。

 public void ballMove(){
if(ballStartY+randomBally > jpH){
randomBally -=4;

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

完整代码如下

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 = random.nextInt(max-min+1)+min;
int randomBally = random.nextInt(max-min+1)+min;

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){

randomBall();
ballMove();
repaint();
try {Thread.sleep(75); } catch(Exception e){

};

}
}

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

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

public void keyPressed(KeyEvent e) {

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

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

}



public void keyTyped(KeyEvent e) {

}

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

}

}

最佳答案

除了执行 randomBally -=4;randomBally +=4; 你可以执行 randomBally *=-1。您不应增加速度,而应将速度乘以 -1,这会改变运动的方向。

关于java - 乒乓球不会从墙上弹起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16002872/

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