snake game
May 2, 2015 at 2:11am UTC
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' ;
}
}
}
}
May 2, 2015 at 3:12pm UTC
Maybe you can use rand() to get a random row and column of your Map array whenever you want to randomly insert a '*'
May 4, 2015 at 3:21am UTC
thats what I was thinking I tried it but its not compiling
May 4, 2015 at 3:35am UTC
int pellet=rand()% Map[11][22] ;
I tried to use this but its not working
May 4, 2015 at 7:00am UTC
May 5, 2015 at 4:09am UTC
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
May 5, 2015 at 6:30pm UTC
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 May 5, 2015 at 6:30pm UTC
Topic archived. No new replies allowed.