|
/* |
|
* From Led-Matrix |
|
* CLK -> Pin 11 |
|
* CS -> Pin 10 |
|
* DIN -> Pin 12 |
|
* GND -> GND |
|
* VCC -> 5V |
|
* |
|
* From Joystick: |
|
* S-Y -> A1 |
|
* S-X -> A0 |
|
* S-K -> Reset |
|
* GND for button -> A5 |
|
* GND(X/Y) -> GND |
|
* VCC(X/Y -> 3V |
|
*/ |
|
#include "LedControlMS.h" |
|
#include <math.h> |
|
#include <stdlib.h> |
|
|
|
#define NBR_MTX 2 |
|
LedControl lc=LedControl(12,11,10, NBR_MTX); //eğer ihtiyaç duyarsanız pinleri değiştirebilirsiniz. |
|
|
|
//0dan 9a kadar olan numaraların sayı sistemlerinde ki karşılıkları |
|
int n[11][8][8] = {{ |
|
{0,0,0,0,0,0,0,0}, |
|
{0,1,1,0,0,0,0,0}, |
|
{1,0,0,1,0,0,0,0}, |
|
{1,0,0,1,0,0,0,0}, |
|
{1,0,0,1,0,0,0,0}, |
|
{1,0,0,1,0,0,0,0}, |
|
{0,1,1,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0} |
|
}, |
|
{ |
|
{0,0,0,0,0,0,0,0}, |
|
{0,0,1,0,0,0,0,0}, |
|
{0,1,1,0,0,0,0,0}, |
|
{0,0,1,0,0,0,0,0}, |
|
{0,0,1,0,0,0,0,0}, |
|
{0,0,1,0,0,0,0,0}, |
|
{0,1,1,1,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0} |
|
}, |
|
{ |
|
|
|
{0,0,0,0,0,0,0,0}, |
|
{0,1,1,0,0,0,0,0}, |
|
{1,0,0,1,0,0,0,0}, |
|
{0,0,0,1,0,0,0,0}, |
|
{0,0,1,0,0,0,0,0}, |
|
{0,1,0,0,0,0,0,0}, |
|
{1,1,1,1,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0} |
|
}, |
|
{ |
|
|
|
{0,0,0,0,0,0,0,0}, |
|
{0,1,1,0,0,0,0,0}, |
|
{1,0,0,1,0,0,0,0}, |
|
{0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,1,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0} |
|
}, |
|
{ |
|
|
|
{0,0,0,0,0,0,0,0}, |
|
{0,1,1,0,0,0,0,0}, |
|
{1,0,0,1,0,0,0,0}, |
|
{1,0,0,1,0,0,0,0}, |
|
{0,1,1,1,0,0,0,0}, |
|
{0,0,0,1,0,0,0,0}, |
|
{0,1,1,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0} |
|
}, |
|
{ |
|
|
|
{0,0,0,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0}, |
|
{0,0,0,0,0,0,0,0} |
|
}}; |
|
|
|
|
|
const int LRin = A0; //sol/sağ |
|
const int UDin = A1; //yukarı/aşağı |
|
|
|
bool blinkCandy = true; //her döngüde yanıp sönen şekerler. |
|
int moveDir = 0; //0 = sağ, 1 = yukarı, 2 = sol, 3 = aşağı. |
|
int currMoveDir = 0;//Used to keep track of actual current dir, not just last set during a cycle |
|
int length = 1; //başlama uzunluğu |
|
int last[64][2]; //bir yılan için 64 ledden kullanılacak 2 led |
|
int x = 2; //başlangıç pos |
|
int y = 2; //bağlangıç pos |
|
int lastId = 0; |
|
bool collision = false; //çarpmış durumu olunca |
|
bool incLength = false; |
|
int candyPos[2]; //şekerin pozisyonunu tutar |
|
bool candyOnScreen = false; |
|
int speed = 250; |
|
|
|
void setup() { |
|
Serial.begin (9600); |
|
for(int i = 0; i < 64; i++){ //ekran dışındaki tüm konumları ayarla. |
|
last[i][0] = –1; |
|
last[i][1] = –1; |
|
} |
|
|
|
//Set up display |
|
for (int i=0; i< NBR_MTX; i++){ |
|
lc.shutdown(i,false); |
|
/* parlaklığın orta değerlerde ayarlanması */ |
|
lc.setIntensity(i,10); |
|
/* ekranın temizlenmesi */ |
|
lc.clearDisplay(i); |
|
} |
|
|
|
} |
|
|
|
//kronometre. her döngüde kullanıcı girişini kontrol et, her 150. döngüde görüntü oluştur |
|
int timer = 0; |
|
int looper=0; |
|
|
|
void loop() { |
|
|
|
looper++; |
|
looper = looper % speed; |
|
CheckMove(); |
|
if(looper==0) DoMove(); |
|
if(looper==0) checkCollision(); |
|
if(collision) { |
|
GameOver(); |
|
} |
|
|
|
candyCollision(); //Şeker yendiyse kontrol et |
|
|
|
while(!candyOnScreen) createCandy(); //Yeni şeker pazisyonunu random getir. Geçerli bir konum ayarlanana kadar devam et |
|
|
|
if(looper % 100 == 0) candyBlink(); |
|
delay(1); |
|
} |
|
|
|
/** |
|
* |
|
* Oyun bitti. Sonsuz döngüde sıfırlanana kadar skoru yanıp söner. |
|
* |
|
*/ |
|
void GameOver(void){ |
|
while(1){ |
|
lc.clearDisplay(0); |
|
delay(100); |
|
PS(length-1); |
|
delay(500); |
|
} |
|
} |
|
|
|
/** |
|
* Skoru ekrana yaz. |
|
*/ |
|
void PS(int a){ |
|
int nr1; |
|
int nr2; |
|
if(a > 10){ |
|
nr1 = a / 10; |
|
nr2 = a – (nr1 * 10); |
|
} |
|
else if(a==10){ |
|
nr1 = 1; |
|
nr2 = 0; |
|
} |
|
else{ |
|
digit(a, 2); |
|
return; |
|
|
|
} |
|
digit(nr1, 0); |
|
digit(nr2, 4); |
|
} |
|
|
|
/* |
|
* Ekrana rakam bas. |
|
*/ |
|
void digit(int nr1, int offset){ |
|
for(int x1 = 0; x1 < 8; x1++){ |
|
for(int y1 = 0; y1 < 8; y1++){ |
|
if(n[nr1][x1][y1] == 1) lc.setLed(0,y1+offset,7-x1,true); |
|
} |
|
} |
|
} |
|
|
|
/*Blink the candy*/ |
|
void candyBlink(void){ |
|
if(blinkCandy) lc.setLed(0,candyPos[0],candyPos[1],true); |
|
else lc.setLed(0,candyPos[0],candyPos[1],false); |
|
blinkCandy = !blinkCandy; |
|
} |
|
/* |
|
* Ledi yak, pozisyonda (x,y) |
|
*/ |
|
void Led(int x, int y){ |
|
if(!incLength){ |
|
//Kuyrukta son ışını takip et, diziyi yeniden düzenleyelim böylece yeni kuyruk dizin 0'da. |
|
lc.setLed(0,last[0][0], last[0][1],false); |
|
for(int i = 0; i < length; i++){ |
|
last[i][0] = last[i+1][0]; |
|
last[i][1] = last[i+1][1]; |
|
} |
|
|
|
} |
|
else{ |
|
length++; |
|
incLength = false; |
|
} |
|
|
|
last[length][0] = x; |
|
last[length][1] = y; |
|
|
|
lc.setLed(0,last[length][0],last[length][1],true); // Başın bir sonraki pozisyonu için yanar. |
|
} |
|
|
|
/*Joystick'ten girişi oku*/ |
|
void CheckMove(void){ |
|
|
|
int LR = analogRead(LRin); |
|
int UD = analogRead(UDin); |
|
|
|
if(LR > 600 && currMoveDir != 0) moveDir = 2; //sağa git, yılan sol hareket etmiyorsa |
|
if(LR < 100 && currMoveDir != 2) moveDir = 0; //sola git, eğer yılan sağa hareket etmiyorda |
|
if(UD > 600 && currMoveDir != 1) moveDir = 3; //yukarı git, eğer yılan aşağı gitmiyorsa |
|
if(UD < 100 && currMoveDir != 3) moveDir = 1; //aşağı git, eğer yılan yukarı gitmiyorsa |
|
} |
|
|
|
/* |
|
* yılanın temel aldığı currentMoveDir |
|
*/ |
|
void DoMove(void){ |
|
currMoveDir = moveDir; |
|
if(moveDir == 0) MoveRight(); |
|
if(moveDir == 1) MoveUp(); |
|
if(moveDir == 2) MoveLeft(); |
|
if(moveDir == 3) MoveDown(); |
|
Led(x,y); |
|
} |
|
|
|
/*Artış uzunluğu*/ |
|
void IncrementLength(void){ |
|
incLength = true; |
|
} |
|
|
|
void MoveLeft(void){ |
|
x++; |
|
} |
|
void MoveRight(void){ |
|
x–; |
|
} |
|
void MoveDown(void){ |
|
y–; |
|
} |
|
void MoveUp(void){ |
|
y++; |
|
} |
|
|
|
/*şeker yarat*/ |
|
void createCandy(void){ |
|
|
|
int xR = rand() % 8; |
|
int yR = rand() % 8; |
|
|
|
for(int i = 0; i <= length; i++){ |
|
if(xR == last[i][0] && yR == last[i][1]){ |
|
candyOnScreen = false; |
|
return; |
|
} |
|
} |
|
candyPos[0] = xR; |
|
candyPos[1] = yR; |
|
candyOnScreen = true; |
|
lc.setLed(0,xR,yR,true); |
|
} |
|
|
|
/*Şekerle çarpışmayı kontrol et*/ |
|
void candyCollision(){ |
|
if(last[length][0] == candyPos[0] && last[length][1] == candyPos[1]){ |
|
candyOnScreen = false; |
|
IncrementLength(); |
|
} |
|
} |
|
|
|
|
|
/*Yılan ve duvarla çarpışmayı kontrol eder. */ |
|
void checkCollision(){ |
|
int xP = last[length][0]; |
|
int yP = last[length][1]; |
|
|
|
if(xP == –1 || xP == 8 || yP == –1 || yP == 8){ |
|
collision = true; |
|
return; |
|
} |
|
else{ |
|
for(int i = 0; i < length; i++){ |
|
if(last[i][0] == xP && last[i][1] == yP){ |
|
collision = true; |
|
return; |
|
} |
|
} |
|
} |
|
} |
#include “LedControlMS.h” kısmı yanlış diyor