#include <gamekit.h>
long denominator = 172373;
long absolute = 572665;
long multiplier = 1146;
long summand = 568781;
//Tetris melody
int melody[112][2] = {{2,17},{1,12},{1,13},{2,15},{1,13},{1,12} ,{2,10},{1,10},{1,13},{2,17},{1,15},{1,13}, {3,12},{1,13},{2,15},{2,17} ,{2,13},{2,10},{2,10},{2,10} ,{1,0},{2,15},{1,18},{2,22},{1,20},{1,18} ,{3,17},{1,13},{2,17},{1,15},{1,13} ,{2,12},{1,12},{1,13},{2,15},{2,17} ,{2,13},{2,10},{2,10},{2,0}, {2,17},{1,12},{1,13},{2,15},{1,13},{1,12} ,{2,10},{1,10},{1,13},{2,17},{1,15},{1,13}, {3,12},{1,13},{2,15},{2,17} ,{2,13},{2,10},{2,10},{2,0} ,{1,0},{2,15},{1,18},{2,22},{1,20},{1,18} ,{3,17},{1,13},{2,17},{1,15},{1,13} ,{2,12},{1,12},{1,13},{2,15},{2,17} ,{2,13},{2,10},{2,10},{2,0} ,{2,5},{2,5},{2,1},{2,1} ,{2,3},{2,3},{2,-1},{2,-1} ,{2,1},{2,1},{2,-3},{2,-3}, {2,-4},{2,-4},{2,-1},{2,0}, {2,5},{2,5},{2,1},{2,1}, {2,3},{2,3},{2,-1},{2,-1}, {2,1},{2,5},{2,10},{2,10}, {2,9},{2,9},{2,0},{2,0} };
int melody_length = 111;
int melody_position = 0;
int melody_speed = 180;
int sound_volume = 1;
long Timer = 0;
int GamePhase = 1; //1=Selection, 2=Falling Sequence, 3=Death
int Difficulty = 0; //0=easy 3=Hard
int Millis_per_Step = 1200;
int Foods_per_Grow = 1;
int Snake_Length_To_Win = 10;
int Field[10][14];
int pos_x = 1; //Position of upper left pixel
int pos_y = 1;
// {y,x,a} a=0 -> no snake a=10 -> snake
int Snake[30][3];
int Head = 0;
int Ass = 1; //The last snake field at the end
int Direct = 2; //Snake walks (1)south, (2)east, (3)north, (4)west
int Eaten_Food = 0;
int Snake_Length = 2;
int CurLvl = 1;
void setup(){
gamekit.Begin();
Serial.begin(9600);
}
//---------------------------------------------------
// Random Number Generator
//---------------------------------------------------
double generate_rand_double() {
absolute = (multiplierabsolute+summand+(10gamekit.get_systemcounter()%denominator) )%denominator;
return float(absolute)/denominator;
}
//---------------------------------------------------
// Melody
//---------------------------------------------------
void Load_Melody(int No_Of_Melody) {
switch (No_Of_Melody) {
case 1:
break;
case 2:
//do something when var equals 2
break;
}
}
void Play_Next_Melody_Tone() {
play_tone(melody[melody_position][1], melody[melody_position][0]*melody_speed, sound_volume);
melody_position++;
if (melody_position>melody_length) {
melody_position=0;
}
}
void play_frequency(int frequency_, int duration_, int loudness_){
//loudness: 0=no sound, 1=silent, 2=loud
if (loudness_==0) {
//do nothing but delay
delay(duration_);
} else {
if (loudness_==1) {
gamekit.play_tone(frequency_, duration_, 0);
} else {
gamekit.play_tone(frequency_, duration_, 1);
}
}
}
void play_tone(int distance_to_C_, int duration_, int loudness_){
if (distance_to_C_!=0) {
play_frequency(246 * pow(float(2), float(distance_to_C_)/12.0), duration_, loudness_);
} else {
play_frequency(264, duration_, 0);
}
}
void Do_Background_Stuff() {
Play_Next_Melody_Tone();
Read_Keys();
}
//---------------------------------------------------
// The Game
//---------------------------------------------------
void Clear_Screen() {
for (int Counter_y=0; Counter_y<=4; Counter_y++) {
for (int Counter_x=0; Counter_x<=7; Counter_x++) {
gamekit.set_pixel(Counter_y, Counter_x, 0);
}
}
}
void Load_Level_1_1() {
int Field_Level_1[10][14] = { {15,15,15,15,15,15,15,15,15,15,15,15,15,15} , // 0=free 15=wall 17=food
{15,0,0,0,0,0,0,0,0,0,0,0,0,15} ,
{15,0,0,0,0,0,0,17,0,0,0,0,0,15} ,
{15,0,0,0,0,0,0,0,0,0,0,0,0,15} ,
{15,0,0,0,0,0,15,15,0,0,0,0,0,15} ,
{15,0,0,0,0,0,15,15,0,0,0,0,0,15} ,
{15,0,0,17,0,0,0,0,0,17,0,0,0,15} ,
{15,0,0,0,0,0,0,0,0,0,0,0,0,15} ,
{15,0,0,0,0,0,0,0,0,0,0,0,0,15} ,
{15,15,15,15,15,15,15,15,15,15,15,15,15,15} };
for (int Counter_y=0; Counter_y<10; Counter_y++) {
for (int Counter_x=0; Counter_x<14; Counter_x++) {
Field[Counter_y][Counter_x] = Field_Level_1[Counter_y][Counter_x];
}
}
}
void Load_Level_1_2() {
int Snake_Level_1[30][3] = { {3,3,5}, {3,2,5}, {3,1,0}, {2,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0} ,{3,3,0}};
for (int Counter_y=0; Counter_y<30; Counter_y++) {
for (int Counter_x=0; Counter_x<3; Counter_x++) {
Snake[Counter_y][Counter_x] = Snake_Level_1[Counter_y][Counter_x];
}
}
pos_x = 1; //Position of upper left pixel
pos_y = 1;
Head = 0;
Ass = 1; //The last snake field at the end
Direct = 2; //Snake walks (1)south, (2)east, (3)north, (4)west
Eaten_Food = 0;
Snake_Length = 2;
}
void Load_Level_2_1() {
int Field_Level_1[10][14] = { {15,15,15,15,15,15,15,15,15,15,15,15,15,15} , // 0=free 15=wall 17=food
{15,0,0,0,0,0,0,0,0,0,0,0,0,15} ,
{15,0,0,0,0,0,0,17,0,0,0,0,0,15} ,
{15,0,0,0,0,0,0,0,0,0,0,0,0,15} ,
{15,0,0,0,15,15,0,0,15,15,0,0,0,15} ,
{15,0,0,0,15,15,0,0,15,15,0,0,0,15} ,
{15,0,0,17,0,0,0,0,0,17,0,0,0,15} ,
{15,0,0,0,0,0,0,0,0,0,0,0,0,15} ,
{15,0,0,0,0,0,0,0,0,0,0,0,0,15} ,
{15,15,15,15,15,15,15,15,15,15,15,15,15,15} };
for (int Counter_y=0; Counter_y<10; Counter_y++) {
for (int Counter_x=0; Counter_x<14; Counter_x++) {
Field[Counter_y][Counter_x] = Field_Level_1[Counter_y][Counter_x];
}
}
}