Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the Snakes Game C++ #644

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions GAME/C-Snake-Game-master/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# C-Snake-Game
C++ Snake Game | C++ Snake Terminal Game
48 changes: 48 additions & 0 deletions GAME/C-Snake-Game-master/Snake.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Snake" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Snake" 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/Snake" 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 />
<envvars />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
22 changes: 22 additions & 0 deletions GAME/C-Snake-Game-master/Snake.depend
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# depslib dependency file v1.0
1593605426 source:c:\users\vishwa\desktop\snake\main.cpp
<iostream>
<conio.h>
<ctime>
"Snake.h"
"Food.h"

1593605123 c:\users\vishwa\desktop\snake\include\snake.h
<windows.h>
<vector>

1593605804 source:c:\users\vishwa\desktop\snake\src\snake.cpp
"Snake.h"

1593603835 source:c:\users\vishwa\desktop\snake\src\food.cpp
"Food.h"

1593603420 c:\users\vishwa\desktop\snake\include\food.h
<windows.h>
<cstdio>

30 changes: 30 additions & 0 deletions GAME/C-Snake-Game-master/Snake.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="src\Snake.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="565" topLine="12" />
</Cursor>
</File>
<File name="include\Snake.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="449" topLine="9" />
</Cursor>
</File>
<File name="include\Food.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="178" topLine="0" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1758" topLine="0" />
</Cursor>
</File>
<File name="src\Food.cpp" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="124" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
Binary file added GAME/C-Snake-Game-master/bin/Debug/Snake.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions GAME/C-Snake-Game-master/include/Food.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef FOOD_H
#define FOOD_H

#include <windows.h>
#include <cstdio>

#define WIDTH 50
#define HEIGHT 25

class Food
{
private:
COORD pos;

public:
void gen_food();

COORD get_pos();
};

#endif // FOOD_H
36 changes: 36 additions & 0 deletions GAME/C-Snake-Game-master/include/Snake.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef SNAKE_H
#define SNAKE_H

#include <windows.h>
#include <vector>

#define WIDTH 50
#define HEIGHT 25

using namespace std;

class Snake
{
private:
COORD pos;
int vel;
char dir;
int len;
vector<COORD> body;

public:
Snake(COORD pos, int vel);

void grow();
void move_snake();
void direction(char dir);

vector<COORD> get_body();

bool collided();
bool eaten(COORD food);

COORD get_pos();
};

#endif // SNAKE_H
94 changes: 94 additions & 0 deletions GAME/C-Snake-Game-master/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <iostream>
#include <conio.h>
#include <ctime>

#include "Snake.h"
#include "Food.h"

#define WIDTH 50
#define HEIGHT 25

using namespace std;

Snake snake({WIDTH/2, HEIGHT/2}, 1);
Food food;

int score;

void board()
{
COORD snake_pos = snake.get_pos();
COORD food_pos = food.get_pos();

vector<COORD> snake_body = snake.get_body();

cout << "SCORE : " << score << "\n\n";

for(int i = 0; i < HEIGHT; i++)
{
cout << "\t\t#";
for(int j = 0; j < WIDTH - 2; j++)
{
if(i == 0 || i == HEIGHT-1) cout << '#';

else if(i == snake_pos.Y && j+1 == snake_pos.X) cout << '0';
else if(i == food_pos.Y && j+1 == food_pos.X) cout << '@';

else
{
bool isBodyPart = false;
for(int k = 0; k < snake_body.size()-1; k++)
{
if(i == snake_body[k].Y && j+1 == snake_body[k].X)
{
cout << 'o';
isBodyPart = true;
break;
}
}

if(!isBodyPart) cout << ' ';
}
}
cout << "#\n";
}
}

int main()
{
score = 0;
srand(time(NULL));

food.gen_food();

char game_over = false;

while(!game_over)
{
board();

if(kbhit())
{
switch(getch())
{
case 'w': snake.direction('u'); break;
case 'a': snake.direction('l'); break;
case 's': snake.direction('d'); break;
case 'd': snake.direction('r'); break;
}
}

if(snake.collided()) game_over = true;

if(snake.eaten(food.get_pos()))
{
food.gen_food();
snake.grow();
score = score + 10;
}

snake.move_snake();

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
}
}
Binary file added GAME/C-Snake-Game-master/obj/Debug/main.o
Binary file not shown.
Binary file added GAME/C-Snake-Game-master/obj/Debug/src/Food.o
Binary file not shown.
Binary file added GAME/C-Snake-Game-master/obj/Debug/src/Snake.o
Binary file not shown.
9 changes: 9 additions & 0 deletions GAME/C-Snake-Game-master/src/Food.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "Food.h"

void Food::gen_food()
{
pos.X = (rand() % WIDTH - 3) + 1;
pos.Y = (rand() % HEIGHT - 3) + 1;
}

COORD Food::get_pos() { return pos; }
49 changes: 49 additions & 0 deletions GAME/C-Snake-Game-master/src/Snake.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "Snake.h"

Snake::Snake(COORD pos, int vel)
{
this->pos = pos;
this->vel = vel;

dir = 'n';
len = 1;

body.push_back(pos);
}

void Snake::direction(char dir) { this->dir = dir; }
void Snake::grow() { len++; }
COORD Snake::get_pos() { return pos; }

vector<COORD> Snake::get_body() { return body; }

void Snake::move_snake()
{
switch(dir)
{
case 'u': pos.Y -= vel; break;
case 'd': pos.Y += vel; break;
case 'l': pos.X -= vel; break;
case 'r': pos.X += vel; break;
}

body.push_back(pos);
if(body.size() > len) body.erase(body.begin());
}

bool Snake::collided()
{
if(pos.X < 1 || pos.X > WIDTH-2 || pos.Y < 1 || pos.Y > HEIGHT-2) return true;

for(int i = 0; i < len-1; i++)
{
if(pos.X == body[i].X && pos.Y == body[i].Y) return true;
}
return false;
}

bool Snake::eaten(COORD food)
{
if(pos.X == food.X && pos.Y == food.Y) return true;
return false;
}