Public Group

# New to this stuff

This topic is 4990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi fellas! Through my current education i have learned some basic c++ and was going to try making a simpel game like snake, i have a few Q's tho. 1: How do i make the gametick? I need som kinda tick to get the snake to move on if the user havent pressed a key right? 2: When generating the "food" that the snake has to get in order to grow larger, i need to make some random stuff, but how? How can i tell it to avoid the fields that the snake is occupying? Thanks-

##### Share on other sites
Hi, and welcome to GDNet. [smile]

Quote:
 1: How do i make the gametick? I need som kinda tick to get the snake to move on if the user havent pressed a key right?

Yep, right you are. Have you heard of the concept of a game loop? Basically you have all your game logic within a loop, so everything get's constantly updated. It's best practice to also use a technique called "time based modelling" or "time based physics" (or a number of different other names), where you make sure your game doesn't run too fast or too slow. There's a good little introductory article on the topic here that you can check out.

Quote:
 2: When generating the "food" that the snake has to get in order to grow larger, i need to make some random stuff, but how? How can i tell it to avoid the fields that the snake is occupying?

Generate a random value, and then check if it matches the value of any segments of the snake. If it doesn't match, use the value and continue with your game logic as normal. If it does match (and therefore would be on the snake), generate a new coordinate and repeat. It's a simplistic way of doing it, but it works well without being overly complex. I'll leave it to you to try that out, but feel free to ask for more help if you need it.

Hope that's given you some help with your questions. [smile]

##### Share on other sites
For #1 its pretty easy, you main loop should look like this:

bool game_is_up = true;
while(game_is_up)
{
if(kbhit())
{
//process keys here
}
//Update snake, move snake
//draw snake
}

##### Share on other sites
Quote:

That'll work just fine, but I still suggest implementing timing as well so that it runs the same on different systems, etc.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 10
• 9
• 36
• 16
• ### Forum Statistics

• Total Topics
634125
• Total Posts
3015681
×