New to this stuff

Started by
2 comments, last by jbadams 18 years, 10 months ago
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-
Advertisement
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]

- Jason Astle-Adams

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
}
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.

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement