Space Race Game Version 2
by Azmi Deliaslan in Circuits > Arduino
771 Views, 4 Favorites, 0 Comments
Space Race Game Version 2
Hey everyone. Before this game version , ı published first version of game. Today, ı will show you Space Race Game version 2. Let's look steps...
PARTS
Here is a list of parts that i used to make this project:
- Arduino UNO
- Joystick Module
- Jumper Wires (male to male and male to female)
- Breadboard
SCHEMA
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
CODE
The code has explanations of code functions. You will easly understand it. If you have a problem , you can contact me.
#include
LCD5110 lcd(8,9,10,11,12); //(clk,cin,dc,,rst,ce)
boolean kontrol=true; boolean dusmanoldu=true;
extern uint8_t SmallFont[]; //describe the fonts extern uint8_t MediumNumbers[]; extern uint8_t arac[]; //describe our shape extern uint8_t dusmanAraci[]; //desribe enemy shape const int xpin=A0; //joystick x pin char pozisyon=3; //describe the location, our shape start location 3 int BL=3; int skor=0; int hiz=0; int dusmankonum1; //describe enemy1 x location int dusmankonum2; //describe enemy2 x location int dusmankonum3; //describe enemy3 x location int dusmankonum4; //describe enemy4 x location int dusmankonum5; //describe enemy5 x location int dusmaninYdekonumu;
void oyunEkrani(){ //set game screen lcd.clrScr(); lcd.drawRect(0,0,47,47); //we draw rectangle lcd.setFont(SmallFont); //set font lcd.print("skor",57,1); //print score lcd.print("hiz",60,24); //print speed lcd.setFont(MediumNumbers); //set font lcd.printNumI(skor,55,8); //get score lcd.printNumI(hiz,63,32); //get speed lcd.update(); }
void dusmanAracidurum(int yer,int asama){ //set locations for enemy shapes if(yer==1){ lcd.drawBitmap(2,asama,dusmanAraci,7,7);lcd.update();}//if location=1 draw enemy shape this location if(yer==2){ lcd.drawBitmap(10,asama,dusmanAraci,7,7);lcd.update();} if(yer==3){ lcd.drawBitmap(18,asama,dusmanAraci,7,7);lcd.update();} if(yer==4){ lcd.drawBitmap(26,asama,dusmanAraci,7,7);lcd.update();} if(yer==5){ lcd.drawBitmap(34,asama,dusmanAraci,7,7);lcd.update();} if(yer==6){ lcd.drawBitmap(42,asama,dusmanAraci,7,7);lcd.update();} }
void oyunbitti(){ //if we failure, the game over while(1){ delay(100); //wait 1 milisecond lcd.clrScr(); lcd.setFont(SmallFont); lcd.print("OYUN BITTI",CENTER,8); //print game over lcd.print("skorunuz=",3,30); //print your score lcd.setFont(MediumNumbers); lcd.printNumI(skor,60,25); //get last score lcd.update(); } }
void setup() { pinMode(BL,OUTPUT); //set back light Output pinMode(xpin,INPUT); //set x pin as an input lcd.InitLCD(); //initilaze the lcd screen lcd.setContrast(55); //set contrast(amound 0 to 127) Serial.begin(9600); //start serial comunation }
void loop() { analogWrite(BL,350); oyunEkrani();
//joyistic durumu... //set the joyistick location... int durumx=analogRead(xpin); if(durumx <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(durumx >600 && pozisyon!=6 && 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(durumx >300 && durumx <600){ kontrol=true; } //pozisyon durumu... //position state... if(pozisyon==1){ //if location=1; lcd.drawBitmap(2,41,arac,7,7);lcd.update();}//draw our ship if(pozisyon==2){ lcd.drawBitmap(10,41,arac,7,7);lcd.update();} if(pozisyon==3){ lcd.drawBitmap(18,41,arac,7,7);lcd.update();} if(pozisyon==4){ lcd.drawBitmap(26,41,arac,7,7);lcd.update();} if(pozisyon==5){ lcd.drawBitmap(34,41,arac,7,7);lcd.update();} if(pozisyon==6){ lcd.drawBitmap(42,41,arac,7,7);lcd.update();}
if(dusmanoldu){ //if enemy shape is dead,check are they dead dusmankonum1=pozisyon; //draw first enemy shape dusmankonum2=random(0,6);//draw another enemy shape somewhere dusmankonum3=random(0,6); dusmankonum4=random(0,6); dusmankonum5=random(0,6); dusmaninYdekonumu=0; //bring enemy from the top dusmanoldu=false;} //enemy is recreated so they are not dead
dusmanAracidurum(dusmankonum1,dusmaninYdekonumu);dusmaninYdekonumu++; //draw first enemy shape and get it from top to bottom dusmanAracidurum(dusmankonum2,dusmaninYdekonumu);dusmaninYdekonumu++; //draw second enemy shape and get it from top to bottom dusmanAracidurum(dusmankonum3,dusmaninYdekonumu);dusmaninYdekonumu++; dusmanAracidurum(dusmankonum4,dusmaninYdekonumu);dusmaninYdekonumu++; if(dusmaninYdekonumu>35 && ((dusmankonum1 == pozisyon)||(dusmankonum2 == pozisyon)||(dusmankonum3 == pozisyon)||(dusmankonum4 == pozisyon)) ){ //if our shape touch enemy shapes oyunbitti(); //the game is over }
if(dusmaninYdekonumu >42){//if our shape escape from enemies dusmanoldu = true; //kill enemy shapes skor++; //increase one by one the score }
//skora gore hizi artiriyoruz //increase speed according to score if (skor>=0 && skor<10){ //if score is amoung 0 to 10 hiz = 1; delay(70); //the game speed is slow 70ms } else if (skor>=10 && skor<20){ //if score is amoung 10 to 20 hiz = 2; delay(60); //increase speed to 65ms and increase speed to 2 } else if (skor>=20 && skor<30){ hiz = 3; delay(50); } else if (skor>=30 && skor<40){ hiz = 4; delay(40); } else if (skor>=40 && skor<50){ hiz=5; delay(25); } else if (skor>=50){ hiz=6; delay(5); }
}
CODE BITMAP
Furthermore, you have to include bitmap graphic for shapes.It must be a .c file.
#include //include for progmem function
const unsigned char arac []PROGMEM = { //our shape bitmap 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, };
const unsigned char dusmanAraci []PROGMEM = { //enemy ship bitmap 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, };
Thanks for Viewing
Meaning of some words :
Oyun => Game
Arac = > Shape
Dusman => Enemy
kontrol = >control
Konum,Yer => Location
Oyun Bitti => Game Over
Skor => Score
Hiz => Speed