0% found this document useful (0 votes)
20 views12 pages

Snake Game in C Programming: Report Submitted BY

Uploaded by

squall enix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

Snake Game in C Programming: Report Submitted BY

Uploaded by

squall enix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Snake game in C

programming
REPORT SUBMITTED
BY:
NIZAM UDDIN
CSE 080 08534
MD. MAHMOOL UL QUAIUM
CSE 080 08541
PROBIN SARKER
CSE 080 08566
JANNATUL NAYEEM RIYA
CSE 080 08579

SUBJECT: STRUCTURED PROGRAMMING


LANGUAGE(SPL).

Submitted To:
Saima Siddique Tashfia

Signature: _____________________
Introduction:

Our project is about making a snake game in C programming.


We chose this topic as this pick our interests while making us proficient in
what we are learning.

Objective:

Our main objective is to implement as much as we can from our SPL


subject. This project shows our effort and passion toward programming
and computers.
As we continue to learn and improve we dream and hope that we will
be able to make big achievement toward programming and our jobs.
We also plan to make other games and some common database projects
to help and improve our proficiency.

2
Steps:

• After deciding our topic we had no idea where start. But after careful study
and help from various YouTube videos we were able to start our work.

• At first we decided our code will be in multiple functions and our first
interest was how to make the walls which separated the snake from
outside. As we continued and struggled a lot we understood the concept
and took next steps for the game. We used loops and nested loops to make
the borders. Many if conditions were used as its very important or the
code wont function as intended.

• The drawing of borders or walls of the game we needed values and so we


made the setup function. That part was very buggy and irritating. For the
fruits random location we used the rand function.

• Then we used input and logic function and completed the game.

• In the input function we used kbhit function to take values real time while
not printing in the console while passing the values with switch function
and make the snake move through WASD buttons. There are two switch
functions used.

• The logic for the tail to keep spawning when fruit is eaten is coded in the
logic function. This was the most brain wrecking part. Also if the snake bit
the tail, game would be over rules was taken from here to draw function.

• System(“cls”) was used to clear the screen of the console as inputs were
getting updated continuously and the loop would run the entire function
again and again. That is the reason the screen looks like its changing.

• It was tough and tiring but nevertheless we finished it.

3
The Code:

• #include<stdio.h>
• #include<stdlib.h>
• #include<conio.h>
• int width=20;
• int height=20;
• int i=0,j=0,x=0,y=0,padd=5,fruitx,fruity,movem,gameover,score=0;
• int tailx[100],taily[100],tailcount=0;
• //The border in which the game will be played.
• void setup()
•{
• x=width/2;//position of the snake heada
• y=height/2;
• gameover=1;
• label1:
• fruitx=rand()%20;//fruits to be spawned randomly inside the height
and width
• if(fruitx==0)
• {
• goto label1;
• }
• label2:
• fruity=rand()%20;
• if(fruity==0)
• {
• goto label2;
• }
•}

4
• void input()//key inputs
•{
• if(kbhit())
• {
• switch(getch())
• {
• case 'w':
• movem=1;
• break;
• case 's':
• movem=2;
• break;
• case 'a':
• movem=3;
• break;
• case 'd':
• movem=4;
• break;
• case 'x':
• gameover=0;
}
}
}

5
• void logic()
• {
• int prevx1,prevy1,prevx2,prevy2;//tail logic using array and array exchaning
• prevx1=tailx[0];
• prevy1=taily[0];
• tailx[0]=x;
• taily[0]=y;
• for(int i=1; i<tailcount; i++)
• {
• prevx2=tailx[i];
• prevy2=taily[i];
• tailx[i]=prevx1;
• taily[i]=prevy1;
• prevx1=prevx2;
• prevy1=prevy2;
• }
• switch(movem)//movement after key input
• {
• case 1:
• x--;
• break;
• case 2:
• x++;
• break;
• case 3:
• y--;
• break;
• case 4:
• y++;
• break;
• }

6
• if(x<0||x>width||y<0||y>height)
• {
• gameover=0;
• }
• if(x==fruitx&&y==fruity)
• {
• label3:
• fruitx=rand()%20;
• if(fruitx==0)
• {
• goto label3;
• }
• label4:
• fruity=rand()%20;
• if(fruity==0)
• {
• goto label4;

• }
• score+=10;
• tailcount++;
• }
• for(int i=0;i<tailcount;i++){//if head touches tail then gameover
• if(x==tailx[i]&&y==taily[i]){
• gameover=0;
• }
•}
•}

7
• void draw()
•{
• system("cls");
• for(int i=0; i<padd; i++)
• {
• printf("\n");
• for(i=0; i<height; i++)
• {
• for(int i=0; i<padd; i++)
• {
• printf(" ");//Padding the sides.
• }
• for(j=0; j<width; j++)
• {
• if(i==0||i==height-1||j==0||j==width-1) //Drawing the border
• {
• printf("\033[1;37m");//Colour
• printf("!");
• }
• else
• {
• if(i==x&&j==y)
• {
• printf("\033[1;34m");
• printf("O");//Drawing head
• }
• else if(i==fruitx&&j==fruity)
• {
• printf("\033[1;33m");
• printf("+");//Drawing fruit
• }

8
• else
• {
• printf("\033[1;34m");
• int per=0;
• for(int t=0; t<tailcount; t++)
• {
• if(i==tailx[t] && j==taily[t]){//Drawing the tail

• printf("o");
• per=1;
• }
• }
• if(per==0)
• {
• printf(" ");
• }
• }

• }
• }
• printf("\n");
• }
• }
• printf("\033[1;36m");
• printf("Score: %d",score);//prints score
• }//draw ends

9
• int main()
•{
• setup();
• while(gameover)
• {
• draw();

• input();
• logic();
• for(int i=0; i<10000; i++)
• {
• for(int j=0; j<9500; j++)
• {

• }

• }
• }
• printf("\nYour score is %d\nBy
***Md.qaiyum,nizam,riya,shubhon***",score);
•}

10
The Output:

11
Conclusion:

This project was done by four people and everyone contributed as


much as they could.

Quaium was a hearty person with positive outlook and was quite
understandable in many regards.
Shubhon participated in writing the code. He is quite fun and has
good taste in ideas.

Riya was able to notice the bugs and the problems. She is very
keen in her understanding.

Nizam was the one who gave the game idea and he helped us
throughout the whole project. He explained the points and
function which we weren’t able to get cleared during our studies.

In Conclusion this game was our effort as a team and everyone


did best as they could and this is our result. While our bonds
grew, we got a taste for striving for bigger goals and we will
continue on.

Thank you for reviewing our report.

12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy