Upload
shailesh-singh
View
130
Download
0
Embed Size (px)
Citation preview
SUBMITTED TO - SUBMITTED BY-
MISS RITU RAI ANKIT SINGLA
ROLL NO-A28
B.TECH (CSE)
REGD.NO:10808340
ACKNOWLEDGEMENT
First of all I bow my head to “Almighty God” who gave command
to my hands to make the project a success. A formal statement of
acknowledgement will hardly meet the ends of justice in matter of
expression of my deep unsound sense of gratitude and obligations
to my esteemed, Head of the Department of Engineering, Lovely
professional university, Phagwara, for his sincere effort, keen
interest and caring nature. Words at my command are insufficient
to show my respect to MISS RITU my internal guide for inspiring
and providing constructive ideas and rendering thought provoking
discussions for the preparation of the project “SNAKE GAME ”.
And for providing all types of assistance. I bow my heads to my
parents for their generous support, positive attitude, invaluable
blessing, financial help and endless sacrifices for my better and
bright future.I find it beyond my words to acknowledge the love,
help and inspiration offered my friends. Finally, I would also like
to thank all those who knowingly or unknowingly helped me all
throughout my project.
“All cannot be mentioned but none is forgotten”.
CONTENTS
1.ACKNOWLEDGEMENT
2.INTRODUCTION
3.RULES
4.ALGORITHM
5.PURPOSE SYSTEM
6.THE LISTING
7.DISCRIPTION
8.HISTORY
9.TYPES
10.SOURCE CODE
The Classic Snake Game
INTRODUCTION :
The following is an example game written in C based on the
game called 'snake' which has been around since the earliest days
of home computing (I c The following is an example game written
in C based on the game called 'snake' which has been around since
the earliest days of home computing emember writing a version of
it for my ZX81), and has re-emerged in recent years on mobile
phones.It isn't the world's greatest game, but it does give you an
idea of what you can achieve with a relatively simple C program,
and perhaps the basis by which to extend the principles and create
more interesting games of your own.
Rules:
Don't run the snake into the wall, or his own tail: you die.
Use your cursor keys: up, left, right, and down.
Keyboard "P" may also be used for "Play" and "Pause"
Eat the colored apples to gain points.
Higher (and faster) play levels award more points.
You will need to click your "Refresh" button to see new high
scores.
ALGORITHM
STEP.1: you can download the executable to try out the game
without having to compile it if you wish. Download it is about
123k in length
STEP.2: Note that on faster PCs, it will be unplayably fast, so
you will need to re-compile it with the pause_length constant set
to a higher value.
STEP.3:My PC is about 350MHz and it is OK.To move the
snake, use 'a' for up, 'z' for down, 'o' for left and 'p' for right.
STEP.4:Again, there are constants you can change if you want to
alter these settings. Press 'x' to exit the game at any time.
STEP.5: The aim of the game is to collect the dots (food) and
avoid the obstacles (crosses, borders, and the snake itself). As you
collect food, the snake gets longer, so increasing your likelihood of
crashing into yourself.
STEP.6:When you have collected enough food, you progress
onto the next level, where your snake gets longer, and the amount
of food to collect to progress through the level gets larger.You get
scored according to the length of the snake and the number of 'x'
obstacles on the screen.The speed increases every 5 levels.
STEP.7: You get a bonus when you complete the level of 1000,
increasing by 1000 each level (e.g. complete level 5, you get a
5000 bonus).There is no concept of lives.
STEP.8: Once you hit an obstacle, that's it, game over.Make sure
you do not have the caps lock on, otherwise the keys will fail to
respond.
THE LISTING
This is the C program for the game. it is reasonably well
structured, but by no means perfect. Note that it uses library
functions that are not available on all C compilers - it was designed
using Borland C++ Builder / Turbo C. Also it uses a few C++
concepts not available in standard C - e.g. const for a constant (you
would use #define in normal C). You may also have to change
your compiler settings if you have them set to ANSI C to C++
instead - the for loops define a local instance of I as a loop
counter, a concept to available in standard C.
DISCRIPTION … Snake (video game )
Snake is a video game that came out in the late 1970s and has maintained
popularity since then, becoming somewhat of a classic. The player controls a
long, thin creature, resembling a snakewhich roams around on a bordered
plane, picking up food(or some other item), trying to avoid hitting its own
tail or the "walls" that surround the playing area. Each time the snake eats a
piece of food, its tail grows longer, making the game increasingly difficult.
The user controls the direction of the snake's head (up, down, left, or right),
and the snake's body follows. The player cannot stop the snake from moving
while the game is in progress.
History
The Snake variety of games originated with the arcade game
Blockade, released by Gremlin in 1976[1] The first known
microcomputer version of Snake, titled 'Worm', was programmed
in 1978 by P. Trefonas (USA) on the TRS-80 computer, and
published by CLOAD magazine the same year. This was followed
shortly afterwards with versions from the same author for the PET
and Apple II computers. A microcomputer port of Hustle was first
written by P. Trefonas in 1979 and published by CLOAD
magazine . This was later released by Milton Bradleyfor the TI-
99/4A in 1980.
Some better-known versions include the Neopets example, which
is known as "Meerca Chase". Its revised version is known as
"Meerca Chase II". A popular variant called nibbles was also
included with MS-DOS for a period of time.
An analog joystick-controlled variant of Snake, called Anaconda,
was included as a hidden minigame in TimeSplitters 2.
The version included on the Nokia N70 and other written by P.
Trefonas in 1979 and published by CLOAD magazine . This was
later released by Milton Bradleyfor the TI-99/4A in 1980.
Some better-known versions include the Neopets example, which
is known as "Meerca Chase". Its revised version is known as
"Meerca Chase II". A popular variant called nibbles was also
included with MS-DOS for a period of time.
An analog joystick-controlled variant of Snake, called Anaconda,
was included as a hidden minigame in TimeSplitters 2.
The version included on the Nokia N70 and other later model
Nokia phones is a 3D version, with level goals. The Nokia version
has a snake in it as well
Snake on the BBC Micro
There were several versions of Snake on the BBC Micro. Snake by
John Cox from Computer Conceptswas different in that the snake
was controlled using the left and right arrow keys relative to the
direction it was heading in. The snake increases in speed as it gets
longer, and there are no 'lives', making achieving a high score or
reaching higher levels relatively difficult as one mistake means
starting from the beginning.
Snake on Nokia phones
Nokia is well known for putting Snake on most of their phones. Versions
include:
Snake - The original, for monochrome phones. Graphics consisted of
black squares and it had 4 directions. An example of a phone with it
installed is the Nokia 5110.
Snake II - Included on monochrome phones. Snake improved to a
snake pattern, introduction of bonus bugs etc... An example of a
phone with it installed is the Nokia 3310.
Snake Xenzia - Included on present-day monochrome phones (and
some cheaper colour phones, such as the Nokia 1600). An example of
a phone with it installed is the Nokia 1112
Snake EX - Included on colour phones. Graphics improved to SNES
quality. It supports multiplayer through Bluetooth and Infra-Red. An
example of a phone with it installed is the Nokia 6260.
Snake EX2 - This is included in several Series40 handsets by Nokia.
Snakes - A 3D version. This game was designed for the N-Gage,
developed by IOMO(published by Nokia). It featured multiplayer
through Bluetooth. Graphics improved to basic PlayStation quality.
Introduction of hex levels, etc. Later Nokia started giving this game
pre-installed(without multiplayer feature) in its Nseries phones like
N70, N73, N80, etc.
CODING OF SNAKE GAME IN C LANGUAGE
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
check();
end();
win();
int m[500],n[500],con=20,TEMP;
clock_t start,stop;
void main(void)
{
int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100;
int a=0,i=0,j,t,temp;
initgraph(&gd,&gm,"..\bgi"setcolor(WHITE);
settextstyle(3,0,6);
outtextxy(200,2," SNAKE 2 BY RAJESH ");
settextstyle(6,0,2);
outtextxy(20,80," Use Arrow Keys To Direct The Snake ");
outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of
Snake");
outtextxy(20,160," Pick The Beats Untill You Win The Game ");
outtextxy(20,200," Press 'Esc' Anytime To Exit ")
;outtextxy(20,220," Press Any Key To Continue ");
outtextxy(20,220," DONT FORGET TO GIVE U R VALUABLE
OPINION ");
ch=getch();
if(ch==27)
exit(0);
cleardevice();
maxx=getmaxx();
maxy=getmaxy();
randomize();
p=random(maxx);
temp=p%13;
p=p-temp;
q=random(maxy);
temp=q%14;
q=q-temp;
start=clock();
while(1)
{
setcolor(WHITE);
setfillstyle(SOLID_FILL,con+5);
circle(p,q,5);
floodfill(p,q,WHITE);
if( kbhit() )
{
ch=getch(); if(ch==0) ch=getch();
if(ch==72&& a!=2)a=1;
if(ch==80&& a!=1)a=2;
if(ch==75&& a!=4)a=3;
if(ch==77&& a!=3) a=4;
}
else
{
if(ch==27)
break;
}
if(i<20)
{
m[i]=x;
n[i]=y;
i++;
}
if(i>=20)
{
for(j=con;j>=0;j--)
{
m[1+j]=m[j];
n[1+j]=n[j];
}
m[0]=x;
n[0]=y;
setcolor(WHITE);
setfillstyle(SOLID_FILL,con);
circle(m[0],n[0],8);
floodfill(m[0],n[0],WHITE);
setcolor(WHITE);
for(j=1;j<con;j++)
{
setfillstyle(SOLID_FILL,con+j%3);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
delay(spd);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(m[0],n[0],8);
floodfill(m[0],n[0],BLACK);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(m[j],n[j],5);
floodfill(m[j],n[j],BLACK);
}
stop=clock();
t=(stop-start)/CLK_TCK;
//printf(" TIME %d sec ",t);
//printf("SCORE %d",con-5);
check();
if(x==p&&y==q)
{
con=con+5;
if(spd>=5)
spd=spd-5;
else
spd=5;
if(con>490)
win();
p=random(maxx);
temp=p%13;
p=p-temp;
q=random(maxy);
temp=q%14;
q=q-temp;
}
if(a==1)
y = y-14;
if(y<0)
{
temp=maxy%14;
y=maxy-temp;
}
if(a==2)
y = y+14;
if(y>maxy)
y=0;
if(a==3)
x = x-13;
if(x<0)
{
temp=maxx%13;
x=maxx-temp;
}
if(a==4)
x = x+13;
if(x>maxx)
x=0;
if(a==0)
{
y = y+14 ;
x=x+13;
}
}
}
check()
{
int a;
for(a=1;a<con;a++)
if(m[0]==m[a] && n[0]==n[a])
end();
else
continue;
}
end()
{
int j,i;
setcolor(WHITE);
for(i=0;i<5;i++)
{
delay(500);
cleardevice();
delay(500);
for(j=0;j<=con;j++)
{
setfillstyle(SOLID_FILL,RED);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
}
settextstyle(3,0,4);
outtextxy(150,150," GAME OVER ");
getch();
getch();
exit(0);
}
win()
{
int j,i;
setcolor(WHITE);
for(i=0;i<5;i++)
{
for(j=0;j<=con;j++)
{
setfillstyle(SOLID_FILL,con);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
delay(500);
cleardevice();
delay(500);
}
settextstyle(3,0,4);
outtextxy(210,320," YOU WIN ");
getch();
exit(0);
}