
Arduino İle Space Game Oyunu – 1. Versiyon
Videoda görmüş olduğunuz oyunun arduino ile nasıl yapıldığını göstermek anlatacağım. Haydi başlayalım :d
Gerekli Malzemeler

- Arduino Uno
- Joystick Modülü
- Jumper Kabloları
- Breadboard
Şema

Şemadaki jumper kabloların bağlantıları:
VCC -> Arduino 5V
BL -> Arduino pin 3
GND -> Arduino GND
CLK (SCLK) -> Arduino pin 8
DIN (MOSI) -> Arduino pin 9
DC -> Arduino pin 10
CE or CS -> Arduino pin 12
RST (RESET) -> Arduino pin 11
Kod

Açıklamaları yabancı arkadaşlarım için ingilizce yapmıştım. Sizlerin de bu iş ile ilgilenmenizden ötürü az da olsa ingilizce bildiğinizi varsayarak türkçeye çevirmeye uğraşmadım. Kod ile alakalı herhangi bir sorunuz olursa çekinmeden sorabilirsiniz.
#include <lcd5110_graph.h>
LCD5110 lcd(8,9,10,11,12); //(clk,din,dc,rst,ce)</p><p>int BL=3;
extern uint8_t SmallFont[]; //describe the fonts
extern uint8_t MediumNumbers[];
extern uint8_t arac[]; //describe our shape
extern uint8_t dusman_araci[]; //desribe enemy shape
const int xpin=A0; //joystick x pin
char pozisyon=2; //describe the location, our shape start location 2
boolean kontrol = true;
boolean dusmanOldu = true;
int skor=0;
int hiz=0;
int dusmanKonumu1; //describe enemy1 x location
int dusmanKonumu2; //describe enemy2 x location
int dusmaninYdeKonumu; //describe enemies y location</p><p> void dusmanAraci(int yer, int asama ){ //set locations for enemy shapes
if (yer==1){ //if location=1;
lcd.drawBitmap(2 , asama, dusman_araci, 12, 8); //draw enemy shape
}
if (yer==2){
lcd.drawBitmap(18, asama, dusman_araci, 12, 8);
}
if (yer==3){
lcd.drawBitmap(34, asama, dusman_araci, 12, 8);
}}</p><p> void oyunBitti() { //if we failure, the game over
while(1) {
delay(100); //wait 1 milisecond
lcd.clrScr(); //clear the screen
lcd.setFont(SmallFont); //set font
lcd.print("GAME OVER",CENTER,20); //print
lcd.update(); //update the screen
}}</p><p>
void setup() {
pinMode(BL,OUTPUT); //set back light Output
lcd.InitLCD(); //initilaze the lcd screen
lcd.setContrast(55); //set contrast(amound 0 to 127)
Serial.begin(9600); //start serial comunation
}</p><p>void loop() {
analogWrite(BL,200);</p><p>
//set game screen
lcd.drawRect(0,0,47,47); //we draw rectangle
lcd.setFont(SmallFont); //set font
lcd.print("skor",53,0); //printf score
lcd.setFont(MediumNumbers); //set font
lcd.printNumI(skor,59,7); //get score
lcd.setFont(SmallFont);
lcd.print("hiz",56,25); //print speed
lcd.setFont(MediumNumbers);
lcd.printNumI(hiz,59,32); //get speed
lcd.update(); //update</p><p>
//set the joyistick location
int x=analogRead(xpin);
if(x<300 && pozisyon!=1 && kontrol==true ){ //if location!=1,x state<300 and control is true
pozisyon--; //decrease the location it means move shape to the left
kontrol=false; //control false for another movement
}
else if(x>600 && pozisyon!=3 && kontrol==true){ //if location!=3,x state>600 and control is true
pozisyon++; //increase the location it means move shape to the right
kontrol=false;
}
else if(x>300 && x<600 ){
kontrol=true;
}
Serial.println(x); //for learning x state
//set the our shape location
lcd.clrScr(); //clear the screen
if (pozisyon==1){ //if location=1;
lcd.drawBitmap(2, 37, arac, 12, 8);} //draw our ship
if (pozisyon==2){
lcd.drawBitmap(18, 37, arac, 12, 8);}</p><p> if (pozisyon==3){
lcd.drawBitmap(34, 37, arac, 12, 8);}</p><p> if (dusmanOldu){ //if enemy shape is dead,check are they dead
dusmanKonumu1 = pozisyon; //draw first enemy shape
dusmanKonumu2 = random(0,4); //draw another enemy shape somewhere
dusmaninYdeKonumu = 0; //bring enemy from the top
dusmanOldu = false;} //enemy is recreated so they are not dead
dusmanAraci (dusmanKonumu1,dusmaninYdeKonumu); dusmaninYdeKonumu++; //draw first enemy shape and get it from top to bottom
dusmanAraci (dusmanKonumu2,dusmaninYdeKonumu); dusmaninYdeKonumu++; //draw second enemy shape and get it from top to bottom
if (dusmaninYdeKonumu>22 && ((dusmanKonumu1 == pozisyon) || (dusmanKonumu2 == pozisyon)) ){ //if our shape touch enemy shapes
oyunBitti();} //the game is over
if (dusmaninYdeKonumu>40){ //if our shape escape from enemies
dusmanOldu = true; //kill enemy shapes
skor++;} //increase one by one the score
//increase speed according to score
if (skor>=0 && skor<=10){ //if score is amoung 0 to 10
hiz = 0; delay(80); //the game speed is slow 80ms
}
if (skor>10 && skor<=20){ //if score is amoung 10 to 20
hiz = 1; delay(65); //increase speed to 65ms and increase speed to 1
}
if (skor>20 && skor<=30){
hiz = 2; delay(50);
}
if (skor>30 && skor<=40){
hiz = 3; delay(40);
}
}
BITMAP Kodu
Bıtmap kodunu şekiller için kullanıyoruz. Videoda da göründüğü gibi dörtgen şekil kullandım. Bitmap kodunu Arduino kodunuzun bulunduğu dosyaya atmayı ihmal etmeyiniz. Ayrıca Bitmap dosyası ile ilgili daha fazla bilgi almak için Arduino – LCD5110 Grafik Eğitimi-Kursu yazımı inceleyebilirsiniz.
//include for progmem function
#include <avr pgmspace.h=""></avr>
//our shape bitmap
const unsigned char arac []PROGMEM = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
//enemy ship bitmap
const unsigned char dusman_araci []PROGMEM = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
Yazarımızın diğer yazılarını incelemek için tıklayınız…

