Hocamızın verdiği görsel ödevini sınıftan bir arkadaşımız snake ile yapmıştı. Karışık ve yorucu umarım anlayabilirsiniz.
Source Code
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<stdio.h>
#include <limits.h>
#include <stdlib.h>
int check = 3,mouse = 0,score = 8;
void checker(int randx,int randy,int x,int y)
{
int temp1 = randx-x;
int temp2 = randy-y;
if(temp1 < 0)
temp1 *= -1;
if(temp2 < 0)
temp2 *= -1;
if(temp1 <= 8 && temp2 <= 8)
{
mouse = 0;
score++;
}
}
void dead(int x,int y)
{
if(x <= 5 || x+10 >= 635 || y <= 5 || y+10 >= 475)
{
outtextxy(10,10,"OYUN BITTI");
Sleep(3000);
return;
}
}
int main()
{
int gd = DETECT, gm;
int x = 6,y = 6,i = 0,j,temp1,temp2,kontrol=1;
int randx=10,randy=10;
int sayac = 0,busy = 0;
initgraph(&gd, &gm, "");
while(1)
{
if(mouse == 0)
{
srand(time(NULL));
randx = rand() % 630 +5;
randy = rand() % 470 +5;
mouse = 1;
}
setcolor(WHITE);
rectangle(randx,randy,randx+7,randy+7);
setcolor(RED);
for(i = 0;i <= 5;i++)
rectangle(0+i,0+i,640-i,480-i);
setcolor(YELLOW);
i =0;
if(check == 0)
{
if(kontrol ==3)
{
busy = 1;
for(i=score-sayac-1;i>=0;i--)
rectangle(x-(i*10),y,x+10-(i*10),y+10);
for(j=sayac;j>=0;j--)
{
rectangle(x,y-(j*10),x+10,y+10-(j*10));
checker(randx,randy,x,y-(j*10));
dead(x,y-(j*10));
}
Sleep(250);
sayac++;
if(sayac == score)
{
y -= (score-1)*10;
kontrol = 0;
sayac = 0;
}
}
else if(kontrol == 2)
{
busy = 1;
for(i=score-sayac-1;i>=0;i--)
rectangle(x+(i*10),y,x+10+(i*10),y+10);
for(j=sayac;j>=0;j--)
{
rectangle(x,y-(j*10),x+10,y+10-(j*10));
checker(randx,randy,x,y-(j*10));
dead(x,y-(j*10));
}
Sleep(250);
sayac++;
if(sayac == score)
{
y -= (score-1)*10;
kontrol = 0;
sayac = 0;
}
}
else if(kontrol == 1)
{
check = 1;
kontrol = 1;
continue;
}
else
{
y-=10;
for(i=0;i<score;i++)
rectangle(x,y+(i*10),x+10,y+10+(i*10));
Sleep(250);
kontrol = 0;
busy = 0;
}
}
else if(check == 1)
{
if(kontrol ==3)
{
busy = 1;
for(i=score-sayac-1;i>=0;i--)
rectangle(x-(i*10),y,x+10-(i*10),y+10);
for(j=sayac;j>=0;j--)
{
rectangle(x,y+(j*10),x+10,y+10+(j*10));
checker(randx,randy,x,y+(j*10));
dead(x,y+(j*10));
}
Sleep(250);
sayac++;
if(sayac == score)
{
y += (score-1)*10;
kontrol = 1;
sayac = 0;
}
}
else if(kontrol == 2)
{
busy = 1;
for(i=score-sayac-1;i>=0;i--)
rectangle(x+(i*10),y,x+10+(i*10),y+10);
for(j=sayac;j>=0;j--)
{
rectangle(x,y+(j*10),x+10,y+10+(j*10));
checker(randx,randy,x,y+(j*10));
dead(x,y+(j*10));
}
Sleep(250);
sayac++;
if(sayac == score)
{
y += (score-1)*10;
kontrol = 1;
sayac = 0;
}
}
else if(kontrol == 0)
{
check = 0;
kontrol = 0;
continue;
}
else
{
y+=10;
for(i=0;i<score;i++)
rectangle(x,y-(i*10),x+10,y+10-(i*10));
Sleep(250);
kontrol = 1;
busy = 0;
}
}
else if(check == 2)
{
if(kontrol ==1)
{
busy = 1;
for(i=score-sayac-1;i>=0;i--)
rectangle(x,y-(i*10),x+10,y+10-(i*10));
for(j=sayac;j>=0;j--)
{
rectangle(x-(j*10),y,x+10-(j*10),y+10);
checker(randx,randy,x-(j*10),y);
dead(x-(j*10),y);
}
Sleep(250);
sayac++;
if(sayac == score)
{
x -= (score-1)*10;
kontrol = 2;
sayac = 0;
}
}
else if(kontrol == 0)
{
busy = 1;
for(i=score-sayac-1;i>=0;i--)
rectangle(x,y+(i*10),x+10,y+10+(i*10));
for(j=sayac;j>=0;j--)
{
rectangle(x-(j*10),y,x+10-(j*10),y+10);
checker(randx,randy,x-(j*10),y);
dead(x-(j*10),y);
}
Sleep(250);
sayac++;
if(sayac == score)
{
x -= (score-1)*10;
kontrol = 2;
sayac = 0;
}
}
else if(kontrol == 3)
{
check = 3;
kontrol = 3;
continue;
}
else
{
x-=10;
for(i=0;i<score;i++)
rectangle(x+(i*10),y,x+10+(i*10),y+10);
Sleep(250);
kontrol = 2;
busy = 0;
}
}
else if(check == 3)
{
if(kontrol ==1)
{
busy = 1;
for(i=score-sayac-1;i>=0;i--)
rectangle(x,y-(i*10),x+10,y+10-(i*10));
for(j=sayac;j>=0;j--)
{
rectangle(x+(j*10),y,x+10+(j*10),y+10);
checker(randx,randy,x+(j*10),y);
dead(x+(j*10),y);
}
Sleep(250);
sayac++;
if(sayac == score)
{
x += (score-1)*10;
kontrol = 3;
sayac = 0;
}
}
else if(kontrol == 0)
{
busy = 1;
for(i=score-sayac-1;i>=0;i--)
rectangle(x,y+(i*10),x+10,y+10+(i*10));
for(j=sayac;j>=0;j--)
{
rectangle(x+(j*10),y,x+10+(j*10),y+10);
checker(randx,randy,x+(j*10),y);
dead(x+(j*10),y);
}
Sleep(250);
sayac++;
if(sayac == score)
{
x += (score-1)*10;
kontrol = 3;
sayac = 0;
}
}
else if(kontrol == 2)
{
check = 2;
kontrol = 2;
continue;
}
else
{
x+=10;
for(i=0;i<score;i++)
rectangle(x-(i*10),y,x+10-(i*10),y+10);
Sleep(250);
kontrol = 3;
busy = 0;
}
}
dead(x,y);
if (!kbhit())
x = x;
else if(busy == 0)
{
if ( GetAsyncKeyState ( VK_UP ) & SHRT_MAX )
check = 0;
else if ( GetAsyncKeyState ( VK_DOWN ) & SHRT_MAX )
check = 1;
else if ( GetAsyncKeyState ( VK_LEFT ) & SHRT_MAX )
check = 2;
else if ( GetAsyncKeyState ( VK_RIGHT ) & SHRT_MAX )
check = 3;
}
checker(randx,randy,x,y);
delay(5);
cleardevice();
}
closegraph();
return 0;
}