Racing Game Help (Arrays)

Hi. I'm trying to create a racing game (this is an assignment for school).

Here is the assignment:
http://www.umich.edu/~engr101/Bielajew/a6.pdf

The thing I'm stuck on right now is, when the car crashes, how to get it to display the 'car' embedded into the wall right at the point where it crashes. So the 'car' can't just show up somewhere in the middle of the barriers, if it crashes it has to be shown embedded in the place where it would have hit.

Here is my code so far:
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
#include <iostream>

using namespace std;

const int Nrows = 52;
const int Ncols = 72;

void printRace(const char a[][Ncols]) 
{  for (int i = 0; i < Nrows; i = i + 1)
   {  for (int j = 0; j < Ncols; j = j + 1)
      {  cout << a[i][j];
      }
      cout << endl;
   }
   return;
}


void initRace(char b[Nrows][Ncols]) 
{  for (int i = 0; i < Nrows; i = i + 1)
   {  for (int j = 0; j < Ncols; j = j + 1)
      {  if (i == 0 || i == 51) b[i][j] = 'X';	// top and bottom
			else if (j == 0 || j == 71) b[i][j] = 'X';   // sides
			else if (i > 0 && j >= 10 && j <= 29 && i < 36) b[i][j] = 'X';
			else if (i > 16 && j >= 40 && j <= 64) b[i][j] = 'X';
			else b[i][j] = ' ';
			if (i == 51 && j > 64 && j < 71) b[i][j] = 'F';
		}
		
   }
   return;
}


int main(void)
{  char track[Nrows][Ncols];
	int x = 1, y = 1, xVel = 0, yVel = 0, xAccel = 0, yAccel = 0;
   initRace(track);
	track[x][y] = 'O';
   printRace(track);

	while(1)
	{	cout << "Vertical and horizontal acceleration (-1,0,1): ";
		cin >> xAccel >> yAccel;
		if (xAccel > 1 || xAccel < -1 || yAccel > 1 || yAccel < -1)
		{	track[x][y] = 'X';
			printRace(track);	
			cout << "You broke your car!" << endl;
			return 0;
		}
		xVel = xVel + xAccel;
		yVel = yVel + yAccel;
		x = x + xVel;
		y = y + yVel;
		if (track[x][y] == 'X')
		{	track[x][y] = 'O';
			printRace(track);
			cout << "You crashed!" << endl;
			return 0;
		}

		track[x][y] = 'O';
		printRace(track);
	}


   return 0;
}


At this point it I have it so the car just shows up where it would be if it were to keep going inside the wall, not stop on the edge,

Any help?

Thanks.
Last edited on
You could just overwrite the map array at the (x,y) the car crashed at by changing it from a 'X' to an 'O'. Then, just check for both values in your collision detection (or go the opposite way and check for an empty space at the next position, rather than a wall).
I'm not sure what you're suggesting. I kinda get that I have to "go backwards" from the point the car would be at inside the wall, but how do I know which way backwards is?
Topic archived. No new replies allowed.