بسم الله الرحمن الرحيم
السلام عليكم
أنا حالياً أدرس هذا الكود و هناك شيء لا أفهمه به : الكود هو :
package dk.koderko.games.pong;
import java.io.IOException;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class PongCanvas extends GameCanvas implements Runnable {
public PongCanvas() {
super(false);
}
public void run() {
while(true) {
updateScreen(getGraphics());
try {
Thread.sleep(sleepTime);
} catch (Exception e) {
}
}
}
public void start() {
try {
ballImg = Image.createImage("/ball.png");
} catch (IOException ioex) {
System.out.println(ioex);
}
ballSprite = new Sprite(ballImg, 3, 3);
ballSprite.defineReferencePixel(2, 2);
ballSprite.setRefPixelPosition(ballX, ballY);
Thread runner = new Thread(this);
runner.start();
}
private void createBackground(Graphics g) {
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
}
private void updateScreen(Graphics g) {
createBackground(g);
moveBall();
ballSprite.setRefPixelPosition(ballX, ballY);
ballSprite.paint(g);
flushGraphics();
}
private void moveBall() {
// NOTE: I have deleted the two lines previously present here
if (ballDirection == 0) { // Added: All of the lines below
ballX -= ballXVel;
ballY -= ballYVel;
} else if (ballDirection == 1) {
ballX += ballXVel;
ballY -= ballYVel;
} else if (ballDirection == 2) {
ballX += ballXVel;
ballY += ballYVel;
} else if (ballDirection == 3) {
ballX -= ballXVel;
ballY += ballYVel;
}
if (ballDirection == 0 && ballX < 0) {
ballDirection = 1;
} else if (ballDirection == 0 && ballY < 0) {
ballDirection = 3;
} else if (ballDirection == 1 && ballY < 0) {
ballDirection = 2;
} else if (ballDirection == 1 && ballX > getWidth()) {
ballDirection = 0;
if (sleepTime > 5) sleepTime--;
} else if (ballDirection == 2 && ballY > getHeight()) {
ballDirection = 1;
} else if (ballDirection == 2 && ballX > getWidth()) {
ballDirection = 3;
if (sleepTime > 5) sleepTime--;
} else if (ballDirection == 3 && ballY > getHeight()) {
ballDirection = 0;
} else if (ballDirection == 3 && ballX < 0) {
ballDirection = 2;
} // Added: All of the lines above
}
private int sleepTime = 30;
private Image ballImg;
private Sprite ballSprite;
private int ballX = getWidth() / 2;
private int ballY = getHeight() / 2;
private final static int ballXVel = 3;
private final static int ballYVel = 1;
private int ballDirection = 1; // Added
}و ما لا أفهمه بالكود هو :
if (ballDirection == 0) {
ballX -= ballXVel;
ballY -= ballYVel;
} else if (ballDirection == 1) {
ballX += ballXVel;
ballY -= ballYVel;
} else if (ballDirection == 2) {
ballX += ballXVel;
ballY += ballYVel;
} else if (ballDirection == 3) {
ballX -= ballXVel;
ballY += ballYVel;
}
if (ballDirection == 0 && ballX < 0) {
ballDirection = 1;
} else if (ballDirection == 0 && ballY < 0) {
ballDirection = 3;
} else if (ballDirection == 1 && ballY < 0) {
ballDirection = 2;
} else if (ballDirection == 1 && ballX > getWidth()) {
ballDirection = 0;
if (sleepTime > 5) sleepTime--;
} else if (ballDirection == 2 && ballY > getHeight()) {
ballDirection = 1;
} else if (ballDirection == 2 && ballX > getWidth()) {
ballDirection = 3;
if (sleepTime > 5) sleepTime--;
} else if (ballDirection == 3 && ballY > getHeight()) {
ballDirection = 0;
} else if (ballDirection == 3 && ballX < 0) {
ballDirection = 2;
}أرجو المساعدة ......:( و هذه الصورة توضح الاتجاهات :
