Sign in to follow this  

New to this stuff

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by chad_420
...


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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this