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
}
}
변수 이름 ... 오 하나님 이유 ... –
랜덤 Y = 랜덤. 철저한 설명이 필요하면 알려주고 예제를 입력 해 보겠습니다. –
오, 방금 코드를 살펴 봤는데 잘못된 연산자를 사용하고 있다고 생각합니다. randomY - = 4는 randomY의 값을 읽으려고 4를 뺀 다음 새 값을 randomY에 저장합니다. randomY = randomY - 4와 같습니다. 그렇게 생각하지 않습니다. 속도 변수 dx와 dy를 사용하면 벽에 따라 충돌이 발생할 때 dx = -dx 또는 dy = -dy를 설정 한 다음 x + = dx 및 y + = dy로 항상 올바른 방향으로 공을 유지해야합니다. –