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 new game (Snake game) #5166

Closed
wants to merge 3 commits 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
75 changes: 75 additions & 0 deletions Games/Snake-Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

# Advanced Snake Game in C++

This is an advanced version of the classic Snake game implemented in C++. It includes features such as user authentication, a loading screen, a larger game grid, and adjustable snake speed. The game is played in the console and is a great project for learning and practicing C++ programming.

## Features

- **User Authentication**: Simple login system with username and password validation.
- **Loading Screen**: A loading effect displayed before the game starts.
- **Larger Grid**: The game grid is expanded to 40x20 for more challenging gameplay.
- **Adjustable Speed**: The snake's speed is adjustable, allowing for a more customized experience.
- **Collision Detection**: The game ends when the snake collides with itself.

## Getting Started

### Prerequisites

- **C++ Compiler**: You need a C++ compiler installed on your system (e.g., GCC, Visual Studio).
- **Windows**: Ensure you have the `<conio.h>` and `<windows.h>` headers available.

### Installation

1. **Clone the repository**:
```bash
git clone https://github.com/rugved0102/Snake-Game.git
cd snake-game-cpp
```

2. **Create a `users.txt` file** in the project directory with the following format:
```
username1 password1
username2 password2
```

### Compilation

To compile the game, run the following command:

```bash
g++ -o snake snake.cpp -std=c++11
```

### Running the Game

After compiling, run the game using:

```bash
./snake
```

### Gameplay Instructions

- **Controls**: Use the `W`, `A`, `S`, and `D` keys to move the snake up, left, down, and right, respectively.
- **Objective**: Guide the snake to eat the fruit (`F`) to grow longer while avoiding collisions with itself.

## Files Included

- **snake.cpp**: The main C++ source code file for the game.
- **users.txt**: A text file storing user credentials for login.

## Future Enhancements

- Implement levels with increasing difficulty.
- Add a high-score system to track the best scores.
- Introduce new obstacles and power-ups for more engaging gameplay.
- Implement a graphical user interface using a library like SDL or SFML.

## Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your changes.

## Acknowledgments

- Inspired by the classic Snake game.
- Thanks to the C++ community for providing excellent resources and tutorials.
178 changes: 178 additions & 0 deletions Games/Snake-Game/game.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h> // For Sleep function

using namespace std;

bool gameOver;
const int width = 40; // Increased grid size
const int height = 20; // Increased grid size
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;

void LoadingScreen() {
system("cls");
cout << "Loading";
for (int i = 0; i < 5; i++) {
cout << ".";
Sleep(500);
}
cout << endl;
}

bool Login() {
string username, password, storedUsername, storedPassword;
cout << "Enter Username: ";
cin >> username;
cout << "Enter Password: ";
cin >> password;

ifstream userFile("users.txt");
while (userFile >> storedUsername >> storedPassword) {
if (username == storedUsername && password == storedPassword) {
return true;
}
}
return false;
}

void Setup() {
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
nTail = 0;
}

void Draw() {
system("cls");
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;

for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O"; // Snake head
else if (i == fruitY && j == fruitX)
cout << "F"; // Fruit
else {
bool print = false;
for (int k = 0; k < nTail; k++) {
if (tailX[k] == j && tailY[k] == i) {
cout << "o"; // Snake tail
print = true;
}
}
if (!print) cout << " ";
}

if (j == width - 1)
cout << "#";
}
cout << endl;
}

for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
cout << "Score: " << score << endl;
}

void Input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
if (dir != RIGHT) dir = LEFT;
break;
case 'd':
if (dir != LEFT) dir = RIGHT;
break;
case 'w':
if (dir != DOWN) dir = UP;
break;
case 's':
if (dir != UP) dir = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}

void Logic() {
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}

// Game over conditions
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameOver = true;

// Snake eats the fruit
if (x == fruitX && y == fruitY) {
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}

int main() {
if (!Login()) {
cout << "Invalid credentials!" << endl;
return 0;
}

LoadingScreen();
Setup();

while (!gameOver) {
Draw();
Input();
Logic();
Sleep(150); // Slower snake speed
}

return 0;
}
Binary file added Games/Snake-Game/game.exe
Binary file not shown.
2 changes: 2 additions & 0 deletions Games/Snake-Game/users.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
user1 password1
user2 password2
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ This repository also provides one such platforms where contributers come over an
| [Prime-No-Finder-Game] (https://github.com/kunjgit/GameZone/tree/main/Games/Prime-No-Finder-Game)|
| [Synonym-Typing-Game] (https://github.com/kunjgit/GameZone/tree/main/Games/Synonym-Typing-Game)|
| [Color-Blink-Challenge] (https://github.com/kunjgit/GameZone/tree/main/Games/Color-Blink-Challenge)|
| [Snake-Game] (https://github.com/kunjgit/GameZone/tree/main/Games/Snake-Game)|
</center>
<br>
<p align="right"><a href="#top">Back to top</a></p>
Expand Down
Binary file added assets/images/Snake-Game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading