13 Replies - 2691 Views - Last Post: 07 May 2013 - 09:42 AM Rate Topic: -----

#1 cllyldrm  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 03-May 13

C++ OPENGL SNAKE GAME

Posted 03 May 2013 - 01:13 PM

Hi. I want to ask question about snake game in opengl. I want to add two box in the snake when program is open but one box seeing. I have lots of class.h and cpp. This is my snake.cpp

#include "Snake.h"

Snake :: Snake()
{
	boxnumber = 2;// in the picture i wrote boxnumber = 1;
	for(int i = 0; i < boxnumber; i++)
	{
		boxes[i] = new box;
	}
	
}

void Snake :: readkeyboard(unsigned char key)
{
	for(int i = 0; i < boxnumber; i++)
	{
		switch (key)
		{
		case 'd':
		case 'D':
			boxes[i]->yon = YON_left;
			break;
		case 'g':
		case 'G':
			boxes[i]->yon = YON_right;
			break;
		case 'r':
		case 'R':
			boxes[i]->yon = YON_up;
			break;
		case 'f':
		case 'F':
			boxes[i]->yon = YON_down;
			break;
		}
	}
}

void Snake :: goahead()
{
	for(int i = 0; i < boxnumber; i++)
	{
		boxes[i]->goahead();
	}
}

void Snake :: draw()
{
	for(int i = 0; i < boxnumber; i++)
	{
		boxes[i]->draw();
	}
}

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: C++ OPENGL SNAKE GAME

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,463
  • Joined: 12-June 08

Re: C++ OPENGL SNAKE GAME

Posted 03 May 2013 - 01:14 PM

what is the question, or is this a 'share your project'?
Was This Post Helpful? 0
  • +
  • -

#3 cllyldrm  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 03-May 13

Re: C++ OPENGL SNAKE GAME

Posted 03 May 2013 - 01:18 PM

View Postmodi123_1, on 03 May 2013 - 01:14 PM, said:

what is the question, or is this a 'share your project'?

Nope. When the program is open. There was one box showing and one box moving. but i want to show two box and moving. I cant do this. I have project.rar. Can i send u to your e mail ? Because of Lots of classes..
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,463
  • Joined: 12-June 08

Re: C++ OPENGL SNAKE GAME

Posted 03 May 2013 - 01:24 PM

Quote

Can i send u to your e mail ? Because of Lots of classes..

Mmm.. no.

I would prefer the discourse to stay on the forum for everyone to help, or learn from.

Though I cannot imagine a 'snake game' having "Lots of classes.. ".
Was This Post Helpful? 0
  • +
  • -

#5 cllyldrm  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 03-May 13

Re: C++ OPENGL SNAKE GAME

Posted 03 May 2013 - 01:27 PM

View Postmodi123_1, on 03 May 2013 - 01:24 PM, said:

Quote

Can i send u to your e mail ? Because of Lots of classes..

Mmm.. no.

I would prefer the discourse to stay on the forum for everyone to help, or learn from.

Though I cannot imagine a 'snake game' having "Lots of classes.. ".

No problem. I have shape, snake, color, box, dot, grill classes. Because of i say that.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: C++ OPENGL SNAKE GAME

Posted 03 May 2013 - 10:24 PM

How are you positioning your boxes? It's not obvious in your code above how the locations of the boxes are established.
Was This Post Helpful? 0
  • +
  • -

#7 cllyldrm  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 03-May 13

Re: C++ OPENGL SNAKE GAME

Posted 04 May 2013 - 03:43 AM

View PostSkydiver, on 03 May 2013 - 10:24 PM, said:

How are you positioning your boxes? It's not obvious in your code above how the locations of the boxes are established.

#include "Kutu.h"
#include <GL/freeglut.h>

Kutu :: Kutu()
{
	noktaSayisi = 6; //dotnumber
	boyut 		= 20; //size
	cizimSekli 	= GL_TRIANGLES;
	pNoktalar 	= new Nokta[noktaSayisi];
	yon 		= YON_SAG; // first start right
	pozisyonAta(200, 200); //first position
}

void Kutu :: ilerle() //goahead
{
	switch(yon)
	{
	case YON_SOL:
		for(int i = 0; i < noktaSayisi; i++)
			pNoktalar[i].x -= boyut;
	break;
	case YON_SAG:
		for(int i = 0; i < noktaSayisi; i++)
			pNoktalar[i].x += boyut;
	break;
	case YON_YUKARI:
		for(int i = 0; i < noktaSayisi; i++)
			pNoktalar[i].y += boyut;
	break;
	case YON_ASAGI:
		for(int i = 0; i < noktaSayisi; i++)
			pNoktalar[i].y -= boyut;
	break;
	}
}

Nokta Kutu :: baslangicNoktasiniGetir()
{
	return pNoktalar[1];
}

void Kutu :: pozisyonAta(float x, float y) // position 
{
	pNoktalar[0] = Nokta(x, y + boyut);
	pNoktalar[1] = Nokta(x, y);
	pNoktalar[2] = Nokta(x + boyut, y);
	pNoktalar[3] = Nokta(x + boyut, y + boyut);
	pNoktalar[4] = Nokta(x, y + boyut);
	pNoktalar[5] = Nokta(x + boyut, y);
}


Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1306
  • View blog
  • Posts: 4,495
  • Joined: 19-February 09

Re: C++ OPENGL SNAKE GAME

Posted 04 May 2013 - 04:06 PM

You could do something like this :

Snake :: Snake()
{
  boxnumber = 2;// in the picture i wrote boxnumber = 1;
  
  boxes[0] = new box;
  boxes[0]->yon = YON_left;
  boxes[1] = new box;
}




Your problem is deeper though, I think, all of the snake doesn't move the same way as the head. Rather the simulation is usually to move each part of the snake to the position of the part in front of it.

not

  oo      oo
  o  <-   o
  0       0

but

   o      oo
   o  <-  o
  0o      0


Was This Post Helpful? 0
  • +
  • -

#9 cllyldrm  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 03-May 13

Re: C++ OPENGL SNAKE GAME

Posted 04 May 2013 - 04:54 PM

View Post#define, on 04 May 2013 - 04:06 PM, said:



I added this. But the second box added on the the first box anf didnt see. I want to show 2 box like a snake body. They are going same way but two boxes are seeing one box.

#include "Yilan.h"

Yilan :: Yilan()
{
	kutuSayisi = 2;
	Kutular[0] = new Kutu;
	Kutular[0]->yon = YON_SAG;
	Kutular[1] = new Kutu;
}

void Yilan :: klavyeOku(unsigned char key)
{
	switch (key)
	{
	case 'd':
	case 'D':
		for(int i = 0; i < kutuSayisi; i++)
		{
			Kutular[i]->yon = YON_SOL;
			break;
		}
	case 'g':
	case 'G':
		for(int i = 0; i < kutuSayisi; i++)
		{
			Kutular[i]->yon = YON_SAG;
			break;
		}
	case 'r':
	case 'R':
		for(int i = 0; i < kutuSayisi; i++)
		{
			Kutular[i]->yon = YON_YUKARI;
			break;
		}
	case 'f':
	case 'F':
		for(int i = 0; i < kutuSayisi; i++)
		{
			Kutular[i]->yon = YON_ASAGI;
			break;
		}
	}
}

void Yilan :: ilerle()
{
	for(int i = 0; i < kutuSayisi; i++)
	{
		Kutular[i]->ilerle();
	}

Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1306
  • View blog
  • Posts: 4,495
  • Joined: 19-February 09

Re: C++ OPENGL SNAKE GAME

Posted 04 May 2013 - 05:35 PM

Hi, since you have yon = YON_SAG in Kutu, you probably don't need it here, but it looks as if you need ilerle to tell the box to move.

Yilan :: Yilan()
{
	kutuSayisi = 2;
	Kutular[0] = new Kutu;
	// Kutular[0]->yon = YON_SAG;
	Kutular[0]->ilerle();
	Kutular[1] = new Kutu;
}


Was This Post Helpful? 1
  • +
  • -

#11 cllyldrm  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 03-May 13

Re: C++ OPENGL SNAKE GAME

Posted 05 May 2013 - 02:34 AM

View Post#define, on 04 May 2013 - 05:35 PM, said:

Hi, since you have yon = YON_SAG in Kutu, you probably don't need it here, but it looks as if you need ilerle to tell the box to move.

Yilan :: Yilan()
{
	kutuSayisi = 2;
	Kutular[0] = new Kutu;
	// Kutular[0]->yon = YON_SAG;
	Kutular[0]->ilerle();
	Kutular[1] = new Kutu;
}


Thats work. Thank you so much. But the snake isnt go like a snake. :))
I must fix it.
Was This Post Helpful? 0
  • +
  • -

#12 cllyldrm  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 03-May 13

Re: C++ OPENGL SNAKE GAME

Posted 06 May 2013 - 12:02 PM

How can i way to snake like a snake ? (two boxes snake)

It is going like a crab now.
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1306
  • View blog
  • Posts: 4,495
  • Joined: 19-February 09

Re: C++ OPENGL SNAKE GAME

Posted 06 May 2013 - 02:11 PM

If you have a snake with two sections, a head and a body

[body][Head]


when it moves to the right, the head moves to the right and the body takes the position previously occupied by the head.

      [body][Head]



when it moves down, the head moves down and the body takes the position previously occupied by the head.

            [body]
            [Head]



If you have a snake with three sections, a head and two body parts

[body2][body1][Head0]


when it moves to the right, the head moves to the right and the body parts take the position of the part in front of it.

       [body2][body1][Head0]

Was This Post Helpful? 1
  • +
  • -

#14 cllyldrm  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 03-May 13

Re: C++ OPENGL SNAKE GAME

Posted 07 May 2013 - 09:42 AM

Thank you very much sir. I im trying.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1