Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
531 changes: 531 additions & 0 deletions C++/linkedlist.cpp

Large diffs are not rendered by default.

Binary file added snek/bin/Debug/snek.exe
Binary file not shown.
18 changes: 18 additions & 0 deletions snek/include/food.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef FOOD_H
#define FOOD_H
#include<windows.h>
#include<time.h>
#include<stdlib.h>
const int height=30,width=60;
class food
{

private:
COORD pos;
public:
food();
void genfood();
COORD getpos();
};

#endif // FOOD_H
25 changes: 25 additions & 0 deletions snek/include/snake.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef SNAKE_H
#define SNAKE_H
#include<windows.h>

class snake
{
private:
COORD pos;
int len;
int vel;
int direction;

public:
snake(COORD pos,int vel);
void changedir(char dir);
void movesnek();
COORD getpos();

bool eaten(COORD foodpos);

void grow();
bool snakecollided();
};

#endif // SNAKE_H
78 changes: 78 additions & 0 deletions snek/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <iostream>
#include "snake.h"
#include "food.h"
#include<conio.h>
using namespace std;
snake snake({width/2,height/2},1);
food food;
int x=10,y=10;
void board()
{
COORD snakepos=snake.getpos();
COORD foodpos=food.getpos();
cout<<"\n\n\n\n\n\n";
for (int i=0;i<height;i++)
{
cout<<"\t\t\t#";
for(int j=0;j<width-1;j++)
{
if(i==0||i==height-1)
{
cout<<"#";
}
else if(i==snakepos.Y && j+1==snakepos.X)
{
cout<<"D";
}
else if(i==foodpos.Y && j+1==foodpos.X)
{
cout<<"*";
}
else
{
cout<<" ";
}
}
cout<<"#\n";
}
};
int main()
{
bool gameover = false;

while(!gameover)
{
board();
if(GetAsyncKeyState('W'))
{
snake.changedir('w');
}
else if(GetAsyncKeyState('A'))
{
snake.changedir('a');
}
else if(GetAsyncKeyState('S'))
{
snake.changedir('s');
}
else if(GetAsyncKeyState('D'))
{
snake.changedir('d');
}
snake.movesnek();

if(snake.eaten(food.getpos()))
{
food.genfood();
snake.grow();
}
if(snake.snakecollided())
{
cout<<"GAME OVER";
gameover = true;

}
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),{0,0});
}
return 0;
}
Binary file added snek/obj/Debug/main.o
Binary file not shown.
Binary file added snek/obj/Debug/src/food.o
Binary file not shown.
Binary file added snek/obj/Debug/src/snake.o
Binary file not shown.
47 changes: 47 additions & 0 deletions snek/snek.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="snek" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/snek" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
<Add directory="include" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/snek" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
<Add directory="include" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="include/food.h" />
<Unit filename="include/snake.h" />
<Unit filename="main.cpp" />
<Unit filename="src/food.cpp" />
<Unit filename="src/snake.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
22 changes: 22 additions & 0 deletions snek/snek.depend
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# depslib dependency file v1.0
1598523400 source:c:\users\bishal\documents\snek\main.cpp
<iostream>
"snake.h"
"food.h"
<conio.h>

1598343294 c:\users\bishal\documents\snek\include\snake.h
<windows.h>

1598523275 source:c:\users\bishal\documents\snek\src\snake.cpp
"snake.h"
"food.h"

1598459995 source:c:\users\bishal\documents\snek\src\food.cpp
"food.h"

1598342165 c:\users\bishal\documents\snek\include\food.h
<windows.h>
<time.h>
<stdlib.h>

30 changes: 30 additions & 0 deletions snek/snek.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="include\food.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="-7" zoom_2="0">
<Cursor>
<Cursor1 position="42" topLine="0" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="-6" zoom_2="0">
<Cursor>
<Cursor1 position="1667" topLine="38" />
</Cursor>
</File>
<File name="src\snake.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="-8" zoom_2="0">
<Cursor>
<Cursor1 position="819" topLine="35" />
</Cursor>
</File>
<File name="src\food.cpp" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="-6" zoom_2="0">
<Cursor>
<Cursor1 position="170" topLine="0" />
</Cursor>
</File>
<File name="include\snake.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="-8" zoom_2="0">
<Cursor>
<Cursor1 position="353" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
16 changes: 16 additions & 0 deletions snek/src/food.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "food.h"

food::food()
{
genfood();
}
void food::genfood()
{
srand(time(0));
pos.X= (rand() % (width-2))+ 1;
pos.Y= (rand() %(height -2))+ 1;
}
COORD food::getpos()
{
return pos;
}
61 changes: 61 additions & 0 deletions snek/src/snake.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "snake.h"
#include"food.h"
snake::snake(COORD pos,int vel)
{
this->pos=pos;
this->vel=vel;
len=1;
direction='n';
}
void snake::changedir(char dir)
{
direction=dir;
}
void snake::movesnek()
{
switch(direction)
{
case 'w': pos.Y -= vel;
break;
case 'a': pos.X -= vel;
break;
case 's': pos.Y += vel;
break;
case 'd': pos.X += vel;
break;
}
}

COORD snake :: getpos()
{
return pos;
}

bool snake::eaten(COORD foodpos)
{
if(foodpos.X==pos.X && foodpos.Y==pos.Y)
{
return true;
}
else
{
return false;
}
}

void snake::grow()
{
len++;
}

bool snake::snakecollided()
{
if(pos.X<1 || pos.X>=width-1|| pos.Y<1 || pos.Y>=height-1)
{
return true;
}
else
{
return false;
}
}