Pada pembahasan kali ini saya akan membuat sebuah game "Snake" sederhana dengan menggunakan bahasa pemrograman Processing. Bagi Anda yang belum mempunyai program processing silahkan download di www.processing.org . Setelah di download ekstrak processing.rar nya maka folder processingnya akan berisi seperti gambar dibwah ini :
Setelah di save lalu run kodingan yang telah kita buat dengan klik tombol play atau tekan Ctrl + R. Maka hasilnya akan jalan program game “Snake” sperti gambar dibawah ini :
LISTING PROGRAM
Referensi : http://processing.org/
selanjutnya kita buka program processing.exe nya , berikut tampilannya :
Setelah itu kita mulai membuat kodingan pada lembar kerja processing. Setelah semu kodingan sudah lengkap lalu kita save kodingan kita, caranya File > Save As lalu pilih di folder mana kita akan simpan kodingannya. Seperti gambar berikut :
Setelah di save lalu run kodingan yang telah kita buat dengan klik tombol play atau tekan Ctrl + R. Maka hasilnya akan jalan program game “Snake” sperti gambar dibawah ini :
pada game ini dimainkan oleh 1 orang , pemain harus memakan Food untuk menjadi besar , dan tidak boleh mengenai dinding agar permainan tidak berakhir. untuk menggerakan nya cukup menggeser mouse atau arah panah pada keyboard.
RULES
Aturan main (rules) pada game "Snake” adalah sebagai berikut:
- Pada game snake ini hanya bisa dimainkan oleh satu orang pemain.
- Untuk memulai permainan, otomatis setelah permainan ini terbuka. Dan pemain hanya tinggal menggerakkan ular tersebut untuk memakan Food (makanan) agar ular tersebut menjadi semakin panjang.
- Permainan selesai apabila pemain mengenai dinding pembatas telah habis.
LISTING PROGRAM
/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/65603*@* */
/* !do not delete the line above, required for linking your tweak if you re-upload */
/*
Snake
Simon Hajjar
11.05.12
*/
//Variables pertaining to the fundamental functioning
final int SCREEN_SIZE = 600; //Size of the screen
PFont font; //Font used in the game
int score = 0; //Score of the player
int rainbowFreq = 10, rainbowTimer = rainbowFreq; //Rainbow timers
PVector rainbowColor = new PVector(255, 255, 255); //The color of the rainbow color
boolean pauseState = false; //If the game is paused or not
boolean showInstructionMenu = true; //If the instruction menu should be shown
int superModeTimer = 0, superModeMax = 15, chanceOfSuperMode = 10; //If supermode is on
//Variables pertaining to the segments
ArrayList segments = new ArrayList(); //Creates an arraylist holding the segments
char currentDirection, lastDirection; //The current direction of the snake head
int segmentSize = 20; //Size of the segment
int snakeMovementTimer = 0, snakeMovementDelay = 4; //The timers for the snake's movement
boolean addSegmentNextUpdate = false; //If a segment should be added at the next update
//Variables pertaining to the food
Food food = new Food(); //Creates a new food
SuperFood superFood = null; //Creates the superfood
void setup() { //Main method
size(600, 600, JAVA2D); //Creates a screen
font = loadFont("FranklinGothic-MediumCond-48.vlw"); //Loads the font from the data folder
textAlign(CENTER); //Aligns text to the center
textFont(font, SCREEN_SIZE/25); //Sets the text font
segments.add(new Segment(SCREEN_SIZE/2, SCREEN_SIZE/2)); //Adds the head of the snake
}
void draw() { //Continual loop
if(!pauseState) rainbowTimer++; //Increments the rainbow color timer
if(rainbowTimer >= rainbowFreq) { //If the time for color change
rainbowColor = new PVector(random(255), random(255), random(255)); //Changes it to a random color
rainbowTimer = 0; //Resets the rainbow timer
}
if(!pauseState) snakeMovementTimer++; //Increments the snake movement timer if not paused
if(snakeMovementTimer == snakeMovementDelay) { //If the time for movement has come
updateSnake(); //Update the snake
snakeMovementTimer = 0; //Reset the timer
}
if(superModeTimer >= 0) superModeTimer--; //Decrements the super timer
if(superFood == null && superModeTimer < 0 && !pauseState && !showInstructionMenu)
if(random(1000) < chanceOfSuperMode) superFood = new SuperFood();
background(35, 5 , 100); //Sets the background to white
for(int i = 0; i < segments.size(); i++) //Checks each segment
segments.get(i).renderSegment(); //Renders the segment
food.renderFood(); //Renders the food
if(superFood != null) superFood.renderFood(); //Renders superfood if it is not null
fill(175);
text("Nilai Anda: " + score, SCREEN_SIZE/2, segmentSize*6/5); //Writes the score for the player
if(pauseState) { //If the game is paused
fill(0, 10, 200, 100); //Fills a translucent black
noStroke(); //Cancels the stroke
rect(0, 0, SCREEN_SIZE, SCREEN_SIZE); //Cover the screen with a transluscent black
fill(255); //Colors white
text("Permainan Berhenti", SCREEN_SIZE/2, SCREEN_SIZE/2);
text("Tekan 'P' Untuk Melanjutkan Permainan", SCREEN_SIZE/2, SCREEN_SIZE/2 + segmentSize*6/5);
} else if(showInstructionMenu) { //If the instruction menu is shown
text("Gunakan tombol arah pada keyboard", SCREEN_SIZE/2, SCREEN_SIZE*3/4);
text("Tekan 'P' untuk berhenti sejenak", SCREEN_SIZE/2, SCREEN_SIZE*3/4 + segmentSize*6/5);
}
}
void keyPressed() { //Called when a key is pressed
switch(keyCode) { //Checks the key pressed
case UP: if(segments.size() == 1 || lastDirection != 'S' && !pauseState) currentDirection = 'W'; break; //Changes direction to up
case DOWN: if(segments.size() == 1 || lastDirection != 'W' && !pauseState) currentDirection = 'S'; break; //Changes direction to down
case LEFT: if(segments.size() == 1 || lastDirection != 'D' && !pauseState) currentDirection = 'A'; break; //Changes direction to left
case RIGHT: if(segments.size() == 1 || lastDirection != 'A' && !pauseState) currentDirection = 'D'; break; //Changes direction to right
case 'P': case 'p': pauseState = !pauseState; break; //Toggles the pause
}
showInstructionMenu = false; //Take off the instruction menu
}
void updateSnake() { //Updates the snake's position
if(addSegmentNextUpdate) //If must add a new segment
segments.add(new Segment((int)segments.get(segments.size() - 1).location.x, (int)segments.get(segments.size() - 1).location.y)); //Adds a segment at the tail
for(int i = segments.size() - 1; i > 0; i--) { //Checks each segments besides the head
if(!addSegmentNextUpdate) segments.get(i).location = new PVector(segments.get(i - 1).location.x, segments.get(i - 1).location.y); //Sets the location to the block ahead
addSegmentNextUpdate = false; //Removes the need to add a new segment
}
switch(currentDirection) { //Checks the current direction
case 'W': segments.get(0).location.y -= segmentSize; break; //Moves the snake head up
case 'S': segments.get(0).location.y += segmentSize; break; //Moves the snake head down
case 'A': segments.get(0).location.x -= segmentSize; break; //Moves the snake head left
case 'D': segments.get(0).location.x += segmentSize; break; //Moves the snake head right
}
lastDirection = currentDirection; //Sets the last direction used as the current direction
for(int i = 2; i < segments.size(); i++) //Checks each segment besides the head
if(superModeTimer < 0 && segments.get(i).location.x == segments.get(0).location.x && segments.get(i).location.y == segments.get(0).location.y) //If a segment hits the head
startNewGame(); //Starts a new game
if(segments.get(0).location.x >= SCREEN_SIZE || segments.get(0).location.x < 0)
if(superModeTimer >= 0) segments.get(0).location.x = (segments.get(0).location.x >= SCREEN_SIZE) ? 0 : SCREEN_SIZE - segmentSize;
else startNewGame();
if(segments.get(0).location.y >= SCREEN_SIZE || segments.get(0).location.y < 0)
if(superModeTimer >= 0) segments.get(0).location.y = (segments.get(0).location.y >= SCREEN_SIZE) ? 0 : SCREEN_SIZE - segmentSize;
else startNewGame();
}
void startNewGame() { //Starts a new game
superFood = null; //Nullifies the super food
score = 0; //Resets the score
segments.clear(); //Clears all segments
segments.add(new Segment(SCREEN_SIZE/2, SCREEN_SIZE/2)); //Adds the head of the snake
snakeMovementTimer = 0; //Reset the timer
currentDirection = 'Q'; //Nullifys the current direction
keyCode = '+'; //Nullifys the keyCode
food = new Food(); //Resets the food
showInstructionMenu = true; //Show the instruction menu
}
Referensi : http://processing.org/
0 comments:
Post a Comment
silahkan tinggalkan komentar anda disini .. :D