จากบทความที่ 1 และ 2 ที่ผมเขียนไว้จะอธิบายถึงการใช้ slick library นะครับ บทความนี้ก็เหมือนกัน จะเอา library ตัวนี้แหละมาใช้เหมือนเดิมซึ่งเนื้อหาในบทความนี้จะมี
- การทำภาพเคลื่อนไหว
- การชน และ การเก็บคะแนน
- การใส่เสียงประกอบ
- การยิง
การทำภาพเคลื่อนไหว
โดยที่ในตัว slick library จะมี class หนึ่งชื่อ Animation ให้เราใช้ึครับ ซึ่งมีิวิืธีการเรียกใช้โดยจะรับค่า Image[] และ เวลาที่จะแสดงผลต่อเฟรม ผ่าน argument ของ construct ได้เลย
ลักษณะการใช้
Image[] imgAnim = new Image[]{
new Image("frame1.png"),
new Image("frame2.png"),
};
int Fps = 200;
Animation anim = new Animation(imgAnim , Fps);
การชน และ การเก็บคะแนน
การชน สามารถเรียกใช้ผ่าน method ที่ชื่อ Intersects ครับซึ่งเป็น method ของ class Rectangle มี arguments เป็น rectangle 1 ตัว และจะคืนค่าเป็น boolean ครับ ซึ่งตรงนี้เราสามารถใช้ if มาตรวจจับ ได้ว่าชนหรือยัง หากชนจริงก็สามารถเก็บคะแนนได้เลย
ลักษณะการใช้
Rectangle aRect = new Rectangle(10,10,50,50);
Rectangle bRect = new Rectangle(200,10,50,50);
int score = 0;
if(aRect.Intersects(bRect)){
score++;
}
การใส่เสียงประกอบ
การใส่เสียงประกอบ สามารถ เรียกใช้ได้ผ่าน class ที่ชื่อ Sound และ Music ซึ่งไฟล์ที่สนับสนุน จะมีหลายชนิด แต่ไม่สนับสนุน mp3 นะครับ รายละเอียดไฟล์ที่สนุบสนุนจริงๆต้องไปศึกษาเพิ่มเอาเอง โดยการใช้งานหลักๆคงหนีไม่พ้น play() , stop() , loop()
ซึ่งคุณสมบัติของแต่ละตัว คงตามชื่อของมันเลยครับ
ลักษณะการใช้
Sound sound = new Sound("mysound.wav");
sound.play();
sound.loop();
การยิง
จริงๆแล้ว เทคนิค การยิงนี้คือการเอา ArrayList มาเก็บชนิดของกระสุนไว้ครับ แล้ว เวลาจะเพิ่มเข้าไปก็ใช้ method add ขึ้นมาใหม่ ก่อนที่จะไปวาดใน loop ที่เตรียมไว้
ลักษณะการใช้
ArrayList<BulletType> bullet = new ArrayList<BulletType>();
bullet.add(new BulletType());
for(BulletType b : bullet){
.....
}
จริงๆแล้ว บทความนี้ไม่ใช่ภาคจบของการพัฒนาเกมนะครับ แต่มันคือภาคจบของผม เอ้ย ไม่สิ ขอการพัฒนาเกมแบบพื้นฐานสิถึงจะถูก มีภาพ มีเสียง มีการควบคุม มีการเก็บคะแนน ได้หมดแล้ว
จากบทความที่ผ่านๆมาเพื่อนๆคงทราบกันแล้วนะครับว่า ต้องเตรียมไฟล์อะไรไว้บ้าง แต่จะมีเพิ่มขึ้นมาก็แต่ กระสุน ภาพที่จะเอามาทำ Animation และก็ เสียงประกอบ ครับ
@Source Code
//-------------------------------------------------------------------------------
package simple;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.Sound;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.Input;
import java.util.ArrayList;
import java.util.Random;
public class GExm3 extends BasicGame{
private Player player;
private ArrayList<Target> tar = new ArrayList<Target>();
private Random rnd = new Random();
private int score = 0;
public GExm3(){
super("Java Game Example 3");
}
public static void main(String[] args) throws SlickException{
AppGameContainer app = new AppGameContainer(new GExm3(),800,600,false);
app.start();
}
public void init(GameContainer gc) throws SlickException{
gc.setShowFPS(false);
player = new Player(680,100);
for(int i = 0;i<30;i++){
int rndX = rnd.nextInt(100)+50;
int rndY = rnd.nextInt(450)+50;
tar.add(new Target(rndX,rndY));
System.out.println("ID = "+tar.get(i).toString()+" X = "+rndX+", Y = "+rndY);
}
}
public void update(GameContainer gc,int delta) throws SlickException{
}
public void render(GameContainer gc,Graphics g) throws SlickException{
player.draw();
player.control(gc);
g.drawString("Score : "+score,50,50);
for(Target t : tar){
if(t!=null){
t.draw();
}
}
for(Bullet b : player.bullet){
if(b!=null){
for(Target t : tar){
if(t!=null && !t.isDie()){
if(b.box().intersects(t.box())){
t.hp -= b.getAtk();
b.killBullet();
break;
}
}else{
score+=3;
tar.remove(t);
break;
}
}
}
}
}
}
class Bullet{
private int x,y;
private int speed,life = 150;
private Image img;
private boolean die = false;
private int atk = 10;
public Bullet(int x,int y){
this.x = x;
this.y = y;
this.speed = 5;
try{
String url = "resource/";
this.img = new Image(url+"bullet.PNG");
}catch(SlickException e){
System.out.println(e);
}
}
public void killBullet(){
this.die = true;
}
public int getAtk(){
return(atk);
}
public boolean isDie(){
return(die);
}
public Rectangle box(){
Rectangle rect = new Rectangle(x,y,img.getWidth(),img.getHeight());
return(rect);
}
public void updateAndDraw(){
Graphics g = new Graphics();
g.drawImage(img,x,y);
if(life > 0){
life--;
x-=speed;
}else killBullet();
}
}
class Player{
private Image[] stopImage,atkImage;
private Animation animStop,animAtk;
private int fps = 200;
private enum Status{stop,atk};
private Status status = Status.stop;
private int x,y,speed;
public ArrayList<Bullet> bullet = new ArrayList<Bullet>();
private int delay,cooldown = 10;
public Player(int x,int y){
this.x = x;
this.y = y;
this.speed = 3;
this.delay = this.cooldown;
try{
String url = "resource/";
stopImage = new Image[]{
new Image(url+"stop.PNG"),
};
atkImage = new Image[]{
new Image(url+"atk_1.png"),
new Image(url+"atk_2.png"),
new Image(url+"atk_1.png"),
};
animStop = new Animation(stopImage,1000);
animAtk = new Animation(atkImage,fps);
}catch(SlickException e){
System.out.println(e);
}
}
public void draw(){
Graphics g = new Graphics();
switch(status){
case stop:{
g.drawAnimation(animStop,x,y);
break;
}
case atk:{
g.drawAnimation(animAtk,x,y);
if(animAtk.getFrame() == animAtk.getFrameCount()-1){
status = Status.stop;
}
break;
}
}
for(Bullet b : bullet){
if(b != null && !b.isDie()){
b.updateAndDraw();
}else{
bullet.remove(b);
break;
}
}
}
public void control(GameContainer container){
Input Key = container.getInput();
if(Key.isKeyDown(Key.KEY_UP)){
y -= speed;
}
if(Key.isKeyDown(Key.KEY_DOWN)){
y += speed;
}
if(Key.isKeyDown(Key.KEY_SPACE)){
status = Status.atk;
if(delay > 0){
delay--;
}else{
this.bullet.add(new Bullet(this.x-20,this.y+15));
delay = cooldown;
}
}
}
}
class Target{
private int x,y;
private Image img;
public int hp = 50;
private Sound sound;
public Target(int x,int y){
this.x = x;
this.y = y;
try{
this.img = new Image("resource/target.PNG");
this.sound = new Sound("resource/mysound.wav");
}catch(SlickException e){
System.out.println(e);
}
}
public void draw(){
Graphics g = new Graphics();
g.drawImage(img,x,y);
}
public boolean isDie(){
if(hp <= 0){
this.sound.play();
return(true);
}
return(false);
}
public Rectangle box(){
Rectangle g = new Rectangle(x,y,this.img.getWidth(),this.img.getHeight());
return(g);
}
}
//-------------------------------------------------------------------------------
สามารถโหลดตัวอย่างโปรแกรมไปศึกษาต่อได้จาก www.mediafire.com/