
Kodlamayı en sevdiğim C++ ile neler yapabilirim diye düşünürken internette okuduğum bir yazıdak etkilenerek bir yılan oyunu yapmaya karar verdim. getch() fonksiyonunu kullanarak klavye tıklanma olaylarını algılayıp yılana yön verdirmeye çalıştım. windows.h kütüphanesi sayesinde her saniye ekran silinip yılan yeni konumuyla beraber ekrana veriliyor. Ekran çıktıları ve kod’u aşağıda bulabilirsiniz.
Ekran resimleri ve Program Kodu
Program kodu;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
#include<iostream> #include<windows.h> #include<conio.h> using namespace std; bool gameOver; const int widht=20; const int height=20; int x,y,fruitX,fruitY,score; enum eDirection {STOP=0,LEFT,RIGHT,UP,DOWN}; int tailX[100],tailY[100]; int nTail; eDirection dir; void setup() { gameOver=false; dir=STOP; x=widht/2; y=height/2; fruitX=rand()%widht; fruitY=rand()%height; score=0; } void draw() { system("CLS");//clear for(int i=0; i<widht+2; i++) { cout<<"#"; } cout<<endl; for(int i=0; i<height; i++) { for(int j=0; j<widht; j++) { if(j==0) cout<<"#"; if(i==y && j==x) cout<<"O"; else if(i==fruitY && j==fruitX) cout<<"*"; else { bool print=false; for(int k=0;k<nTail;k++) { if(tailX[k]==j && tailY[k]==i) { cout<<"o"; print=true; } } if(!print) cout<<" "; } if(j==widht-1) cout<<"#"; } cout<<endl; } for(int i=0; i<widht+2; i++) { cout<<"#"; } cout<<endl<<"Score: "<<score<<endl; } void input() { if(_kbhit()) { switch(getch()) { case 'a': dir=LEFT; break; case 'd': dir=RIGHT; break; case 'w': dir=UP; break; case 's': dir=DOWN; break; case 'x': gameOver=true; break; } } } void logic() { int prevX=tailX[0]; int prevY=tailY[0]; int prev2X,prev2Y; tailX[0]=x; tailY[0]=y; for(int i=1;i<nTail;i++) { prev2X=tailX[i]; prev2Y=tailY[i]; tailX[i]=prevX; tailY[i]=prevY; prevX=prev2X; prevY=prev2Y; } switch(dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; } /*if(x>widht || x<0 || y>height || y<0) gameOver=true;*///duvardan geçmesini istiyorsan bunu yap if(x>=widht) x=0; else if(x<0) x=widht-1; if(y>=height) y=0; else if(y<0) y=height-1; for(int i=0;i<nTail;i++) { if(tailX[i]==x && tailY[i]==y) gameOver=true; } if(x==fruitX && y==fruitY) { score+=10; fruitX=rand()%widht; fruitY=rand()%height; nTail++; } } void mainMenu() { int s; cout<<"Seviye Sec 1-10"<<endl<<"->"; cin>>s; setup(); while(!gameOver) { draw(); input(); logic(); Sleep(100/s); } char ch; cout<<"Score="<<score<<endl; cout<<"Devam etmek istiyor musun! (E/e)"<<endl<<"->"; cin>>ch; if(ch=='e' || ch=='E') { system("CLS"); nTail=0; mainMenu(); } } int main() { mainMenu(); system("CLS"); cout<<"Game Over!"; Sleep(2000); return 0; } |
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void menu();
void game();
void controls(char s[][22],int temp[][22],int eating);
void credits();
void table(char s[][22],int score);
bool checkFood(char s[][22],int fx,int fy);
void spawnFood(char s[][22],int & fx,int & fy);
bool gameover(char s[][22],int temp[][22]);
void endgame(int score,int highscore);
void help();
void options();
void difficulty();
void pause();
int lenght;
char contr = ‘n’;
int chek[32][22];
double diff=0.5;
int main()
{
srand(time(0));
int i;
char sl;
for(i=0;i0)
{
cout<<""<<endl;
cout<>sl;
if(sl==’e’)
{
system(“cls”);
goto start;
}
else goto quit;
}
start:
{
menu();
}
cin>>sl;
if(sl==’1′)
{
system(“cls”);
game();
i=1;
}
else if(sl==’2′)
{
options();
system(“cls”);
goto start;
i=1;
}
else if(sl==’3′) goto quit;
else
{
system(“cls”);
i=1;
goto start;
}
}
quit:
{
system(“exit”);
}
return 0;
}
void table(char s[][22],int score)
{
int x=32;
int y=22;
int i,j;
for(i=0;i<y;i++)
{
for(j=0;j<x;j++)
{
if(j<(x-1)) cout<<s[j][i];
if(j==(x-1)) cout<<s[j][i]<<endl;
}
}
cout<<""<<endl;
cout<<"skorun: "<<score;
}
void game()
{
char s[32][22];
int temp[32][22];
int i,j,fx,fy,x,y,score=0,highscore=score;
double k;
int eating;
for(i=0;i<22;i++)
{
for(j=0;j0 && i<21) s[j][i]=' ';
if(j==0 || j==31) s[j][i]='|';
}
}
spawnFood(s,fx,fy);
for(y=1;y<21;y++)
{
for(x=1;x<31;x++)
{
temp[x][y]=0;
}
}
s[17][10]='o';
s[16][10]='*';
s[15][10]='*';
temp[15][10]=3;
temp[16][10]=2;
temp[17][10]=1;
system("cls");
table(s,score);
lenght=3;
while(true)
{
eating=0;
Sleep(diff*1000);
if(kbhit())
{
contr=getch();
if(contr=='0') pause();
goto movemake;
}
else
{
goto movemake;
}
movemake:
{
for(x=1;x<31;x++)
{
for(y=1;yhighscore) highscore=score;
}
}
gameEnd:
{
endgame(score,highscore);
return;
}
}
void controls(char s[][22],int temp[][22],int eating)
{
int i,x,y;
for(i=lenght;i>0;i–)
{
if(i==lenght)
{
for(x=1;x<31;x++)
{
for(y=1;y1 && i<lenght)
{
for(x=1;x<31;x++)
{
for(y=1;y<21;y++)
{
if(temp[x][y]==i && s[x][y]=='*')
{
temp[x][y]=i+1;
x=30;
y=20;
}
}
}
}
else if(i==1)
{
for(x=1;x<31;x++)
{
for(y=1;y<21;y++)
{
if(temp[x][y]==i && s[x][y]=='o')
{
movecheck:
if(contr!='1' && contr!='2' && contr!='3' && contr!='4')
{
s[x][y]='*'; temp[x][y]=i+1;
if(temp[x-1][y]==i+2)
{
s[x+1][y]='o'; temp[x+1][y]=1;
return;
}
else if(temp[x+1][y]==i+2)
{
s[x-1][y]='o'; temp[x-1][y]=1;
return;
}
else if(temp[x][y+1]==i+2)
{
s[x][y-1]='o'; temp[x][y-1]=1;
return;
}
else if(temp[x][y-1]==i+2)
{
s[x][y+1]='o'; temp[x][y+1]=1;
return;
}
}
else if(contr=='1')
{
if(temp[x][y-1]==0)
{
s[x][y]='*'; temp[x][y]=i+1;
s[x][y-1]='o'; temp[x][y-1]=1;
return;
}
else
{
contr='n';
goto movecheck;
}
}
else if(contr=='2')
{
if(temp[x][y+1]==0)
{
s[x][y]='*'; temp[x][y]=i+1;
s[x][y+1]='o'; temp[x][y+1]=1;
return;
}
else
{
contr='n';
goto movecheck;
}
}
else if(contr=='3')
{
if(temp[x-1][y]==0)
{
s[x][y]='*'; temp[x][y]=i+1;
s[x-1][y]='o'; temp[x-1][y]=1;
return;
}
else
{
contr='n';
goto movecheck;
}
}
else if(contr=='4')
{
if(temp[x+1][y]==0)
{
s[x][y]='*'; temp[x][y]=i+1;
s[x+1][y]='o'; temp[x+1][y]=1;
return;
}
else
{
contr='n';
goto movecheck;
}
}
}
}
}
}
}
}
bool checkFood(char s[][22],int fx,int fy)
{
int x,y,c=0;
for(x=1;x<31;x++)
{
for(y=1;y<21;y++)
{
if(s[x][y]=='o' && x==fx && y==fy)
{
c=1;
}
}
}
if(c==1) return true;
else return false;
}
void spawnFood(char s[][22],int & fx,int & fy)
{
int i,j;
fx=(rand()%30)+1;
fy=(rand()%20)+1;
for(j=1;j<31;j++)
{
for(i=1;i<21;i++)
{
if(s[j][i]!='*' && s[j][i]!='o')
{
s[fx][fy]='+';
return;
}
else
{
fx=(rand()%30)+1;
fy=(rand()%20)+1;
i=1;
j=1;
}
}
}
}
bool gameover(char s[][22],int temp[][22])
{
int x,y,c=0;
for(x=0;x<32;x++)
{
for(y=0;y<22;y++)
{
if((x==0 || x==31 || y==0 || y==21) && s[x][y]=='o')
{
c=1;
}
if(chek[x][y]==1 && s[x][y]=='o')
{
c=1;
}
}
}
if(c==1) return true;
if(c==0) return false;
}
void endgame(int score,int highscore)
{
cout<<""<<endl<<endl;
cout<<" ———————————————– "<<endl;
cout<<"| * * * * * ****** * * |"<<endl;
cout<<"| * * * * * * * * * * * * |"<<endl;
cout<<"| * * * * * * * * ***** * * * |"<<endl;
cout<<"| ******* * * * * * * * * * * |"<<endl;
cout<<"| * * * ** * ** ****** * ** |"<<endl;
cout<<" ———————————————– "<<endl;
cout<<""<<endl<<endl;
cout<<" EN YUKSEK SKOR: "<<score<<endl<<endl;
cout<<" S K O R U N: "<<highscore<<endl;
cout<<""<<endl<<endl;
}
void menu()
{
cout<<""<<endl;
cout<<" ————————————– "<<endl;
cout<<"| | "<<endl;
cout<<"| * * * * * * * | "<<endl;
cout<<"| * * * * * * * * * | "<<endl;
cout<<"| * * * * * * * * * | "<<endl;
cout<<"| * * * ******* * * * | "<<endl;
cout<<"| * * ***** * * * ** | "<<endl;
cout<<"| | "<<endl;
cout<<"————————————— "<<endl;
cout<<""<<endl<<endl;
cout<<" M E N U: "<<endl<<endl;
cout<<" 1: Oyunu baslat "<<endl<<endl;
cout<<" 2: Ayarlar "<<endl<<endl;
cout<<" 3: Cikis "<<endl<<endl;
}
void help()
{
char sel;
system("cls");
while(true)
{
cout<<" ———————————————-"<<endl;
cout<<"| |"<<endl;
cout<<"| 1 – yukari |"<<endl;
cout<<"| |"<<endl;
cout<<"| 3 – sola 4 – saga |"<<endl;
cout<<"| |"<<endl;
cout<<"| 2 – asagi |"<<endl;
cout<<"| |"<<endl;
cout<<"| 0 – DURDUR |"<<endl;
cout<<"| |"<<endl;
cout<<" ———————————————- "<<endl<<endl<<endl;
cout<>sel;
if(sel==’e’) return;
else system(“cls”);
}
}
void credits()
{
char sel;
system(“cls”);
while(true)
{
cout<<""<<endl<<endl;
cout<<" Fikir: Yani Bilemiomm bence sino"<<endl<<endl;
cout<<" Bas Programci: sino "<<endl<<endl;
cout<<" Tasarimci: sino "<<endl<<endl;
cout<<" Test Eden: sino "<<endl<<endl;
cout<<" Ozel Tesekkurler: sino31 "<<endl<<endl<<endl;
cout<>sel;
if(sel==’e’) return;
else system(“cls”);
}
}
void options()
{
char sel;
system(“cls”);
while(true)
{
system(“cls”);
cout<<""<<endl<<endl;
cout<<" O P S I Y O N L A R : "<<endl<<endl;
cout<<" 1: Zorlugu sec "<<endl<<endl;
cout<<" 2: Kontroller "<<endl<<endl;
cout<<" 3: Sunum "<<endl<<endl;
cout<<" 4: Geri don "<<endl<>sel;
if(sel==’1′) difficulty();
else if(sel==’2′) help();
else if(sel==’3′) credits();
else if(sel==’4′) return;
else system(“cls”);
}
}
void difficulty()
{
char sel;
system(“cls”);
while(true)
{
cout<<""<<endl<<endl;
cout<<" S U A N K I Z O R L U K:"<<endl<<endl;
if(diff==1) cout<<" K O L A Y"<<endl<<endl;
else if(diff==0.5) cout<<" N O R M A L"<<endl<<endl;
else if(diff==0.2) cout<<" Z O R"<<endl<<endl;
else if(diff==0.1) cout<<" C O K Z O R"<<endl<<endl;
else if(diff==0.03) cout<<" E B E N I N"<<endl<<endl;
cout<<" Z O R L U G U S E C:"<<endl<<endl;
cout<<" 1: kolay "<<endl<<endl;
cout<<" 2: Normal "<<endl<<endl;
cout<<" 3: Zor "<<endl<<endl;
cout<<" 4: Cok zor "<<endl<<endl;
cout<<" 5: Ebenin "<<endl<<endl<<endl<<endl;
cout<<" 6: Geri don "<<endl<>sel;
if(sel==’1′)
{
diff=1;
system(“cls”);
}
else if(sel==’2′)
{
diff=0.5;
system(“cls”);
}
else if(sel==’3′)
{
diff=0.2;
system(“cls”);
}
else if(sel==’4′)
{
diff=0.1;
system(“cls”);
}
else if(sel==’5′)
{
diff=0.03;
system(“cls”);
}
else if(sel==’6′) return;
else system(“cls”);
}
}
void pause()
{
char c;
cout<<""<<endl<<endl;
cout<>c;
if(c==’e’) return;
else goto ooo;
}