Question

In: Computer Science

Write code for a simple snake game (using dots) on C program. Using openframeworks. it should...

Write code for a simple snake game (using dots) on C program.

Using openframeworks. it should be simple because I am new to coding and I cant write complicated code.

just make it simple.

Solutions

Expert Solution

#include<graphics.h>
#include<time.h>

int main()
{
   int i,X[100],Y[100],rx,ry,gm,gd,l,d=2,s=16;
   srand(time(NULL));
   detectgraph(&gd,&gm);
   initgraph(&gd,&gm,NULL);
   setfillstyle(1,1);
   bar(s-s/2,s-s/2,(s/2)+s*(1350/s),s+s/2);
   bar(s-s/2,(-s/2)+s*(700/s),(s/2)+s*(1350/s),(s/2)+s*(700/s));
   bar(s-s/2,s-s/2,s+s/2,(s/2)+s*(700/s));
   bar((-s/2)+s*(1350/s),s-s/2,(s/2)+s*(1350/s),(s/2)+s*(700/s));
   X[0]=s*(1350/(2*s)); Y[0]=s*(700/(2*s));
bar(X[0]-s/2,Y[0]-s/2,X[0]+s/2,Y[0]+s/2);
l=5;
for(i=1;i<l;i++)
{
X[i]=X[0]-(i*s);
Y[i]=Y[0];
bar(X[i]-s/2,Y[i]-s/2,X[i]+s/2,Y[i]+s/2);
}
rx=s; ry=s;
setfillstyle(1,2);
while(getpixel(rx,ry)!=0)
   {
       rx=s*(1+rand()%(1350/s-1));
       ry=s*(1+rand()%(700/s-1));
   }
   bar(rx-s/2,ry-s/2,rx+s/2,ry+s/2);
delay(2000);
   while(1)
   {
   setfillstyle(1,0);
       bar(X[l-1]-s/2,Y[l-1]-s/2,X[l-1]+s/2,Y[l-1]+s/2);
       for(i=l-1;i>0;i--)
   {
       X[i]=X[i-1];
       Y[i]=Y[i-1];
       }
   if(d==0)
           X[0]=X[0]-s;
       if(d==1)
           Y[0]=Y[0]-s;  
   else if(d==2)
           X[0]=X[0]+s;
       else if(d==3)
           Y[0]=Y[0]+s;
       if(getpixel(X[0],Y[0])==1)
           break;
       if((GetAsyncKeyState(VK_RIGHT))&&(d!=0))
           d=2;
       else if((GetAsyncKeyState(VK_UP))&&(d!=3))
           d=1;
       else if((GetAsyncKeyState(VK_LEFT))&&(d!=2))
           d=0;
       else if((GetAsyncKeyState(VK_DOWN))&&(d!=1))
           d=3;
       if(getpixel(X[0],Y[0])==2)
       {
           rx=s; ry=s;
           setfillstyle(1,2);
           while(getpixel(rx,ry)!=0)
           {
               rx=s*(1+rand()%(1350/s-1));
               ry=s*(1+rand()%(700/s-1));
           }
           bar(rx-s/2,ry-s/2,rx+s/2,ry+s/2);
           l=l+1;
       }
       setfillstyle(1,1);
       for(i=0;i<l;i++)
        bar(X[i]-s/2,Y[i]-s/2,X[i]+s/2,Y[i]+s/2);  
       delay(100);
}
printf("score : %d",l-5);
   while(!GetAsyncKeyState(VK_RETURN));
   closegraph();
   getch();
   return 0;  
}


Related Solutions

Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c coding.
Write a code for simple racing game (using dots) on c coding.
Write a simple code for a simple connect the dots game in plain C coding.
Write a simple code for a simple connect the dots game in plain C coding.
Simple code for a game on C coding.
Simple code for a game on C coding.
C# To write a program using a stack and a program using a queue. Code a...
C# To write a program using a stack and a program using a queue. Code a class encapsulating a queue of foods using a circular array. A food has the following attributes: name, the number of calories per serving and the number of servings per container. Limit your queue to 20 food items. In addition to writing the enqueue, dequeue, and peek methods, you will write two more methods: a method that returns the average calories per serving of all...
Using Python, Assignment Write a program that plays a game of craps. The program should allow...
Using Python, Assignment Write a program that plays a game of craps. The program should allow the player to make a wager before each “turn”. Before each turn, allow the user to either place a bet or exit the game. After each turn display the player’s current balance. Specifics Each player will start with $500.00. Initially, and after each turn give the user the option of betting or leaving the program. Implement this any way you wish, but make it...
Using C Write a program that will serve as a simple shell. This shell will execute...
Using C Write a program that will serve as a simple shell. This shell will execute an infinite for loop. In each iteration of the loop, the user will be presented with a prompt. When the user enters a command, the shell will tokenize the command, create a child process to execute it and wait for the child process to be over. If the user enters an invalid command, the shell should recognize the situation and show a meaningful message....
write a c# program that: The file is using a very simple form of compression in...
write a c# program that: The file is using a very simple form of compression in which there are no spaces, each word is separated by camel casing. For Example, the string "TheCatWillRun" is parsed as "The Cat Will Run". *Now for the statistics* Prints to the console the following statistics about the content in the file retrieved above. - How many of each letter are in the file - How many letters are capitalized in the file - The...
Create a Snake game project in C++ using all these which are given below. 1. Classes...
Create a Snake game project in C++ using all these which are given below. 1. Classes 2. structures 3. files ( MULTFILLING AND FILE HANDLING ) 4. loops 5. conditions 6. switch 7. functions 8. array Note: Also upload the zip file.
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT