5 Replies - 2422 Views - Last Post: 05 December 2011 - 12:24 PM Rate Topic: -----

#1 Marcus007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-December 11

Snake game..

Posted 05 December 2011 - 09:26 AM

I've posted a topic here yesterday or the other day, not sure. Anyway, my topic was about creating a snake game and where to start. I'v already started and here's what I've done. If anyone could lend me hand and help me how to make the snake body and make it bend like the one in a snake game. Really need your help here guys.
 
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4

int nScore;
int nGameDelay;
 
void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void snake(int x, int y)
{
  HANDLE hConsole;
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  gotoxy(x,y);
  printf("*******");
}

void moveSnake()
{
  
} 

void background()
{
  int i;
  gotoxy(0, 0);
  printf("_______________________________________________________________________________");
  for(i=1; i<=20; i++)
  {
    gotoxy(0,i);
    printf("|");
  }
  for(i=1; i<=20; i++)
  {
    gotoxy(78,i);
    printf("|");
  }
  gotoxy(0, 20);
  printf("_______________________________________________________________________________");
}

void display(int x, int y)
{  
  background();
  snake(x,y);
}


main()
{
  int pick;
  do
  {
    printf("\n\n\n\n\n\n\n");
    printf("\t\t _______  __    _  _______  ___   _  _______   \n");
    printf("\t\t|       ||  |  | ||   _   ||   | | ||       |  \n");
    printf("\t\t|  _____||   |_| ||  |_|  ||   |_| ||    ___|  \n");
    printf("\t\t| |_____ |       ||       ||      _||   |___   \n");
    printf("\t\t|_____  ||  _    ||       ||     |_ |    ___|  \n");
    printf("\t\t _____| || | |   ||   _   ||    _  ||   |___   \n");
    printf("\t\t|_______||_|  |__||__| |__||___| |_||_______|  \n");
    printf("\n\t\t              Press 1 to Play                \n");
    printf("\t\t              Press 2 to Quit                \n");
    printf("\t\t              Pick: "); scanf("%d", &pick);
    if (pick != 1 && pick != 2)
      printf("Invalid! Choose again!"); 
    if (pick == 1)
    {
      system("CLS");
      background();
      system("CLS");
    } 
  } while (pick != 2);
  fflush(stdin);
  getchar();
}




Is This A Good Question/Topic? 0
  • +

Replies To: Snake game..

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,337
  • Joined: 31-December 10

Re: Snake game..

Posted 05 December 2011 - 10:18 AM

It looks to me like you have just the basics for the game, have you tried anything yourself or are you just hoping someone will give you "teh codez"??? My suggestion would be to use a dynamic array for the snake body. You could even make a struct that contains (x,y) coordinates for each part of the snake body. Then make an array of that struct, Figure out how to populate the array with coordinates, and then draw the snake using those coordinates. The tricky part would be the moving of the snake because you need to figure out what direction the snake is going to move, whether it's a valid move or not, and then refresh the screen and draw the snake body in the new position. This will have to be done with some sort of a timer, which shouldn't be to difficult to figure out, just have a look on google and at the <time.h> header file.

Try to at least get it to draw the snake in it's starting position. Work on it in small chunks, instead of trying to do the whole thing in one go at it. The problem gets easier if you break it up into smaller problems and solve those. And of course, if you have any questions or get any errors/warnings, then post again. But really try to do some of it on your own, nobody here gets paid so no one is going to do it for you. Also, if you're genuine about really learning programming, then attempting to do it yourself is a good start.
Was This Post Helpful? 1
  • +
  • -

#3 Marcus007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-December 11

Re: Snake game..

Posted 05 December 2011 - 10:43 AM

At last, someone replied. Thank you for your time on reading and replying to my post.

To be honest, I, myself, want to do it on my own because this is a project of mine and is 25% of my final grade. There will be a demo for this and each function will be explain(algorithms, how i came up with the code, etc.) So by saying this, I'm not relying on someone on giving me "teh codez" :D, but relying on you, experts, on giving me some hints on where to start and how to apply it. We're currently studying on C but what we're studying are limited to: loops, if-else, switch, recursion, pointers, nothing else more. So arrays would be "new" to me as we haven't discussed it yet BUT I'm interested to learn about arrays and structures as I make this program. I have read about the arrays and structures and it's sounds really easy to me but I think I would have a hard time applying it to my codes.

If you would look at my problem, I'm trying to draw the borders out when I try to do it, it only shows the half part of it and the other half shows the MENU again. I have to scroll up before I see the "whole" border of the game and another problem is, I can't seem to see the snake as the background calls the function "snake" which prints the body of the snake.

I want to try everything but I don't know WHAT to try.

Really appreciated you pushing me to the limits and I thank you for that. But hints and advice won't be bad either. :D thank you.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,337
  • Joined: 31-December 10

Re: Snake game..

Posted 05 December 2011 - 11:15 AM

That seems strange that you've learned about pointers but not arrays. The concept of arrays and pointers is similar, but not entirely the same. For example, when you pass an array to a function (as an argument), it is seen as just a pointer to the first element of an array. Basically arrays are used to hold a collection of variables of the same type. So you can declare an array of 10 integers like this:
int array[10];

The array is capable of holding 10 integers, because there is no initialization of the array, it contains garbage values right now. You can access individual elements of the array using the subscript operator ([]). You said you know loops, so you can use a loop to go through the entire array and print each element. Note: to access the first element, C/C++ doesn't start at 1 (one) but 0 (zero). So to fill the above array with integers would look like this:
const int SIZE = 10;
int array[SIZE];
for(int i = 0; i < SIZE; ++i)
    array[i] = i + 1;
for(int i = 0; i < SIZE; ++i)
    printf("array[%d] = %d\n", i, array[i]);


You could probably write snake without using arrays, but I imagine it could help using them. Post any new code you have so we can help.
Was This Post Helpful? 0
  • +
  • -

#5 Marcus007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-December 11

Re: Snake game..

Posted 05 December 2011 - 11:55 AM

Yeap, that's exactly what I read about arrays.
int array[10] has 10 integers but with garbage values and(i think) by putting a value to each element, you would declare(? is it the right word) each element i.e. array[0] = 5, array[1] = 2, array[2] = 4 then when adding up these arrays, array[0] + array[1] + array[2] = 11. Correct me if I'm wrong here.

By means that Arrays could help, could you explain how? I'm thinking that for example, this array[0] would be the "head" of the snake, I would just use this array for moving the whole body? Is this what you mean that arrays could help?

Before I post new codes, which I haven't started typing yet, could you tell me how I could move the snake by ITSELF? I mean, if I manage to draw the border and the snake itself, how could I make it move? This is without the user input yet(up, down, left, right)
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,337
  • Joined: 31-December 10

Re: Snake game..

Posted 05 December 2011 - 12:24 PM

That's not the way to initialize arrays, instead it would look something like this (note: this is called an initializer list):
int numArr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
char charArr[10] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };


Later on in the program, if you wanted to access individual elements, then you use the subscript operator like numArr[3] to access the 4th element, remember that the counting starts at zero and not one.

Now onto your question about moving the snake. You just make what is called an infinite-loop (i.e. while(true) or for(;;) ). Then inside the loop is where all the code for the game as it's running goes. Sometimes you'll also see this loop referred to as the main game loop or other variations like that. Inside the loop you would draw the snake, change the snake's position according to its direction, clear the screen, and start the loop over. The next time the snake is drawn, it will be in it's new position. Since the loop will go really fast, you will probably need to add a slight pause in your loop, so the snake won't be moving that fast. There is usually some type of a pause() function but it depends on what Operating System you run, what it's name will be and where it can be found.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1