Problem with the Homework

Hello everyone, I'm having some trouble with my homework. I need to write a snake game, thus my snake needs to move continuesly. The problem is that when i press a button it doesn't turn and move right away, instead it stops and when i press the same button again then it starts moving. My code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <string>
#include "Robots_Modified.h"
#include "MiniFW_Modified.h"
#include "strutils.h"
#include "randgen.h"
using namespace std;

int main ()
{
	int a = 0;
	string name;
	EmptyAllTheCells(0, 0, 16, 16);

	GetInput("Please enter the name of the snake" , name);


	Robot r1(0, 0);
	r1.SetColor(pink);
	r1.SetName(name);
	Robot r2(17, 2);
	r2.SetColor(pink);
	r2.SetVisibility (false);
	Robot r3(17, 3);
	r3.SetColor(pink);
	
	Robot r4(17, 4);
	r4.SetColor(pink);
	Robot r5(17, 5);
	r5.SetColor(pink);
	Robot r6(17, 6);
	r6.SetColor(pink);
	
	RandGen rnd;

	for (int i=0; i<5; i++)
	{
		PutThings(rnd.RandInt(0, 15), rnd.RandInt(0, 15), rnd.RandInt(0, 5));
	}

	

	while ( a<1)

	{
		

	if (IsPressed(keyRightArrow))
		{				
			do
			{r1.PickThings();
				r1.Turn(east);
			r1.Move();
			
			
			
			}while( !(IsPressed(keyUpArrow) || IsPressed(keyDownArrow)));
		}
	if (IsPressed(keyLeftArrow))
		{
			do
			{r1.Turn(west);
			r1.Move();
			r1.PickThings();
			}while( !(IsPressed(keyUpArrow) || IsPressed(keyDownArrow)));
		}
	if (IsPressed(keyUpArrow))
		{
			do
			{r1.Turn(north);
			r1.Move();
			r1.PickThings();
			}while( !(IsPressed(keyLeftArrow) || IsPressed(keyRightArrow)));
		}
	if (IsPressed(keyDownArrow))
		{
			do
			{r1.Turn(south);
			r1.Move();
			r1.PickThings();
			}while( !(IsPressed(keyLeftArrow) || IsPressed(keyRightArrow)));
		}
	}


}



It seems that the problem occurs because when i press a button it exits the do while loop but does not enter other 'if's so i need to press again.

I couldn't get around this problem so i'll appreciate any help.
BTW in this hw it is not allowed to use exit goto break continue or any global variables.

And, sorry for my english, it's not my native language :)
Try storing whether you entered any if's in a bool variable. Like this:

1
2
3
4
5
6
7
8
9
10
11
12

   bool ok = false;
	if (IsPressed(keyLeftArrow )  && !ok)
		{
			do
			{r1.Turn(west);
			r1.Move();
			r1.PickThings();
			}while( !(IsPressed(keyUpArrow) || IsPressed(keyDownArrow)));

                        ok = true;
		}


There probably is a more elegant solution, but this should get the job done.
Last edited on
Another thing you could do is use a container to store the user input...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//...

enum UserInput {NO_INPUT, UP, DOWN, LEFT, RIGHT};

//...

std::list<int> input_queue;
int last_input;

//...

if (IsPressed(keyRightArrow) && last_input!=RIGHT)
    input_queue.push_back(RIGHT);

if (IsPressed(keyLeftArrow) && last_input!=LEFT)
    input_queue.push_back(LEFT);

if (IsPressed(keyUpArrow) && last_input!=UP)
    input_queue.push_back(UP);

if (IsPressed(keyDownArrow) && last_input!=DOWN)
    input_queue.push_back(DOWN);

//... 

And decide what to do with it at a later point...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//...

int cur_input=NO_INPUT;

if (!input_queue.empty())
{
    cur_input=input_queue.front();
    input_queue.pop_front();
}

switch (cur_input)
{
    case NO_INPUT: //...
    case UP: //...
    case DOWN: //...
    case LEFT: //...
    case RIGHT: //...
}

//... 

Also, mind that a snake should always Move() and PickThings(),
so, it's probably better to put these function calls somewhere else.
Last edited on
Topic archived. No new replies allowed.