Praktek 5 : User Input

19.13 Diposting oleh Gunawan

Pada praktik sebelumnya yaitu Praktek 4 Game Pimpong : Collision Detection, kita sudah membuat bola bergerak dengan di batasi oleh garis pinggir layar.

Pada praktek kali ini kita akan mencoba menambahkan dua buah Image Raket yaitu Image yang berada di sebelah kiri yang digerakan oleh user dan Image yang berada di sebelah kanan yang bergerak otomatis dijalankan oleh HP. Perhatikan Gambar Berikut :

Misalkan Image sebelah kiri yang di jalankan oleh user kita beri nama padSprite dan sebelah kanan kita namakan AISprite.Image kiri dan Image kanan terdiri dari satu image yang di gandakan artinya sebelum anda merubah script silahkan terlebih dahulu download dan simpan gambar pad.png berikut :


Silahkan Download dan simpan di C:/WTK25/APPS/GamePimPong/res gambar beriku Klik kanan di sini ( Klik kanan lalu save Image as atau save target as atau save Image)


Berikut adalah Code dari hasil penambahan Image Raket sehingga menghasilkan seperti gambar di atas.

/* -------------------------------------

* Nama File : PongCanvas.java

* by Gunawan

* jagungodak@yahoo.com

*--------------------------------------

*/

import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Image;

import javax.microedition.lcdui.game.GameCanvas;

import javax.microedition.lcdui.game.Sprite;

import java.util.Random;

public class PongCanvas extends GameCanvas implements Runnable {

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;

private Image padImg;

private Sprite padSprite;

private int padX = 10;

private int padY = getHeight() / 2;

private final static int padYVel = 2;

private Sprite AISprite;

private int AIX = getWidth() - 10;

private int AIY = getHeight() / 2;

private final static int AIYVel = 2;

private int actX;

public PongCanvas() {

super(false);

}

public void run(){

while(true){

updateScreen(getGraphics());

try{

Thread.sleep(sleepTime);

}catch(Exception ex){

}

}

}

public void start(){

try{

ballImg=Image.createImage("/face.png");

padImg = Image.createImage("/pad.png");

}catch(IOException ioex){

System.out.println("Kesalahan Pada Image"+ioex);

}

ballSprite=new Sprite(ballImg);

ballSprite.defineReferencePixel(2, 2);

ballSprite.setRefPixelPosition(ballX, ballY);

padSprite = new Sprite(padImg, 3, 20);

padSprite.defineReferencePixel(1, 10);

padSprite.setRefPixelPosition(padX, padY);

AISprite = new Sprite(padImg, 3, 20);

AISprite.defineReferencePixel(3, 10);

AISprite.setRefPixelPosition(AIX, AIY);

AISprite = new Sprite(padImg, 3, 20);

AISprite.defineReferencePixel(3, 10);

AISprite.setRefPixelPosition(AIX, AIY);

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);

createScoreboard(g);

moveBall();

movePad();

moveAI();

ballSprite.setRefPixelPosition(ballX , ballY);

ballSprite.paint(g);

padSprite.setRefPixelPosition(padX, padY);

padSprite.paint(g);

AISprite.setRefPixelPosition(AIX, AIY);

AISprite.paint(g);

flushGraphics();

}

public void moveBall(){

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 <>

ballDirection = 1;

} else if (ballDirection == 0 && ballY <>

ballDirection = 3;

} else if (ballDirection == 1 && ballY <>

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 <>

ballDirection = 2;

}

if (ballDirection == 0 && ballSprite.collidesWith(padSprite, false)) {

ballDirection = 1;

} else if (ballDirection == 3 && ballSprite.collidesWith(padSprite, false)) {

ballDirection = 2;

} else if (ballDirection == 1 && ballSprite.collidesWith(AISprite, false)) {

ballDirection = 0;

} else if (ballDirection == 2 && ballSprite.collidesWith(AISprite, false)) {

ballDirection = 3;

}

}

private void movePad() {

int keyState = getKeyStates();

if ((keyState & UP_PRESSED) != 0 && padY > padSprite.getHeight() / 2) {

padY -= padYVel;

} else if ((keyState & DOWN_PRESSED) != 0 && padY <>

padY += padYVel; }

}

private void moveAI() {

Random random = new Random();

actX = getWidth() / 3 + Math.abs(random.nextInt() % (getWidth() / 8));

if (ballY <> actX && AIY > AISprite.getHeight() / 2) AIY -= AIYVel;

if (ballY > AIY && ballX > actX && AIY <>

}

}



:D:D udah jalan toh gamenya yach...Ok.. tinggal satu lagi yang perlu di tambahkan yaitu : penambahan Score masing-masing Pemain. Silahkan Baca Praktek 6 : Akhir dari Petualangan Game Pimpong

0 komentar:

Posting Komentar