乒乓球没有弹出墙壁

问题描述 投票:2回答:1

当它撞到墙壁时,它只是沿墙壁滚动,但我正在反转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

        }

}
java swing class user-interface pong
1个回答
0
投票

而不是做randomBally -=4;randomBally +=4;你可以做randomBally *=-1。你应该将速度乘以-1,而不是增加速度,这会改变运动的方向。

© www.soinside.com 2019 - 2024. All rights reserved.