Kali ini saya akan memberikan contoh tentang sebuah game java sederhana yang pernah saya buat dan ini merupakan game Ke Tiga yang saya buat dengan menggunakan bahasa pemograman Java. Mungkin bagi temen - temen yang ingin mengembangkan game saya ini menjadi lebih baik lagi silakan saja.
Logika game ini adalah :
* Player harus menjalankan Ular untuk mencari makanan yg berbentuk lingkaran merah dengan cepat dan tepat yang untuk mendapatkan point.
* Player harus menjalankan Ular biar tidak menabrak diding arena supaya tidak mati bro..hehehe :D )
* Player semakin banyak memakan maka akan mendapatkan poit banyak dan semakin banyak makan pula semakin panjang ularnya alias dowo uler'e bro..hehehe :D )
Class engin
package Snake_engin;
import javax.swing.JOptionPane;
import m_snake.MainPanel.snakeBody.Food;
import m_snake.MainPanel.snakeBody.snake;
public class engin {
public engin()
{
}
public void Move_mySnake(int newDirection,boolean food_eaten)
{
if(!Game_Over)
{
switch(newDirection)
{
case 1: if(direction!=3)
{
my_snake.put_Head_postion(0,-step_to_move);
direction=1;
}
else move_as_last(direction);
break;//UP
case 2: if(direction!=4)
{
my_snake.put_Head_postion(step_to_move,0);
direction=2;
}
else move_as_last(direction);
break;//RIGHT
case 3:
if(direction!=1)
{
my_snake.put_Head_postion(0,step_to_move);
direction=3;
}
else move_as_last(direction);
break;//DOWN
case 4:
if(direction!=2)
{
my_snake.put_Head_postion(-step_to_move,0);
direction=4;
}
else move_as_last(direction);
break;//LEFT
}
if(food_eaten)
{
my_snake.Increase_lenght();// IF the Snake ate new Food
}
my_snake.Move_snake();
if((my_snake.getHeadX()==snakeFood.get_int_X())&&(my_snake.getHeadY()== snakeFood.get_int_Y()))
{
snakeFood.getFood();
Score+=20;
my_snake.Increase_lenght();
while(check_food_place())
{
snakeFood.getFood();
}
}
Game_Over=my_snake.check_game_over();
}
if(Game_Over)
{
JOptionPane.showConfirmDialog(null,"Score = "+Integer.toString(Score),"Your Score",JOptionPane.DEFAULT_OPTION);
// Thread.yield();
}
}
public void move_as_last(int diirec)
{
switch(direction)
{
case 1:
my_snake.put_Head_postion(0,-step_to_move);
break;//UP
case 2:
my_snake.put_Head_postion(step_to_move,0);
break;//RIGHT
case 3:
my_snake.put_Head_postion(0,step_to_move);
break;//DOWN
case 4:
my_snake.put_Head_postion(-step_to_move,0);
break;//LEFT
}
}
public int[] return_Snake_postionsX()
{
return my_snake.get_All_postionsX();
}
public int[] return_Snake_postionsY()
{
return my_snake.get_All_postionsY();
}
public int get_Oval_size()
{
return my_snake.getOval_Size();
}
public int get_my_snake_lenght()
{
return my_snake.get_lenght();
}
public void set_step_length()
{
step_to_move=5;
}
public int get_Food_X()
{
return (int)snakeFood.getX();
}
public int get_Food_Y()
{
return (int)snakeFood.getY();
}
public boolean check_food_place()
{
int xfood=(int)snakeFood.getX();
int yfood=(int)snakeFood.getY();
if(xfood>260||yfood>260)return true;
else
return my_snake.compare_with_foodPostion(xfood,yfood);
}
public boolean get_Game_over()
{
return Game_Over;
}
private int direction=2;
private boolean Game_Over;
private boolean Moving_vertical=false;
private snake my_snake=new snake();
private final Food snakeFood=new Food();
private int step_to_move=20;
private int Score;
}
Class main frame
package m_snake;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JApplet;
import javax.swing.JFrame;
import m_snake.MainPanel.myPanel;
public class MainFrame extends JApplet implements KeyListener{
/** Creates a new instance of MainFrame */
public void init() {
setSize(540,540);
Container cpane=getContentPane();
cpane.add(snakePanel);
addKeyListener(this);
}
/**
* @param args the command line arguments
*
*/
public static void main(String[] args) {
// TODO code application logic here
MainFrame my_Game=new MainFrame();
JFrame myFrame=new JFrame(" Simple Snake Game ");
myFrame.setSize(300,300);
myFrame.getContentPane().add(my_Game);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.addKeyListener(my_Game);
my_Game.init();
my_Game.start();
my_Game.setVisible(true);
myFrame.setVisible(true);
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==UP) {
snakePanel.set_Direction(1);
} else if (e.getKeyCode()==RIGHT) {
snakePanel.set_Direction(2);
} else if(e.getKeyCode()==DOWN) {
snakePanel.set_Direction(3);
} else if(e.getKeyCode()==LEFT) {
snakePanel.set_Direction(4);
}
}
public void keyReleased(KeyEvent e) {
}
private final int LEFT = 37;
private final int RIGHT = 39;
private final int UP = 38;
private final int DOWN = 40;
private myPanel snakePanel=new myPanel();
}
Class mypanel
package m_snake.MainPanel;
import Snake_engin.engin;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import m_snake.MainPanel.snakeBody.Food;
import m_snake.MainPanel.snakeBody.snake;
/**
*
* @author SQLStudent
*/
public class myPanel extends JPanel {
/** Creates a new instance of myPanel */
public myPanel() {
setBorder(my_border);
// mythread.setPriority(1);
mythread.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D=(Graphics2D)g;
g2D.setColor(Color.BLUE);
Parts_X=my_engin.return_Snake_postionsX();
Parts_Y=my_engin.return_Snake_postionsY();
for(int i=0;i<my_engin.get_my_snake_lenght();i++) {
g2D.fillRect(Parts_X[i],Parts_Y[i],my_Oval_size,my_Oval_size);
}
g2D.setColor(Color.RED);
g2D.fillOval(my_engin.get_Food_X()+5,my_engin.get_Food_Y()+5,my_Oval_size-5,my_Oval_size-5);
}
class MY_Thread extends Thread {
public void run() {
while(true) {
my_engin.Move_mySnake(direction,false);
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
repaint();
if(my_engin.get_Game_over()) {
stop();
}
}
}
}
public void set_Direction(int newDirection) {
direction=newDirection;
}
private engin my_engin=new engin();
private int Parts_X[];
private int Parts_Y[];
private int direction=2;
private boolean is_my_first_moves=true;
private final MY_Thread mythread=new MY_Thread();
private final int my_Oval_size=my_engin.get_Oval_size();
private final int LEFT = 37;
private final int RIGHT = 39;
private final int UP = 38;
private final int DOWN = 40;
private LineBorder my_border=new LineBorder(Color.green.darker(),3);
}
Note : Shilakan mencoba bila masih kesulitan mebuat game ini bisa komentar di bawah ini siap saya bantu.
Terimakasih telah mengunjungi blog saya ini.
Pengunjung yg baik selalu ninggalin jejak :)
Salam Wani Sinau!
Tidak ada komentar:
Posting Komentar