Hollywood best songs ever free download mp3. This is my version of the Snake game, written in C++. How could it be improved, and what general advice would be useful for future projects?
Active3 years, 7 months ago
$begingroup$Pimgd
21.4k55 gold badges5959 silver badges142142 bronze badges
FunkyFunky
70511 gold badge77 silver badges1616 bronze badges
$endgroup$
To create a snake game in visual studio. To create and manage an array of snake parts in the game. To spawn and respawn food across the screen. To detect hit test with the border and snakes own body. Start and restart the game. Keep score in the game. Manage the project and follow good programming practice by using comments and indentation. Please need help with this homework. The following code performs a snake game using graphics.h library in turbo c compiler.please add two more features. Using an '+' character to increase velocity or reduce delay and also an option to.
2 Answers
Unless you know how long the game loop will be on every computer, making your
sleep a constant is generally bad practice. If you know that you want 2fps, a good way to keep it in line is get the time at the start of the game loop, then at the end, find out the difference, and use that to calculate the amount of time needed to sleep to keep the step the same. e.g, If the loop takes 0.1s, and you want 2fps, then put in the sleep of 0.4s.
Other than that, I'd possibly say that you need to have another variable alongside
food which is snakeLength or something. I don't know if you're printing out the score on the screen, but if you are keeping track of the score, I'd imagine that you want it to start at 0, as opposed to 3, and 1 more int isn't that big a deal when you get better readability.
Possibly consider making
direction an enum, with UP, DOWN, LEFT and RIGHT, because right now it's a little tricky to follow, and you wouldn't need to change too much of the logic, as enums are int s with some extra stuff, so you can easily compare in the way that you're doing now. Having said that, I'm not sure I follow what your direction values relate to, as I don't see anywhere that direction is set to 5, so that check seems unnecessary.
In your
generateFood function, you access map directly, where you've made a function which does that job exactly in getMapValue , so you might want to consider using that, as at some point in the future you may decide to make it a Map class and then you'll run into errors with accessing private variables (I'd hope!).
Other than this, things seem pretty good, so I'm going to start nit-picking :P. I'd just suggest little things like alphabetizing your YannYann
#include s and function prototypes. It's not that big a deal since you have 2, but something to bear in mind. As well as that, your clearScreen() and printMap() feels a lot like a Draw() , so you could possibly wrap them both up in that function, and just call init , update , draw and cleanup (when you're doing object loading and using pointers and whatnot) since you seem to be nearly following the game loop pattern (as an aside, if you are planning to make more games, read that entire book, it's a thing of beauty), and reading that article better explains my point about sleep .
1,89611 gold badge1111 silver badges3535 bronze badges
$endgroup$$begingroup$
This review is going to be mostly on code style and general code quality improvements.
OOP:
First consideration is that for a C++ program we expect to see some Object Oriented Programming - OOP. Your program is basically structured programming, which look a lot more like C than C++.
You should start out by refactoring the code into a few classes. Some classes like
SnakeGame , Board /Map and Food come to mind.
Global variables:
Since you didn't use classes, you have ended up with some global variables in the file. Globals should be avoided in most cases. In your game, you could have passed those variables as arguments to the functions.
But suppose you want to stick with the globals. If so, you should still try to minimize scope. If the variables are not needed outside the file where they were declared, then you should wrap them inside an unnamed namespace, to make the variables file scoped. Like this:
Now those variables cannot be accessed outside the file where they were declared.This reduces the chance of undesired state changes by other modules and makes debugging a little easier.
Also, it is preferable to always initialize global variables to some default safe value. These variables:
Should be initialize to something.
Naming tidbits:
A preferred naming convention for constants, like
mapwidth and mapheight , is ALL_UPPERCASE . This helps readability by clearly differentiating from mutable variables to compile-time constants.
Also,
TOTAL_MAP_SIZE is more descriptive than just size .
Miscellaneous:
You could avoid the function prototypes by just placing
main() at the end of the file.
getMapValue() could be simplified by replacing the if with a default case in the switch:
Portability:Synopsis Of Snake Game In C Languageconio.h is unfortunately Windows only, so this code won't compile on other OSs.I don't know of any simple replacement to kbhit() and getch() but _sleep() can be replaced by std::this_thread::sleep_for() in C++11.
system('cls') is also not part of the C++ standard and therefore not portable.
15.8k44 gold badges2424 silver badges8484 bronze badges
$endgroup$
protected by Jamal♦Jul 30 '15 at 5:04
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead? Not the answer you're looking for? Browse other questions tagged c++beginnergamesnake-game or ask your own question.
Project: Classic Snake Game
– To download it for free (scroll down)
This classic arcade game is developed using C# Language. Itis a common game where the player has to eat Food which will be available in different Coordinates. The more you eat food, more you’ll get score points. Player has to move the direction of the snake by using Left, Right, Up, Down Arrow Keys and Backspace key to Pause the game/return to the main menu. From the settings: Graphics resolution can be maintained and difficulty level, as well as sound level, can also be maintained. This gaming project is easy to operate and understand by the users.
DOWNLOAD SNAKE GAME IN C# WITH SOURCE CODE : CLICK THE BUTTON BELOWYou May Also LikeSimple Banking System In C++ With Source Code
Project: Simple Banking System In C++ -to download simple banking system in c++ project for free(Scroll Down) The…
Staff Management System In C++ With Source Code
Project: Staff Management System in C++ with source code - To download the Staff Management System project for…
Vehicle Seat Reservation System in C++ With Source Code
Project: Vehicle Seat Reservation System in C++ with source code - To download the Vehicle Seat Reservation System…
Simple Text Editor In C# (updated) With Source CodeSimple Snake Game Code In C Language
Project: - Simple Text Editor In C# (updated) with source code -To download simple text editor in c#…
Snake Game In C Language Source CodePong Game In C# With Updated Version With Source CodeProject Report On Snake Game In C Language
Project: Pong Game In C# With Updated Version With Source Code About Project The pong game in C#…
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |