snake game

I'm trying to figure out where to go from here, its not homework just personal project im working on. what I want to know is how to randomly generate the
'*' which will be the "food" for my snake






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
#include<iostream>
#include<Windows.h>
using namespace std;

char Map[11][22] =
{
	"---------------------",
	"|S              *   |",
	"|                   |",
	"|                   |",
	"|                   |",
	"|                   |",
	"|                   |",
	"|                   |",
	"|                   |",
	"|                   |",
	"---------------------"
};
int x = 1, y = 1;
bool GameRunning = true;
//void srand;
int main()
{
	while (GameRunning == true)
	{
		system("cls");
		for (int display = 0; display < 11; display++)
		{
			cout << Map[display] << endl;
		}
		system("pause>nul");

		if (GetAsyncKeyState(VK_DOWN))
		{
			int y2 = y + 1;
			if (Map[y2][x] == ' ')
			{
				Map[y][x] = ' ';
				y++;
				Map[y][x] = 'S';
			}
		}
		if (GetAsyncKeyState(VK_UP))
		{
			int y2 = y - 1;
			if (Map[y2][x] == ' ')
			{
				Map[y][x] = ' ';
				y--;
				Map[y][x] = 'S';
			}
		}
		if (GetAsyncKeyState(VK_RIGHT))
		{
			int x2 = x+1;
			if (Map[y][x2] == ' ')
			{
				Map[y][x] = ' ';
				x++;
				Map[y][x] = 'S';
			}
		}
		if (GetAsyncKeyState(VK_LEFT))
		{
			int x2 = x - 1;
			if (Map[y][x2] == ' ')
			{
				Map[y][x] = ' ';
				x--;
				Map[y][x] = 'S';
			}
	     }

	}
} 
Maybe you can use rand() to get a random row and column of your Map array whenever you want to randomly insert a '*'
thats what I was thinking I tried it but its not compiling
int pellet=rand()% Map[11][22] ; I tried to use this but its not working
You can see an example of how rand is used here

http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand

You may need to generate two random numbers one for the row and one for the column and then do something like Map[randRow][randCol] = '*'.
I dont think im understanding how to use it in my program...but its not working...Im thinking I may have to my program up in a class and use what I have as functions
this is my newest error
1
2
3
4
5
6
7
8
9
srand(time(NULL));
	int pellet = rand();//possible error

	while (GameRunning == true)
	{
		for (int pellet = 0; pellet % Map[11][22]; pellet++)//error line 
		{
			cout << '*';
		}
Last edited on
Topic archived. No new replies allowed.