Estou começando a implementar uma versão do pong para celular.
Mas a bola não está colidindo com a barra. Espero que o código esteja legível.
A bola é um arquivo .png de 16 x 16, mas não é transparente.
Já tentei ballSprite.collidesWith(barSprite, true) e ballSprite.collidesWith(barSprite, false) e nada de funcionar…
E a minha lógica para que a bola rebata ao chegar nos limites da tela do celular também parece não estar funcionando.
Conto com a ajuda de vocês!
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class ExampleGameCanvas extends GameCanvas implements Runnable {
private boolean isPlay; // Game Loop runs when isPlay is true
private long delay; // To give thread consistency
private int barx, bary; // To hold x and y positions of the bar sprite.
private int ballx, bally; // To hold x and y positions of the ball sprite.
private boolean upBall, downBall, leftBall, rightBall; // To hold the four directions of the ball.
private boolean rightPlayer, leftPlayer; // To hold the two directions of the bar.
private int width; // To hold screen width
private int height; // To hold screen height
// Sprites to be used.
private Sprite barSprite;
private Sprite ballSprite;
// Constructor and initialization.
public ExampleGameCanvas() throws Exception {
super(true);
width = getWidth();
height = getHeight();
// Cellphone used for tests has a display 128 x 110;
barx = (width - 64) / 2;
bary = height - 8;
ballx = (width - 16) / 2;
bally = (height - 16) / 2;
delay = 20;
// Load image to sprite.
Image bar = Image.createImage("/bar.PNG"); // bar.PNG has 64 x 8 pixels.
Image ball = Image.createImage("/icone.PNG"); // ball.PNG has 16 x 16 pixels.
barSprite = new Sprite(bar, 64, 8);
ballSprite = new Sprite(ball, 16, 16);
// Initialize ball variables holding direction.
upBall = false;
downBall = true;
rightBall = true;
leftBall = false;
}
// Automatically start thread for game loop.
public void start() {
isPlay = true;
Thread t = new Thread(this);
t.start();
}
public void stop() {
isPlay = false;
}
// Main Game Loop
public void run() {
Graphics g = getGraphics();
while (isPlay == true) {
input();
moveBall();
checkCollision();
drawScreen(g);
try {
Thread.sleep(delay);
}
catch (InterruptedException ie) {
}
}
}
// Method to handle user inputs.
private void input() {
int keyStates = getKeyStates();
// Left.
if(((keyStates & LEFT_PRESSED) != 0) && (barx > 0)) {
barx--;
leftPlayer = true;
rightPlayer = false;
}
// Right.
if(((keyStates & RIGHT_PRESSED) !=0) && (barx < (width - 64))) {
barx++;
rightPlayer = true;
leftPlayer = false;
}
}
private void moveBall() {
// Ball moves down and moves right.
if((downBall == true) && (rightBall == true)) {
if(ballx < (width - 16)) {
ballx++;
bally++;
if(ballx == (width - 16)) { // Check limits on right side.
rightBall = false;
leftBall = true;
}
}
}
// Ball moves down and moves left.
else if((downBall == true) && (leftBall == true)) {
if(ballx > 0) {
ballx--;
bally--;
if(ballx == 1) { // Check limits on left side.
leftBall = false;
rightBall = true;
}
}
}
// Ball moves up and moves right.
else if((upBall == true) && (rightBall == true)) {
if(ballx < (width - 16)) {
ballx++;
bally--;
if(ballx == (width - 16)) { // Check limits on right side.
rightBall = false;
leftBall = true;
}
}
}
// Ball moves up and moves left.
else if((upBall == true) && (leftBall == true)) {
if(ballx > 0) {
ballx--;
bally++;
if(ballx == 1) { // Check limits on left side.
leftBall = false;
rightBall = true;
}
}
}
// If ball is outside screen, put ball on initial position.
if((bally > height) || (bally == -16)) {
ballx = (width - 16) / 2;
bally = (height - 16) / 2;
downBall = true;
rightBall = true;
upBall = false;
leftBall = false;
}
}
// If ball collides with bar, ball should go up.
private void checkCollision() {
if(ballSprite.collidesWith(barSprite, false)) {
upBall = true;
downBall = false;
}
}
// Method to display graphics.
private void drawScreen(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
// Display sprites.
barSprite.setPosition(barx, bary);
ballSprite.setPosition(ballx, bally);
barSprite.paint(g);
ballSprite.paint(g);
flushGraphics();
}
}