Snake xenzia nokia game play online12/3/2023 Now those variables cannot be accessed outside the file where they were declared. 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. If so, you should still try to minimize scope. In your game, you could have passed those variables as arguments to the functions.īut suppose you want to stick with the globals. Since you didn't use classes, you have ended up with some global variables in the file. Some classes like SnakeGame, Board/ Map and Food come to mind. You should start out by refactoring the code into a few classes. Your program is basically structured programming, which look a lot more like C than C++. OOP:įirst consideration is that for a C++ program we expect to see some Object Oriented Programming - OOP. This review is going to be mostly on code style and general code quality improvements. ![]() 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. It's not that big a deal since you have 2, but something to bear in mind. I'd just suggest little things like alphabetizing your #includes and function prototypes. Other than this, things seem pretty good, so I'm going to start nit-picking :P. 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!). 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. 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 ints with some extra stuff, so you can easily compare in the way that you're doing now. 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. Other than that, I'd possibly say that you need to have another variable alongside food which is snakeLength or something. ![]() ![]() e.g, If the loop takes 0.1s, and you want 2fps, then put in the sleep of 0.4s. 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. Unless you know how long the game loop will be on every computer, making your sleep a constant is generally bad practice. Places the initual head location in middle of map Change to direction determined by key pressed Amount of food the snake has (How long the body is) How could it be improved, and what general advice would be useful for future projects? #include This is my version of the Snake game, written in C++.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |