A Little help, if possible... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

A Little help, if possible...

After completing the Ben Ryves Tutorial and finally completing half of the MSDN Windows Programming Tutorial, I have decide to start working on a Snake game in C++! So far I can make the arena, some 10-12 levels ( Finite State Machine - Thanks to the tutorials shared by jay and help from many others), and the snake and random cherries. The game will increase the length of the snake by 1 whenever he eats a cherry... But, there is a problem that I am unable to work out a code for the dying scenario...

6th Jun 2017, 4:30 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
104 Answers
+ 8
oh btw. The levels could probably all be in one state. you could load levels layouts from a file or randomly generate them available states would be something like: intro menu level options exit that way it will be much easier to add new levels
6th Jun 2017, 5:54 AM
jay
jay - avatar
+ 8
lol. we about the same progress. Mine moves too fast by default though
6th Jun 2017, 8:09 AM
jay
jay - avatar
+ 7
the snake is alllive! got mine appearing and responding to input.. but it has exponential growth lol. (every move the snake grows). more work required. How goes you Kinshuk?
6th Jun 2017, 7:59 AM
jay
jay - avatar
+ 7
i havent done the length bit yet either. it just grows by itself 😢
6th Jun 2017, 8:12 AM
jay
jay - avatar
+ 7
not yet. I have only got the head displaying.. so you are ahead of me!
6th Jun 2017, 8:14 AM
jay
jay - avatar
+ 7
It continues on its own. What I did was store the current direction in a variable. Then in move function check which direction is current and +\- x y based on that. this continues the movement of the snake
8th Jun 2017, 10:36 AM
jay
jay - avatar
+ 7
😆 ta. Yeah it is still very drafty! Lemme know improvements! I like improvements
8th Jun 2017, 9:22 PM
jay
jay - avatar
+ 6
Nice! Well Done. Here is a suggestion. You track the head position right? you have an index array that holds the info on what pixel/ascii char is where. so based on this. if head and body are at the same x y coord. Dead. if head = wall then dead.
6th Jun 2017, 4:36 AM
jay
jay - avatar
+ 6
That gamemqking itself is pure awesomeness. I am still stuck in my little world of Python 🐍.
6th Jun 2017, 5:06 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
ReadConsoleInput
8th Jun 2017, 8:28 AM
jay
jay - avatar
+ 6
lemme know when u grabbed a copy
8th Jun 2017, 8:33 AM
jay
jay - avatar
+ 6
That version has globals. ick. I had to do it that way though to get it into one file.. circular references and junk in Window and Gamestates.. But thanks! Hope it helps you with ideas for your version and the booking system you are designing
8th Jun 2017, 8:59 AM
jay
jay - avatar
+ 6
Its a place that I put the final drawing code. As it is part of gamestate it gets called every loop. (along with logic and handle input) It makes drawing to the screen unified across all gamestates. It is also where you could control movement speed (as you can see it goes pretty fast now as that code is still missing)
8th Jun 2017, 9:03 AM
jay
jay - avatar
+ 6
check for only keydown events. eventBuffer.Event.KeyEvent.bKeyDown for a clearer understanding see the Paint Code I uploaded. It is alot clearer than in the snake game
8th Jun 2017, 9:05 AM
jay
jay - avatar
+ 6
I just added that otherway just incase you ever needed it. what you said is correct for snake. lol thats an awesome idea. maybe a super cherry that gives you a laser for x seconds!
8th Jun 2017, 9:20 AM
jay
jay - avatar
+ 6
In my getting inputs function the only thing I do is set the current direction variable I have another function called move this is what is actually called each loop in logic. In the move function I do a switch statement on the current direction variable. if current direction is up then y = - 1 if down y = +1 etc. i.e int dy, dx; switch(currentDir) { case UP: dy = -1 } newY = oldY + dy; does this make sense?
8th Jun 2017, 1:45 PM
jay
jay - avatar
+ 6
No bother friend!
8th Jun 2017, 1:49 PM
jay
jay - avatar
+ 6
sorry finger slipped
8th Jun 2017, 1:52 PM
jay
jay - avatar
+ 6
I take it you are not using game states. Wow That is cool code. I am unsure why it needs a loop though. But just seeing that, should work. looks the same as I am doing it (with a loop)
8th Jun 2017, 2:00 PM
jay
jay - avatar
+ 6
This is why I used the state engine. Because we can seperate everything into three main functions easily. This can also be done in just a main single file. You want three main functions in your loop. handle input, (for getting input), logic (for game logic, move goes here eating fruit etc) and render (for printing to screen) in these you include all your other functions
8th Jun 2017, 2:03 PM
jay
jay - avatar