[go: up one dir, main page]

0% found this document useful (0 votes)
23 views15 pages

ADSA Report UID-75,47,69

report for dsa

Uploaded by

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

ADSA Report UID-75,47,69

report for dsa

Uploaded by

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

PROJECT NAME

SNAKE GAME 2D

A PROJECT REPORT

Submitted by
NANDAN 23BET10075
AADIT 23BET10069
ISHANT 23BET10047

in partial fulfillment for the award of the degree of

BACHELOR OF ENGINEERING

IN

INFORMATION TECHNOLGY

Chandigarh University
TABLE OF CONTENTS

List of Figures.............................................................................................................

CHAPTER 1. INTRODUCTION ...........................................................................


1.1. Introduction to Project................................................................................................... 5

1.2. Identification of Problem ............................................................................................... 6

CHAPTER 2. BACKGROUND STUDY ............................................................. 7


2.1. Existing solutions ............................................................................................................ 7

2.2. Problem Definition .......................................................................................................... 8

2.3. Goals/Objectives.............................................................................................................. 8

CHAPTER 3. DESIGN FLOW/PROCESS ......................................................... 9


3.1. Evaluation & Selection of Specifications/Features ....................................................... 9

3.2. Analysis of Features and finalization subject to constraints....................................... 9

3.3. Design Flow ....................................................................................................................12


CHAPTER 4. RESULTS ANALYSIS AND VALIDATION ........................... 13
4.1. Implementation of solution ...........................................................................................13

CHAPTER 5. CONCLUSION AND FUTURE WORK .................................. 15


5.1. Conclusion ......................................................................................................................15

5.2. Future work.......................................................................................................................


R

ACKNOWLEDGMENT

I would like to express my sincere gratitude to everyone who supported and guided me throughout
this project. First, I would like to extend my heartfelt thanks to my project supervisor, Monika devi ,
for their invaluable guidance, encouragement, and insightful feedback. Their expertise in
programming and game development helped shape this project and deepened my understanding of
software design principles. I am also thankful to my peers and friends, who provided feedback, tested
the game, and offered helpful suggestions that contributed significantly to improving the final
product. Their support and encouragement motivated me to strive for quality and pay attention to
details throughout the development process. Additionally, I would like to acknowledge the resources
and facilities provided by Chandigarh University, which made it possible to work on this project
effectively. Access to resources such as [mention any specific software, tools, or library support, if
applicable] was instrumental in bringing this project to completion. Finally, I would like to thank my
family for their unwavering support and encouragement. Their belief in my abilities has been a
constant source of motivation.

Thank you all for your invaluable contributions to this project.

signature
R

CHAPTER 1 INRODUCTION

1.1 INTRODUCTION TO PROJECT


The 2D Snake game is a classic arcade game popularized in the late 1970s and 1980s, and it
remains a fun and engaging experience to develop and play. The player controls a "snake" that
moves across a grid, eating food items that randomly appear. When the snake eats a food item,
it grows in length, increasing the game's difficulty. The game ends if the snake collides with
itself or the boundaries of the grid.

1.2 IDENTIFICATION OF PROBLEM

The classic Snake game, while simple, presents an engaging problem that involves several
foundational programming concepts such as data structures, real-time input handling, and
collision detection. The problem tackled by this project is twofold:

1. Game Development Problem: Developing a dynamic, real-time game requires understanding


and implementing interactive components like movement, user input, and game state
management. The game logic must accurately manage the snake’s movement across a grid,
the growth of its body, and interactions with both food and obstacles (including itself).

2. Algorithmic and Logical Challenges: The Snake game also poses several computational
challenges:

• Efficient Collision Detection: The game must detect collisions quickly to avoid lagging
in response to player input. This requires a system that tracks the snake’s body positions
and boundaries in real time.

• Randomized Food Placement: Food must appear at random grid locations without
overlapping with the snake, necessitating both randomness and careful positioning.

• Growing Structure Management: As the snake grows, the program must efficiently
handle and store the additional body segments in a way that allows smooth, continuous
movement.

CHAPTER 2 BACKGROUND STUDY


R

2.1 EXISTING SOULTIONS


The Snake game has been a staple in gaming since the late 1970s, evolving through various
platforms and implementations. Here are some of the prominent existing solutions:

1. Classic Mobile Snake (Nokia): One of the most popular versions of the Snake game was
pre-installed on Nokia mobile phones in the late 1990s and early 2000s. This game had
a simple grid, a steadily increasing speed, and boundary-wrapping, where the snake
could move through one edge of the screen to reappear on the opposite side.

2. Arcade and PC Versions: Numerous PC and arcade versions of the game have been
developed, often with minor tweaks like adding obstacles, varying speeds, or unique
scoring systems. Some versions introduce more complex graphics and sound, aiming to
improve player engagement.

3. Modern Online and Mobile Variants: Online platforms and mobile applications have
brought new life to the Snake game, with variations that include power-ups, multiplayer
options, and complex mazes. These modern versions often include updated graphics and
customizable settings for an enhanced user experience.

While each of these solutions provides entertainment and variations on gameplay, they are
often closed systems that don’t encourage customization or learning. Therefore, developing
a new Snake game offers the chance to enhance the basic structure and introduce features
that can be modified and extended, making it suitable as an educational project or a
foundational gaming application.

2.2 PROBLEM DEFINITION

Problem Definition: The objective of this project is to develop a customizable, console-


based 2D Snake game in C++. The project must address key gameplay elements, including
snake movement, real-time user input handling, and efficient collision detection, with an
additional focus on achieving a clean, modular design that can easily be extended or
modified.

Core Problem Areas:


R

1. Snake Growth Management: As the snake consumes food, it should grow accurately
while maintaining smooth movement across a grid.

2. Collision Detection and Game Over Conditions: Implement efficient collision detection
to end the game if the snake collides with itself or with the boundaries.

3. Food Generation and Positioning: Ensure food items are placed randomly within the grid
without overlapping with the snake’s body.

4. Real-time Input Handling: Capture user input (up, down, left, right) in real-time to
control the snake without delays.

2.3 GOALS/OBJECTIVES

The primary goals and objectives of this project are outlined below:

1. Implement Core Gameplay Mechanics: Create a functioning 2D Snake game where the player
can control the snake, consume food to grow, and avoid obstacles.

2. Develop Efficient Collision Detection: Implement an efficient system for detecting collisions
with both the grid boundaries and the snake’s own body.

3. Ensure Smooth, Real-time Input Handling: Develop a responsive input system to handle
player controls without noticeable lag.

4. Create a Modular Codebase for Future Enhancements: Use a modular approach in coding to
allow future enhancements, such as adding levels, increasing speed with score, or introducing
obstacles.

5. Provide a Console-based UI for Accessibility: Design the game to be accessible in a console-


based environment to avoid dependencies on external libraries, making it suitable for
beginners learning C++.

6. Test for Game Balance and Functionality: Test thoroughly to ensure the game is balanced,
with smooth gameplay, proper collision mechanics, and functional growth and scoring
mechanisms.

CHAPTER 3 DESIGN FLOW/PROCESS


R

3.1 Evaluation & Selection of Specifications/Features

The initial step in designing the 2D Snake game involves identifying and evaluating possible features
to include, followed by selecting the most feasible and essential ones. Since this project aims for a
console-based game in C++, our feature set is restricted to those that are functional and perform well
in a text-based environment.

Primary Features Considered:

1. Snake Movement and Growth:

• Core feature for player control and game progression.

• Must handle continuous movement in four directions (up, down, left, right).

• Growth upon consuming food to increase the challenge.

2. Food Placement:

• Random generation of food items on the grid.

• Ensure food does not overlap with the snake’s body.

3. Collision Detection:

• Detect collisions with the grid boundaries and with the snake’s own body.

• Trigger game over conditions upon collision.

4. Score Tracking:

• Track and display score based on the amount of food consumed.

• Optionally, increase the difficulty level as the score increases.

5. Real-time Input Handling:

• Immediate response to directional input (up, down, left, right) for smoother gameplay.

• Continuous game movement without waiting for input.

6. Game Display (Grid Representation):

• Display the game grid with boundaries, snake, and food using characters in a console
interface.

Additional Optional Features (evaluated but not selected for this basic version):
R

• Increasing Speed with Score: While adding difficulty by increasing speed could improve
gameplay, it may complicate the implementation and require more fine-tuning.

• Obstacles: Adding obstacles increases difficulty but requires extra collision detection and
would clutter a console-based interface.

3.2 Analysis of Features and Finalization Subject to Constraints

After evaluating the features, we analyzed them further in terms of the constraints associated with a
C++ console application:

1. Memory Constraints:

• Since the game will be played on a grid, storing snake position and tracking its growth could
require significant memory for larger grids. Therefore, we limit the grid size to a manageable
width and height, such as 20x20 cells.

2. Real-Time Input Handling:

• Console applications in C++ have limited support for non-blocking input. Implementing real-
time input requires using functions like _kbhit() and _getch() (from conio.h) to capture user
input without halting the game loop.

3. Performance:

• Console output can be slow, especially if the screen needs to be redrawn frequently. To handle
this, system("cls") is used to clear the screen quickly before each frame.

4. Modularity:

• The design should support modularity, allowing future enhancements. Each core function,
such as movement, input, and drawing, is isolated in separate functions to make the codebase
easier to modify.

5. Finalized Features: Based on these constraints, the final features selected for this implementation
are:

• Snake movement and growth

• Random food placement

• Collision detection (wall and self-collision)


R

• Score tracking

• Real-time input handling

• Console-based grid display

3.3 Design Flow

The design flow represents the sequence of steps and functions in the Snake game, ensuring smooth
gameplay and efficient handling of all features.

1. Design Flow Diagram

• Game Setup: Initialize game variables (snake position, food position, score, and tail length).

• Main Game Loop:

• Draw Function: Clear and redraw the game grid with the snake, food, and boundaries.

• Input Handling: Capture player input and set the snake’s direction.

• Game Logic: Update snake position, check for collisions, and manage score and tail growth.

• Delay (Sleep): Control game speed by adding a short delay at the end of each loop iteration.

Below is a breakdown of each step in the design flow:

1. Game Setup (Setup Function):

• This function initializes the game variables:

• The snake starts in the center of the grid.

• The initial food position is randomly placed.

• The score is set to 0, and the tail length is initialized.

2. Draw Function:

• This function renders the game grid, updating the console to reflect the current state.

• Steps involved:

• Clear the screen.


R

• Draw the top boundary of the grid.

• For each cell, determine if it contains the snake’s head, body, food, or is empty.

• Draw the left and right boundaries.

• Display the player’s score.

3. Input Handling (Input Function):

• Captures the player’s direction input in real time.

• Uses _kbhit() to detect if a key was pressed and _getch() to read the key without halting the
game.

• Updates the snake’s direction based on the player’s input.

4. Game Logic (Logic Function):

• Handles the main game mechanics, including:

• Movement: Updates the snake’s head position based on the direction.

• Tail Management: Updates the position of each tail segment to follow the head.

5. Collision Detection:

• Checks if the snake has collided with the grid boundary or with itself.

• If a collision is detected, the game ends.

6. Food Consumption:

• If the snake’s head moves over the food, increase the score, grow the tail, and randomly
generate new food.

7. Game Loop with Delay:

• The main game loop iterates continuously, calling Draw, Input, and Logic functions.

• A short delay using _sleep() (or std::this_thread::sleep_for in modern C++) controls the speed
of the snake, making gameplay manageable.

3.4 OUTPUT
R
R

CHAPTER 4 RESULTS ANALYSIS AND VALIDATION

4.1 IMPLEMENTATION AND SOLUTION

Results Analysis
1. Gameplay and User Interaction:
• The snake responds instantly to player input, and the real-time input handling
improves gameplay. Food items are correctly generated at random positions and
do not overlap with the snake, meeting all design criteria.
2. Score Tracking and Growth:
• The score increases each time the snake consumes food, and the snake grows as
intended. This contributes to an escalating challenge as the game progresses.
3. Collision Detection:
• The game detects collisions with the boundaries and the snake’s body, effectively
R

ending the game upon collision. This feature works consistently, providing an
accurate game-over condition.
4. Console Display:
• The console display successfully shows all elements (snake, food, and
boundaries), making the game playable in a simple, text-based environment.
Validation
To validate the success of the implementation, the game was tested under various
conditions:
• Boundary Collisions: The game reliably ended when the snake hit the boundary,
confirming that wall collision detection is functioning.
• Self-Collision: Self-collision detection accurately triggered game-over conditions
when the snake ran into itself.
• Food Consumption and Growth: The snake’s growth and score increase
functioned correctly with each food item consumed, verifying that food collection
and scoring are working as intended.

CHAPTER 5 CONCLUSION NAD FUTURE WORK


5.1 Conclusion
The project successfully delivered a functional 2D Snake game in C++, designed to run in a console
environment. The implementation met the primary objectives established at the beginning of the
project, including:
1. Core Gameplay Mechanics: The Snake game features smooth player-controlled movement,
food collection, and score tracking. Each time the snake consumes food, it grows in length,
increasing the game's challenge level, which is essential to the classic Snake experience.
2. Real-Time Input Handling: Real-time input detection allowed for responsive and smooth
control of the snake. The game’s responsiveness contributes significantly to the player’s
experience, ensuring the gameplay feels intuitive and engaging.
3. Collision Detection and Game Over Conditions: Effective collision detection ensures that the
game ends when the snake hits the grid boundary or collides with itself. This core functionality
provides an accurate representation of traditional Snake gameplay, where players must avoid
obstacles as they progress.
R

4. Modular and Extendable Code Structure: The code was organized into modular functions for
each gameplay element, such as Setup(), Draw(), Input(), and Logic(). This modular design not
only facilitated the game’s development but also created a solid foundation for future
expansions and improvements.
5.
Overall, the project achieved its goal of creating a simple, console-based Snake game that is easy to
play, challenging, and enjoyable. It serves as an excellent example of using basic programming
techniques in C++ to develop a real-time, interactive game. The final product is well-suited for
educational purposes, particularly for those learning about game design fundamentals, data structures,
and real-time input handling in a console environment.

5.2 Future Work


While the project successfully delivered a playable Snake game, there are several opportunities to
expand and enhance its functionality and user experience. Some potential areas for future work include:
1. Enhanced Graphics and User Interface:
• Integrating a graphical user interface (GUI) using a library such as SDL (Simple Direct
Media Layer) or SFML (Simple and Fast Multimedia Library) would improve the visual
experience, moving beyond simple console-based graphics.
• Adding visual effects, such as animations for snake movement and food collection,
would make the game more engaging.
2. Difficulty Levels and Speed Control:
• Introducing different difficulty levels with varying snake speeds and grid sizes would
allow players to select a level that suits their skill.
• Dynamic speed adjustment could be added, where the snake’s speed increases gradually
as the score increases, making the game more challenging over time.
3. Obstacles and Power-ups:
• Adding randomly placed obstacles on the grid would create additional challenges for
the player.
• Introducing power-ups (e.g., temporary invincibility, speed boost, or score multipliers)
could add variety to the gameplay and encourage strategic decision-making.
4. Multiplayer Mode:
• Implementing a multiplayer mode with either local or online play would allow players
to compete against each other on the same grid, increasing the game’s appeal.
R

• Possible modes include cooperative gameplay, where players work together to collect
food without colliding, or competitive mode, where each player tries to collect more
food than the other.
5. High Score Tracking and Game Saving:
• Adding a high score tracker would allow players to save their best scores across
sessions, enhancing replay ability.
• A save and load feature could allow players to pause and resume their game.
6. Enhanced AI and Autonomous Snake Mode:
• Creating an AI-controlled snake that could play autonomously would add a layer of
interest for educational and demonstration purposes. This AI could navigate the grid,
find paths to the food, and avoid obstacles, showcasing algorithmic approaches in game
development.
7. Mobile and Web Versions:
• Expanding the game to mobile platforms or a web-based version would make it
accessible to a wider audience.
• Frameworks like HTML5/JavaScript for web or C++ with cross-platform support for
mobile could be used to port the game, ensuring it reaches more players and serves as a
learning resource for various platforms.

You might also like