Corona Game

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Advar
Posts: 4
Joined: May 8th, 2020, 7:32 pm

Corona Game

Post by Advar » May 8th, 2020, 7:56 pm

Been following along in the beginner series and have made a game about Corona with all the extra time I've been left with:
CoronaGame.zip
(4.07 MiB) Downloaded 171 times
If you want to check out the source code it can be found here:
https://github.com/K-Sunderland/Corona-Game
(It's my first time using git, and I really don't know what I'm doing)

Just thought this game might bring joy to the small number of people who will ever play it.
Enjoy!
:D :D

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Corona Game

Post by albinopapa » May 8th, 2020, 9:11 pm

Nicely done.

I have a few suggestions about the code. I don't know how far you are into the tutorials, so I don't know if any of this will make sense.

You can look up finite-state machines. These are very helpful in organizing your code into states. For instance, OpeningState, InitializiationState, GameRunState, GameOverState. This means you can switch between states with few if/else if/else calls, because the only things each state is concerned with is bundled together. The only other thing each state section of code cares about is what causes a state change. For instance, you have a check if user presses enter to move from each state like title screen to choosing difficulty to level transition to starting the leve.

The second suggestion would be to use the Keyboard::ReadKey() function ( called by wnd.kbd.ReadKey() ). This allows you to stop keeping track of when a key is down or up from the previous frame. This can clean up some of your code as well.

The third suggestion is separating your state handling into their own functions like void HandleTitleScreenState(), HandleInitState(), and so on. This makes debugging a lot easier since the code pertaining to a particular state is contained within a smaller section of code instead of one massive function that the Game::UpdateModel has turned into.

The fourth suggestion is to keep with the idiom of not mixing logic and render/drawing operations. The Game::UpdateModel function should be where all the game logic should go and the Game::ComposeFrame is where all the draw functions are called from. This can also be broken up into state draw functions like DrawTitleScreenState, DrawInitState, DrawDifficultyState, and so on.

While this may be a toy project and therefore not merrit any changes, these are suggestions that can be used going forward on other projects in the future to keep your code organized. The idea is twofold. One it should make debugging easier and two if you need help from someone and you have to share your code, it makes your code easier to reason about. If someone can say "Okay, things poop out during level transition, I just need to look at the code that deals with that". If you have a HandleLevelTransitionState() function that's where they need to look and not through the 400 or so lines of code in the Game::UpdateModel() function.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Corona Game

Post by albinopapa » May 8th, 2020, 9:15 pm

Oh, and std::random_shuffle has apparently been removed in C++17 in favor of just std::shuffle.

Usage differs a bit from std::random_shuffle

Code: Select all

    std::random_device rd;
    std::mt19937 g(rd());
 
    std::shuffle(v.begin(), v.end(), g);
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Advar
Posts: 4
Joined: May 8th, 2020, 7:32 pm

Re: Corona Game

Post by Advar » May 8th, 2020, 10:24 pm

Thanks so much for the suggestions! I'll take them into consideration and continue working on it.
Hope you have a wonderful quarantine!
:D :D

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Corona Game

Post by albinopapa » May 9th, 2020, 8:02 am

I can't help myself.

Figured I'd refactor the code to use enums for a state machine with your project. There are some things in there that you may not have covered yet especially in the Game::HandleInitializeState() function. I don't know if you've made it to where chili talks about lambda functions, if not it may confuse you but at least you get exposed to it.

The .rar file is only the Game.h and Game.cpp files as that's really all I changed.

EDIT: I think I have all the logic the same, but I can't be sure. Either way, you can use it as an outline more than anything.
Attachments
Engine.rar
modified Game.h and Game.cpp files
(3.47 KiB) Downloaded 158 times
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Advar
Posts: 4
Joined: May 8th, 2020, 7:32 pm

Re: Corona Game

Post by Advar » May 10th, 2020, 4:23 am

You didn't have to do that... But thank you! Your code is extremely sexy ;). I couldn't get it to build - got this weird error
Clamp() Error.PNG
Clamp() Error.PNG (4.04 KiB) Viewed 3489 times
Error Details.PNG
(5.46 KiB) Not downloaded yet
Algorithm is included. Hmmm. I try commenting out the line and it all worked fine, didn't notice anything askew. What did that line do?
:D :D

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Corona Game

Post by albinopapa » May 10th, 2020, 5:19 am

The difficulty_index was the Easy, Normal, Hard options in that menu, the std::clamp is only available when you have the C++ language standard set to C++17 or latest standard in the project properties. It clamps the first parameter between the second and third parameters inclusive ( so in this case 0, 1, 2 ).

This would be the same as:
difficulty_index = std::min( 0, std::max( difficulty_index, 2 ) );

I know I didn't have to do what I did, but it seemed like a good way to show the things I described, plus I think I learned something from doing it.

I've talked about state machines before here on this forum actually: State Machines. I cover different methods of dealing with states from bools to some C++17 features.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Advar
Posts: 4
Joined: May 8th, 2020, 7:32 pm

Re: Corona Game

Post by Advar » May 10th, 2020, 5:30 am

I'm glad you did it, though. I think I learned a lot from it (seeing what good-structured code looks like). Google didn't really show it how you did. I changed the standard to 17 and it built right this time. I'm about at the snake game part of the tutorials and got way in over my head on this game. My dad does all his stuff in VB.net and he gave me a couple of suggestions, and stack overflow helped with the rest, but I think seeing your code helped more than anything. It's a lot easier seeing it in front of you, used in context. I'm excited to see what the rest of the tutorials have to offer. Knowing Chili, it will be awesome.
:D :D

Post Reply