2017-08-25 21 views
0

作爲新編碼之間的檢測/碰撞,我看了關於如何創建傍視頻。這很好,因此我想嘗試使用一些用於Pong的編碼技術來重新創建Brick Breaker。到目前爲止,我擁有球,槳和遊戲基礎。但是,球不會與槳發生碰撞,而不會通過它的彈跳。的Java對象命中

代碼基礎遊戲:

import java.applet.Applet; 
import java.awt.Color; 
import java.awt.Image; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

public class Breaker extends Applet implements Runnable, KeyListener{ 
final int WIDTH = 600, HEIGHT= 400; 
Thread thread; 
Graphics gfx; 
Image img; 
Ball b1; 
Player p1; 

public void init() { 
    this.resize(WIDTH, HEIGHT); 
    this.addKeyListener(this); 
    b1 = new Ball(); 
    p1 = new Player(1); 
    img = createImage(WIDTH,HEIGHT); 
    gfx = img.getGraphics(); 
    thread = new Thread(this); 
    thread.start(); 
} 

public void paint(Graphics g) { 
    gfx.setColor(Color.black); 
    gfx.fillRect(0, 0, WIDTH, HEIGHT); 
    b1.draw(gfx); 
    p1.draw(gfx); 
    g.drawImage(img, 0, 0, this); 
} 

public void update (Graphics g) { 
    paint(g); 
} 

@Override 
public void keyPressed(KeyEvent e) { 
    if(e.getKeyCode() == KeyEvent.VK_LEFT){ 
     p1.setLeftAccel(true); 
    } 
    else if(e.getKeyCode() == KeyEvent.VK_RIGHT){ 
     p1.setRightAccel(true); 
    } 

} 

@Override 
public void keyReleased(KeyEvent e) { 
    if(e.getKeyCode() == KeyEvent.VK_LEFT){ 
     p1.setLeftAccel(false); 
    } 
    else if(e.getKeyCode() == KeyEvent.VK_RIGHT){ 
     p1.setRightAccel(false); 
    } 
} 

@Override 
public void run() { 
    for(;;) { 

     p1.move(); 
     b1.move(); 
     b1.checkPlayerCollision(p1); 

     repaint(); 
     getToolkit().sync(); 
     try { 
      Thread.sleep(10); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

@Override 
public void keyTyped(KeyEvent arg0) { 
} 
} 

球:

import java.awt.Graphics; 
import java.awt.Color; 

public class Ball { 
int score; 
double xVel, yVel, x, y; 

public Ball() { 
    x= 350; 
    y = 250; 
    xVel= 0; 
    yVel = 1; 
} 

public void draw(Graphics g) { 
    g.setColor(Color.white); 
    g.fillOval((int)x - 10, (int)y - 10, 20, 20); 
} 

public void checkPlayerCollision(Player p1) { 
    if (x <= 50) { 
     if(x >= p1.getX() && x <= p1.getX() + 10) 
      xVel = -xVel; 
    } 
    else if (x >= 680) { 
     xVel = -xVel; 
    } 
} 

public void move() { 
    x += xVel; 
    y += yVel; 

    if (y < 10) //Bounce at top side of screen 
     yVel = -yVel; 
    if (x < 10) // Bounce at left side of screen 
     xVel = -xVel; 
} 

public int getX() { 
    return (int)x; 
} 

public int getY() { 
    return (int)y; 
} 


} 

球員:

import java.awt.Color; 
import java.awt.Graphics; 

public class Player implements Paddle { 
double x, xVel; 
boolean leftAccel, rightAccel; 
int player, y; 

public Player(int player) { 
    leftAccel = false; rightAccel = false; 
    x=260; xVel=0; 
} 

@Override 
public void draw(Graphics g) { 
    g.setColor(Color.white); 
    g.fillRect((int)x, 380, 80, 10); 

} 

@Override 
public void move() { 
    if(leftAccel){ 
     xVel -= 2; 
    } 
    else if (rightAccel) { 
     xVel += 2; 
    } 
    else if (!leftAccel && !rightAccel) { 
     xVel = 0; 
    } 
    if(xVel >= 5) { 
     xVel = 5; 
    } 
    else if (xVel <= -5) { 
     xVel = -5; 
    } 
    x += xVel; 

    if(x < 0) 
     x=0; //Collision with left wall 
    if(x > 520) 
     x = 520; //Collision with right wall 

} 

public void setLeftAccel(boolean input) { 
    leftAccel = input; 
} 

public void setRightAccel(boolean input) { 
    rightAccel = input; 
} 

@Override 
public int getX() { 
    return (int)x; 

} 
} 

槳:

import java.awt.Graphics; 

public interface Paddle { 
public void draw(Graphics g); 
public void move(); 
public int getX(); 

} 

我還沒有完全理解命中檢測,所以我只是將用於pong的東西搞砸了,但是,這不起作用(我相信它可能看起來很奇怪)。

+0

你似乎只能檢查X座標在'Ball.checkPlayerCollision()'函數。 X(通常)是水平軸。嘗試用'if(y <= 50)'替換第一個檢查('if(x <= 50)')。編輯:並且反轉'yVel'而不是'xVel'。 – Kalkran

+0

您也可能想要將橢圓的半徑包含在碰撞檢測中。你質疑的位置是球的中間位置,但你實際上想要質疑球的邊界是否與某物相撞。 –

回答

1

碰撞檢測可能很難,有時周圍包裹你的頭。真正能幫助我的是在一張紙上畫出我需要檢查的東西,並逐個將代碼寫入代碼。這也是一個好主意,讓您的軸記住,它真的很容易混淆的xy,但他們會對遊戲產生重要影響。 我的目光被吸引到你的checkPlayerCollision功能,因爲它似乎有點過,我身邊有改變了一些檢查和評價了每個檢查/功能正在做什麼。

public void checkPlayerCollision(Player p1) { 
    if (
     // Is the ball at or below paddle height? 
     y <= 50 
     // AND Is the ball right of the left side of the paddle? 
     && x >= p1.getX() 
     // AND Is the ball left of the right side of the paddle? 
     && x <= p1.getX() + 10 
    ) { 
     // Collision with the paddle! 
     // (Ball is lower than y=50 and between left and right side of the paddle!) 
     // As we want the ball to go back up, we will be changing its velocity 
     // along the Y-axis. 
     yVel -= yVel; 
    } else if (x >= 680) { 
     // This checks whether the ball went too far to the right..? 
     xVel -= xVel; 
    } 
} 

最好的提示我可以給你:搶一張紙,只是畫一個簡單的網格或圖形,並用它來繪製出你需要檢查不同的事情。請務必標明您的(0,0)點,所以當你與不同的邊框工作,你知道哪些是0(通常是頂部和左側邊框),哪些是等於width和畫布的height