2

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

        }

}
4

1 回答 1

0

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

于 2018-01-01T06:30:42.097 回答