2016-09-30 1 views
0

저는 코딩 초보자이며 탁구 게임을 만들기를 바랍니다. 나는 꽤 가까워서, 패들 모두 움직이고, 그들이 교차 할 때 공은 그들로부터 튀어 나온다. 하지만 일단 화면의 상단과 하단을 통과하면 볼의 궤적을 뒤집을 때 약간의 문제가 있습니다. 나는이 문제를 해결할 수 있다고 생각했지만 제대로 작동하지 않는 것 같습니다.Pong : 창의 상단과 하단 경계에서 볼을 뒤집습니다.

도움을 주시면 감사하겠습니다. 아래 코드.

import java.util.Random; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import org.newdawn.slick.Animation; 
import org.newdawn.slick.AppGameContainer; 
import org.newdawn.slick.BasicGame; 
import org.newdawn.slick.Color; 
import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.Image; 
import org.newdawn.slick.Input; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.SpriteSheet; 
import org.newdawn.slick.geom.Rectangle; 

public class PongAttempt extends BasicGame{ 
Image player1Img; 
Image player2Img; 
Image ball; 

float ballx = 500; 
float bally = 500; 

float x = 0; 
float y = 0; 

float bx = 100; 
float by = 40; 

float velocityx = -.10f; 
float velocityy = 0f; 

float player1Vel = 0; 
float player2Vel = 0; 

Rectangle player1Dim; 
Rectangle player2Dim; 
Rectangle ballDim; 

public PongAttempt(String gamename){super(gamename);} 

@Override 
public void init(GameContainer gc) throws SlickException { 

    player1Img = new Image("res/troll.png"); 
    player2Img = new Image("res/troll.png"); 
    ball = new Image("res/ball.png"); 

    player1Dim = new Rectangle(x, y, player1Img.getWidth(), player1Img.getHeight()); 
    player2Dim = new Rectangle(bx, by, player2Img.getWidth(), player2Img.getHeight()); 
    ballDim = new Rectangle(ballx, bally, ball.getWidth(), ball.getHeight()); 

    bx = gc.getWidth() - player2Img.getWidth(); 
    by = 0; 

    ballx = gc.getWidth()/2; 
    bally = gc.getHeight()/2; 

} 


@Override 
public void update(GameContainer gc, int i) throws SlickException { 

} 

@Override 
public void render(GameContainer gc, Graphics g) throws SlickException 
{ 

    g.setBackground(Color.black); 

    //Player 1 move and generate boundaries 

    if(gc.getInput().isKeyDown(Input.KEY_W) && y > 0) { 
     y = y - 0.2f; 
     player1Vel = -0.2f; 
    } 
    if(gc.getInput().isKeyDown(Input.KEY_S) && y < (gc.getHeight() - player1Img.getHeight())) { 
     y = y + 0.2f; 
     player1Vel = 0.2f; 
    } 



    //Player 2 Move and generate boundaries 

    if(gc.getInput().isKeyDown(Input.KEY_O) && by > 0) { 
     player2Vel = -0.2f; 
     by = by + player2Vel; 
    } 
    if(gc.getInput().isKeyDown(Input.KEY_L) && by < (gc.getHeight() - player2Img.getHeight())) { 
     player2Vel = 0.2f; 
     by = by + player2Vel; 
    } 


    if(gc.getInput().isKeyDown(Input.KEY_ESCAPE)){ 
     gc.exit(); 
    } 

    player1Dim.setX(x); 
    player1Dim.setY(y); 

    player2Dim.setX(bx); 
    player2Dim.setY(by); 

    ballDim.setX(ballx); 
    ballDim.setY(bally); 


    if(ballDim.intersects(player1Dim)){ 
     velocityx = velocityx * -1; 
     velocityy = player1Vel; 
    } 

    if(ballDim.intersects(player2Dim)){ 
     velocityx = velocityx * -1; 
     velocityy = player2Vel; 
    } 

    //This is where I tried to get the ball to bounce off the top and bottom 

if(ballx == 0 - ball.getHeight()){ 
     velocityx = velocityx * -1; 
    } 

    if(ballx == gc.getHeight() - ball.getHeight()){ 
     velocityx = velocityx * -1; 
    } 

    ballx = ballx + velocityx; 
    bally = bally + velocityy; 

    player1Img.draw(x, y); 
    player2Img.draw(bx, by); 
    ball.draw(ballx, bally); 

} 

public static void main(String[] args) 
{ 
    try 
    { 
     AppGameContainer appgc; 
     appgc = new AppGameContainer(new PongAttempt("Simple Slick Game")); 
     appgc.setDisplayMode(appgc.getScreenWidth(), appgc.getScreenHeight(), true); 
     appgc.start(); 
    } 
    catch (SlickException ex) 
    { 
     Logger.getLogger(PongAttempt.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
} 

답변

0

공의 높이가 정확히 0 (또는 창 높이)과 같은지 확인하고 있습니다. 렌더링되는 확률과 공 정확히은 0입니다. 공이 이미 0을 지나고 속도가 변경되었는지 실제로 확인해야합니다.

실제로 볼이 0을 지나면 속도가 변경되고 볼이 느려지므로 볼이 프레임 외부에있는 동안 다시 렌더링 될 수 있습니다. 그 속도는 화면에서 반전되고 멀리 떨어집니다. 이로 인해 창 밖에서 지속적으로 진동이 발생하고 모든 프레임이 속도 반전됩니다.

볼의 속도가 항상 정확한지 확인하고 더 복잡한 지오메트리를 수행하여 볼이 실제로 한 벽에서 벽을 통과해도 벽에서 튀어 나오게하는 추가 오류 확인이 있습니다.

+0

감사합니다. 지금 사용해 보았습니다. – SykoTron