Selasa, 25 Juli 2017

BAB 4 Pengantar Teknologi Game - Arduino Retro Game

4 HASIL GAME

4.1 Hasil game Retro Snake

  1. Berikut adalah tampilan keseluruhan:

Figure 4.1 Tampilan Keseluruhan
  1. Berikut adalah tampilan ketika game akan segera di mulai.

Figure 4.2 Tampilan game Retro Snake
  1. Berikut adalah tampilan ketika game sedang dimainkan.

Figure 4.3 Tampilan game Retro Snake
  1. Berikut adalah tampilan ketika game sedang di pause.

Figure 4.4 Tampilan game Retro Snake
  1. Berikut tampilan Score ketika game berakhir.


Figure 4.5 Tampilan game Retro Snake 

BAB 5 Pengantar Teknologi Game - Arduino Retro Game

5 PENUTUP

5.1 Kesimpulan

Permainan Retro snake tidak perlu permainan berukuran besar, dengan grafis indah, dan menguras tenaga ponsel untuk menjadi permainan yang memorable. Permainan ular-ularan sederhana ala Arduino adalah buktinya. Meskipun permainan ini sangat sederhana dari segi struktur dan representasi, permainan ini berhasil menjadi bagian memorable dari sejarah penggunaan teknologi seseorang.
Arduino adalah pengendali mikro single-board yang bersifat open-source, dirancang untuk memudahkan penggunaan elektronik dalam berbagai bidang. Hardwarenya memiliki prosesor Atmel AVR dan softwarenya memiliki bahasa pemrograman sendiri. Arduino juga merupakan platform hardware terbuka yang ditujukan kepada siapa saja yang ingin membuat peralatan elektronik interaktif berdasarkan hardware dan software yang fleksibel dan mudah digunakan. Mikrokontroler diprogram menggunakan bahasa pemrograman arduino yang memiliki kemiripan syntax dengan bahasa pemrograman C. Karena sifatnya yang terbuka maka siapa saja dapat mengunduh skema hardware arduino dan membangunnya.
Dengan adanya Arduino membuat game dapat berinteraksi dengan pengguna. Arduino berguna untuk mengguhungkan antara game dengan komponen lcd untuk menampilkan Retro Snake, Analog untuk mengendalikan ular dan sebagai output untuk menampilkan skor ketika game berakhir.

DAFTAR PUSTAKA
  1. Bob Blomquist. 2016 Game Retro Snake Arduino
https://create.arduino.cc/projecthub/circuito-io-team/retro-nokia-snake-game-with-arduino-f8163b?ref=tag&ref_id=games&offset=2
Tanggal Akses: 17 Juli 2017
  1. Plimbi. 2015
http://www.plimbi.com/article/160134/adu-dua-game-snake-android-ala-nokia-lawas
Tanggal Akses: 17 Juli 2017
  1. http://tobuku.com/docs/Arduino-Pengenalan.pdf
Tanggal Akses: 20 Juli 2017
  1. Suprianto. 2015
http://blog.unnes.ac.id/antosupri/pengertian-cara-kerja-dan-fungsi-transistor/
Tanggal Akses: 17 Juli 2017
  1. http://www.dien-elcom.com/2012/08/pengertian-dan-jenis-transistor.html
Tanggal Akses: 17 Juli 2017
  1. http://teknikelektronika.com/pengertian-ic-integrated-circuit-aplikasi-fungsi-ic/
Tanggal Akses: 20 Juli 2017
  1. http://belajarelektronika.net/pengertian-dan-fungsi-ic-integrated-circuit/
Tanggal Akses: 17 Juli 2017
  1. Dickson Kho
http://teknikelektronika.com/kelebihan-keterbatasan-ic-integrated-circuit/
Tanggal Akses: 17 Juli 2017
  1. Suprianto. 2015
http://blog.unnes.ac.id/antosupri/pengertian-push-button-switch-saklar-tombol-tekan/
Tanggal Akses: 21Juli 2017
  1. Dickhson Kho. 2014
http://teknikelektronika.com/fungsi-pengertian-speaker-prinsip-kerja-speaker/\
Tanggal Akses: 17 Juli 2017
  1. https://id.wikipedia.org/wiki/Baterai_ion_litium
Tanggal Akses: 17 Juli 2017
  1. Fachrozy Aulia
http://fachrozyaulia.blogspot.co.id/2014/01/fungsi-jenis-jenis-dan-pengertian-dioda.html
Tanggal Akses: 17 Juli 2017
  1. Elektronika Dasar. 2012
http://elektronika-dasar.web.id/lcd-liquid-cristal-display/

Tanggal Akses: 17 Juli 2017 

BAB 3 Pengantar Teknologi Game - Arduino Retro Game

3 IMPLEMENTASI

Pada tahap ini akan dibuat implementasi dari game Retro Snake arduino

3.1 Penjelasan Hardware

3.1.1 Peralatan

Berikut adalah peralatan yang digunakan untuk membuat game Retro Snake dengan menggunakan Arduino.
  1. Arduino Mega 2560 & Genuino Mega 1560.
Arduino Uno adalah papan mikrokontroler yang berbasis pada ATmega328P ( datasheet ). Ini memiliki 14 pin input / output digital (yang 6 dapat digunakan sebagai output PWM), 6 input analog, kristal kuarsa 16 MHz, koneksi USB, colokan listrik, header ICSP dan tombol reset. Ini berisi semua yang dibutuhkan untuk mendukung mikrokontroler; Cukup hubungkan ke komputer dengan kabel USB atau nyalakan dengan adaptor AC-to-DC atau baterai untuk memulai .. Anda dapat bermain-main dengan UNO Anda tanpa terlalu banyak memikirkan tentang melakukan sesuatu yang salah, skenario terburuk Anda dapat mengganti Chip untuk beberapa dolar dan mulai lagi.

Figure 3.1 Arduino Mega 2560 & Genuino Mega 1560
Spesifikasi:
    • Mikrokontroler ATmega328P
    • Tegangan Operasi 5V
    • Tegangan Input (disarankan) 7-12V
    • Tegangan Input (batas) 6-20V
    • Pin Digital I / O 14 (dimana 6 memberikan output PWM)
    • PWM Digital I / O Pins 6
    • Pin input analog 6
    • Arus DC per I / O Pin 20 mA
    • Arus DC untuk Pin 3.3V 50 mA
    • Memori flash 32 KB (ATmega328P) dimana 0.5 KB digunakan oleh bootloader
    • SRAM 2 KB (ATmega328P)
    • EEPROM 1 KB (ATmega328P)
    • Kecepatan jam 16 MHz
    • LED_BUILTIN 13
    • Panjangnya 68,6 mm
    • Lebar 53,4 mm
    • Berat 25 g
  1. LCD 84x48 - Nokia 5110
Nokia 5110 adalah layar LCD grafis dasar untuk banyak aplikasi. Ini pada awalnya ditujukan untuk digunakan sebagai layar ponsel. Yang ini dipasang pada PCB yang solder mudah. Menggunakan kontroler PCD8544, yang sama digunakan di LCD Nokia 3310. PCD8544 adalah pengontrol / pengontrol LCD CMOS daya rendah, yang dirancang untuk menggerakkan tampilan grafik dari 48 baris dan 84 kolom. Semua fungsi yang diperlukan untuk display disediakan dalam satu chip, termasuk chip on-chip dari suplai LCD dan tegangan bias, sehingga menghasilkan komponen eksternal dan konsumsi daya rendah. Antarmuka PCD8544 ke mikrokontroler melalui antarmuka bus serial.

Figure 3.2 LCD 84X48 - Nokia 5110
Spesifikasi module ini yaitu :
    • LCD monochrome dengan ukuran 84 x 48 dot pixel
    • Dapat digunakan sebagai tampilan graphic, text maupun bitmaps.
    • Menggunakan backlight LED sebanyak 4 pcs warna biru
    • Resolusi tampilan / display resolution: 240 x 320 px
    • menggunakan chip driver PCD8544 tegangan 3.3V
    • Ukuran: 42 x 42mm (Board)
Pin Out

Figure 3.3 Pin Out
Berikut tabel keterangan wiring ke arduino

Figure 3.4 Wiring Arduino
Contoh pemasangan LCD 84x48 - Nokia 5110 pada Arduino Uno

Figure 3.5 Pemasangan LCD
  1. Analog Thumb
Tambahkan joystick kecil ke proyek Anda dengan thumbnail PSP ini. Ini adalah komponen pengganti PSP1000’s, tapi kami pikir mereka akan menghasilkan add-on joystick / thumbstick yang bagus untuk proyek yang dapat dipakai atau portabel. Joystick adalah tipe analog ’self-centering’. Itu berarti pada dasarnya bertindak seperti dua potensiometer 10 kk, satu untuk naik / turun dan satu lagi untuk kiri / kanan. Hubungkan satu pin VCC ke catu daya Anda (3 atau 5V yang terbaik) dan pin ground ke ground. Kemudian baca dua pin yang tersisa dengan pin analog masing-masing. Saat joystick digerakkan, voltase akan mengikuti gerakan. Saat Anda melepaskan joystick itu akan berpusat pada dirinya sendiri. Ada 4 bantalan di bagian bawah, Anda terbaik dari solder 4 kabel ke pin ini dan kemudian menempelkan semuanya ke bawah.

Figure 3.6 Analog Thumb
  1. Pushbutton
Tombol sesaat standar 12mm persegi. Yang sangat kami sukai adalah kepala tombol besar dan nuansa taktil yang bagus (klik ’sangat baik). Tombol ini sangat bagus untuk input pengguna pada PCB atau tombol reset besar yang bagus.

Figure 3.7 PushButton
  1. Mini Speaker PC Mount 12mm 2.048kHz
Ini adalah speaker bulat 12mm kecil yang beroperasi di sekitar rentang 2kHz terdengar. Anda bisa menggunakan speaker ini untuk membuat musik sederhana atau antarmuka pengguna. Ini bukan speaker piezoelektrik sejati tapi berperilaku sama. Alih-alih kristal piezoelektrik yang bergetar dengan arus listrik, speaker kecil ini menggunakan elektromagnet untuk menggerakkan lembaran logam tipis. Itu berarti Anda perlu menggunakan beberapa bentuk arus bolak-balik untuk mendapatkan suara. Kabar baiknya adalah bahwa speaker ini disetel untuk merespon terbaik dengan gelombang persegi (misal dari mikrokontroler).

Figure 3.8 Mini Speaker PC Mount 12mm 2.048kHz
  1. Lithium Ion Battery - 2200mAh 7.4v
LiPo dengan debit tinggi ini adalah cara yang bagus untuk menyalakan proyek R / C, robot, atau portabel. Ini adalah pilihan tepat untuk segala hal yang membutuhkan baterai kecil dengan banyak pukulan. Tegangannya cukup rendah untuk tidak mengenakan pajak pada sirkuit pengatur Anda, dan tingkat pelepasannya cukup tinggi untuk menampung banyak barang elektronik dan beberapa motor kecil. Baterai memiliki dua sel dan output 7.4V menyimpan 2200mAh charge. Karena ini adalah paket baterai sel dual, diperlukan charger khusus. Baterai ini tidak kompatibel dengan pengisi daya sel tunggal.

Figure 3.9 Lithium Ion Battery - 2200 mAH 7.4v
Spesifikasi :
    • 7.4V 2-cell pack
    • 2200mAh of charge
    • 30C continuous discharge rate
    • JST-XH charge plug
    • Dean’s Connector discharge leads
  1. Kode Sumber
 
#include <EEPROM.h>  
// Global variables and defines  
//define Nokia LCD contrast and dimentions(in pixels)  
#define LCD_CONTRAST 70  
#define LCD_SIZE_COL 84  
#define LCD_SIZE_ROW 48  
bool dl = false, dr = false, du = false, dd = false; 
// to check in which direction the snake is currently
moving  
uint8_t x[200], y[200], i, slength, tempx = 10, tempy
= 10, 
xx, yy;  unsigned int high; 
uint8_t bh, bl;
uint8_t xegg, yegg;  
int freq, tb;  
bool l, r, u, d, p; 
unsigned long time = 280, beeptime = 50;  
int score = 0, flag = 0; 
#include <EEPROM.h>  
// object initialization 
Adafruit_PCD8544 nokiaLcd(NOKIALCD_PIN_DC, 
NOKIALCD_PIN_CS, 
NOKIALCD_PIN_RST);  
Button pushButton(PUSHBUTTON_PIN_1); 
// Setup the essentials for your circuit to work. 
It runs first
every time your circuit is powered with electricity.  
void setup()
{   
//Serial.begin(9600);   
pinMode(JOYSTICKPSP_PIN_X, INPUT);   
pinMode(JOYSTICKPSP_PIN_Y, INPUT); 
 //Initialize Nokia instance   
nokiaLcd.begin(LCD_SIZE_COL, LCD_SIZE_ROW);   
nokiaLcd.clearDisplay();   
nokiaLcd.setContrast(LCD_CONTRAST); 
//Adjust display contrast   
pushButton.init();   
slength = 8; 
xegg = (nokiaLcd.width()) / 2;   
yegg = (nokiaLcd.height()) / 2;   
nokiaLcd.setTextSize(2);   
//Initial nokiaLcd   
nokiaLcd.setTextColor(BLACK);   
nokiaLcd.setCursor(10, 15);   
nokiaLcd.setCursor(10, 40); 
nokiaLcd.display();   
delay(4000);   
nokiaLcd.clearDisplay();   
for (i = 0; i <= slength; i++) //Set starting 
coordinates of snake   
{     
x[i] = 25 - 3 * i;    
y[i] = 10;   
} 
for (i = 0; i < slength; i++)  //Draw the snake   
{     
nokiaLcd.drawCircle(x[i], y[i], 1, BLACK);   
nokiaLcd.display();   
dr = true;  //Going to move right initially  
// Main logic of your circuit. It defines the 
interaction
between the components you selected. After setup, 
it runs over 
and over again, in an eternal loop.  void loop()  
{ 
movesnake();    
//This is called endlessly  
void movesnake()  
{   
int X_val = analogRead(JOYSTICKPSP_PIN_X);   
int Y_val = analogRead(JOYSTICKPSP_PIN_Y);   
l = X_val < 400 ? 1 : 0;   
d = X_val > 650 ? 1 : 0;   
r = Y_val < 400 ? 1 : 0;;   
u = Y_val >= 650 ? 1 : 0; 
p = pushButton.onPress();  
if (p == HIGH)            
//Pause game for 5 seconds   
{     
nokiaLcd.clearDisplay();    
nokiaLcd.setTextColor(BLACK);     
nokiaLcd.setCursor(25, 10);     
nokiaLcd.setTextSize(1);     
nokiaLcd.print("Pause");     
nokiaLcd.display();     
delay(1000); 
while (!pushButton.onPress()); 
   nokiaLcd.clearDisplay();     
redraw();         
//Redraw the snake and egg at the same position
as it was  
if (millis() % time == 0) 
//this condition becomes true after every ’time’
milliseconds...
millis() 
returns the time since launch of program   
{
direct();     
if (flag == 0) 
//flag 0 means no directional key has been pressed 
in the last 
’time’ milliseconds     
{      
if (dr == true)
{ 
 tempx = x[0] + 3;  
// so the snake moves one step in the direction
it is moving
currently    
    tempy = y[0];      
}       
if (dl == true)
{         
tempx = x[0] - 3; 
//The new coordinates of head of snake goes in 
tempx,tempy     
   tempy = y[0];     
}     
if (du == true) 
{         
tempy = y[0] - 3; 
tempx = x[0]; 
}       
if (dd == true) 
{         
tempy = y[0] + 3;         
tempx = x[0];       
}    
}     
flag = 0;    
checkgame();
 //Check if snake has met egg or coincided with
itself     
checkegg();     
if (tempx <= 0) { 
tempx = 84 + tempx; 
//If the new coordinates are out of screen, set
them accordingly     
}     
else if (tempx >= 84) 
{       
tempx = tempx - 84;    
}     
if (tempy <= 0) 
{       
tempy = 48 + tempy;     
}    
else if (tempy >= 48) 
{       
tempy = tempy - 48; 
}     
for (i = 0; i <= slength; i++)
//Change the coordinates of all points of snake     
{       
xx = x[i];       
yy = y[i];       
x[i] = tempx;      
y[i] = tempy;       
tempx = xx;       
tempy = yy;     
} 
 drawsnake();           
//Draw the snake and egg at the new coordinates   
void checkgame()      
//Game over checker  
for (i = 1; i < slength; i++)        
//Checking if the coordinates of head have become
equal to one 
of the non head points of snake  
{     
if (x[i] == x[0] && y[i] == y[0])    
{     
bh = EEPROM.read(1); 
bl = EEPROM.read(0);       
high = (((0xff00 + bh) << 8) + bl);       
if (score > high)      
{        
high = score;         
bh = (high >> 8);         
bl = high & 0xff;         
EEPROM.write(1, bh);         
EEPROM.write(0, bl);       
} 
nokiaLcd.clearDisplay();       
nokiaLcd.setTextColor(BLACK);       
nokiaLcd.setTextSize(1);       
nokiaLcd.setCursor(20, 12);       
nokiaLcd.print("Game Over");       
nokiaLcd.setCursor(15, 30);       
nokiaLcd.print("Score: ");      
nokiaLcd.print(score);      
nokiaLcd.setCursor(15, 40);       
nokiaLcd.print("High: "); 
nokiaLcd.print(high);       
nokiaLcd.display();       
beep(20, 5000);      
nokiaLcd.clearDisplay();       
slength = 8;            
//Resetting the values       score = 0;       
time = 280;       
redraw();             
//Restart game by drawing snake with the 
resetted length and score     
} 
}
void checkegg()      //Snake meets egg 
{   
if (x[0] == xegg or x[0] == (xegg + 1) or 
x[0] == (xegg + 2)or x[0] == (xegg - 1)) 
//Snake in close vicinity of egg   
{     
if (y[0] == yegg or y[0] == (yegg + 1) or
y[0] == (yegg + 2) or y[0] == (yegg - 1))     
{       
score += 1;                    
//Increase length,score and increase 
movement speed by decreasing 
’time’      
slength += 1;       
if (time >= 90)
{         
time -= 5;       
}       
nokiaLcd.fillRect(xegg, yegg, 3, 3, WHITE);  
//Delete the consumed egg       
nokiaLcd.display();       
beep(35, beeptime);  
//Beep with a sound of 35Hz for ’beeptime’ ms       
xegg = random(1, 80); 
//Create New egg randomly       
yegg = random(1, 40);    
} 
} 
void direct() 
//Check if user pressed any keys and change
direction if so  
{   
if (l == HIGH and dr == false)  
//when key LEFT is pressed ,L will become low   
{    
dl = true; du = false; dd = false;     
tempx = x[0] - 3;  
//Save the new coordinates of head in tempx,
tempy     
tempy = y[0];     
flag = 1;  
//Do not change direction any further for 
the ongoing ’time’
milliseconds   
} 
else if (r == HIGH and dl == false)   
{     
dr = true; du = false; dd = false;     
tempx = x[0] + 3;     
tempy = y[0];    
flag = 1;   
}   
else if (u == HIGH and dd == false)  
{    
du = true; dl = false; dr = false; 
tempy = y[0] - 3;     
tempx = x[0];     
flag = 1;  
}   
else if (d == HIGH and du == false)   
{    
dd = true; dl = false; dr = false;     
tempy = y[0] + 3;    
tempx = x[0];    
flag = 1; 
 }  
else if (p == HIGH)            
//Pause game for 5 seconds  
{    
nokiaLcd.clearDisplay();     
nokiaLcd.setTextColor(BLACK);    
nokiaLcd.setCursor(25, 10);    
nokiaLcd.setTextSize(1);    
nokiaLcd.print("Pause");    
nokiaLcd.display();     
delay(1000); 
while (!pushButton.onPress());     
nokiaLcd.clearDisplay();    
redraw();   
//Redraw the snake and egg at the same 
position as it was  
void drawsnake()        
//Draw snake and egg at newly changed positions  
{   
nokiaLcd.fillRect(xegg, yegg, 3, 3, BLACK);   
//Draw egg at new pos   
nokiaLcd.drawCircle(x[0], y[0], 1, BLACK);    
//Draw new head of snake   
nokiaLcd.drawCircle(x[slength], y[slength],
1, WHITE); 
//Delete old tail of snake  
nokiaLcd.display();  
void redraw()   
//Redraw ALL POINTS of snake and egg  
{   
nokiaLcd.fillRect(xegg, yegg, 3, 3, BLACK);   
for (i = 0; i < slength; i++)  
{     
nokiaLcd.drawCircle(x[i], y[i], 1, BLACK);   
}   
nokiaLcd.display(); 
void beep (int freq, long tb)          
//This function creates a sound of 
frequency ’freq’ 
Hz and for a duration of ’tb’ milliseconds  
{   
int x;   
long delayAmount = (long)(500 / freq);     
//Calculate time duration of half cycle   
long loopTime = (long)(tb / (delayAmount * 2)); 
//Calculate no. of cycles   
for (x = 0; x < loopTime; x++) 
// One iteration produces sound for one cycle   
{     
digitalWrite(PIEZOSPEAKER_PIN_SIG, HIGH);  
//High for half cycle     
delay(delayAmount); 
digitalWrite(PIEZOSPEAKER_PIN_SIG, LOW);   
//Low for half cycle    
delay(delayAmount);   
}   
delay(2);   
//a little delay to make all notes sound separate