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を越えると速度が変わってボールが遅くなると、ボールがまだフレームの外側にあるときにレンダリングが再開する可能性があるため、実際はそれより少し複雑ですその速度は逆転してスクリーンから再び離れる。これは窓の外で連続的に振動し、その速度はフレームごとに逆転します。

ボールの速度が常に正しいことを確認し、ボールが実際に壁を飛び越えて1つのフレーム内を移動してもボールを実際に跳ね返させるために、より複雑なジオメトリを実行することで、

+0

ありがとうございました、私は今働くことができました。 – SykoTron

関連する問題