Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are making a game named autoracer... There are at least 3 cars coming from th

ID: 3573388 • Letter: W

Question

We are making a game named autoracer... There are at least 3 cars coming from the top of the screen and there is another car on the bottom of the screen. The car on the bottom part should not meet the other cars coming from the top. If it crashes the game over. This car can be moved by joystick. (right side &left side).

It is programmed on stm32f4 board....using keil......

I attached my c code ... I have problem on collision part....eventhough the car is crashed the game still goes on.... and if I move the car with joystick the game over......

Also i don't know how to add more car coming from the top.. Please... help me....  

#include <stdio.h> //printf(), scanf()
#include <stdlib.h> //srand(), rand(), malloc()
#include <string.h> //
#include <ctype.h> //
#include <time.h> //time(), clock()
#include <math.h> //

#include <stm32f4xx.h>
#include "GLCD.h"
#include "JOY.h"
#include "I2C.h"

#define ESC 27 //
#define ENTER 13
#define SPACE 32 //,
#define LEFT 75 //224 75
#define RIGHT 77 //224 77
#define UP 72 //224 72
#define DOWN 80 //224 80


#define winX 3 //
#define winY 0 //
#define winWidth 35 //
#define winHeight 40//
#define FREE_DROP 0 //
#define MOVE_DOWN 1 //
#define MOVE_LEFT 2 //
#define MOVE_RIGHT 3 //
#define MOVE_DROP 4 //
#define ROTATION 5 //

#define WAKEUP ((GPIOA -> IDR & (1 << 0))==1)
#define TAMPER ((GPIOC -> IDR & (1<<13))==0)
#define USER ((GPIOG -> IDR & (1<<15))==0)
#define TH 100
#define J_INITIAL -1
#define J_DOWN 0
#define J_UP 1

int car_x, car_y,car_x2, car_y2; //
int mycar_x, mycar_y; //
int win[winHeight][winWidth]; //
int car_action; // freedown
int mycar_action; // freedown
int free_drop_delay = 2; //
int free_drop_count; //

void Start();
void NewCar(void);
void NewCar2(void);
void MyCar(void);
void Display(void);
void checkKey(void);
void Update(void);
int IsCollision(void);
int getch(void);
void FixCar(void);
void FixMyCar(void);

void BUT_Init (void) {

RCC->AHB1ENR |= (1UL << 0) | (1UL << 2) | (1UL << 6) ;

GPIOA->MODER &= ~(3UL << 2* 0);
GPIOC->MODER &= ~(3UL << 2*13);
GPIOG->MODER &= ~(3UL << 2*15);
}

char car [4][4]={ //
3,3,3,3,
3,3,3,3,
3,3,3,3,
3,3,3,3,
};

char mycar [4][4]={//
  1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
};

int GameOver = 0;
int GamePoint = 0;

int main()
{
int i;
char gameover[128];
char finalpoint [128];

BUT_Init();
JOY_Init();
GLCD_Init();
GLCD_Clear(Black);
GLCD_SetTextColor(White);
GLCD_SetBackColor(Black);

Start(); //

while(!GameOver)
{
Display(); //
//getch();
checkKey(); //
Update(); //
//Sleep(40); //40ms
for(i=0 ; i<400000 ; i++);
}

GLCD_Clear(Black);
  sprintf(gameover, "Game Over!");
GLCD_DisplayString(15, 15, 0, gameover);
  sprintf(finalpoint, "Your final point is %d", GamePoint);
GLCD_DisplayString(18, 10, 0, finalpoint);

return 0;
}

void Start() //
{
int x, y;
NewCar();
NewCar2(); //
MyCar();
free_drop_count = free_drop_delay; //20 1
//
for (x=0; x<winWidth; x++)
{
  for(y=0; y<winHeight; y++)
  {
   if (x == 0 || x == winWidth-1 ||
    y == 0 || y == winHeight-1)
   {
    win[y][x]=2; // win[y][x]=2;
   } else
   {
    win[y][x]=0; // 1
   }
  }
}
}

void NewCar() //
{
   car_x = rand()%40; // x
   car_y = 1; // y
   car_action = FREE_DROP;
}

void NewCar2() //
{
   car_x2 = rand()%40; // x
   car_y2 = 1; // y
   car_action = FREE_DROP;
}

void MyCar() //
{
mycar_x = winWidth/2;
mycar_y = winHeight-6;
}

void Display() //
{
int x, y;
char point[128];
//
for (y=0; y<winHeight; y++)
{
//gotoXY(winX, winY+y);
for (x=0; x<winWidth; x++)
{
  if(win[y][x]==1||win[y][x]==3) {
   //printf("");
   GLCD_DisplayChar(winY+y, winX+x, 0, '@');
  }  
  else if(win[y][x]==2) {
   //printf("");
   GLCD_DisplayChar(winY+y, winX+x, 0, '#');
  }
  else {
    //printf("·");
   GLCD_DisplayChar(winY+y, winX+x, 0, ' .');
  }
}
//printf(" ");
GLCD_DisplayChar(winY+y, winX+x, 0, ' ');
}

//
for (y=0; y<4; y++)
{
for (x=0; x<4; x++)
{
  if (car[y][x] ==3 )
  {
    //gotoXY(winX + (brick_x + x)*2, winY + brick_y + y);
    //printf("");
   GLCD_DisplayChar(winY + car_y + y, winX + car_x + x, 0, '@');
  }
}
}

//
for (y=0; y<4; y++)
{
for (x=0; x<4; x++)
{
  if (mycar[y][x] ==1 )
  {
    //gotoXY(winX + (brick_x + x)*2, winY + brick_y + y);
    //printf("");
   GLCD_DisplayChar(mycar_y+y, mycar_x+x, 0, '@');
  }
}
}

//
//gotoXY(30, 25);
//printf("Point = %d",GamePoint);
// sprintf(point, "Point = %d", GamePoint);
//GLCD_DisplayString(30, 27, 0, point);
}

int IsCollision() // .... ...
{
int x, y;
for (y=0; y<4; y++)
{
for (x=0; x<4; x++)
{
  if (mycar[y][x] ==1) //
  {
   if (win[car_y+y][car_x+x] != 0)// !=0
   {
    return 1; //
   }
  }
}
}
return 0; //
}

void checkKey() //
{
int key;
key=getch(); // key
if (key == 224) // 224
{
   key=getch(); //
}
switch (key)
{
   //case ESC:
   // GameOver = 1;
     // break;
   //case ENTER:
     // break;
   //case SPACE:
// car_action = MOVE_DROP;
    // break;
   case LEFT:
   mycar_action = MOVE_LEFT;
      break;
   case RIGHT:
   mycar_action = MOVE_RIGHT;
      break;
// case UP:
// car_action = ROTATION;
    // break;
// case DOWN:
// car_action = MOVE_DOWN;
   //   break;
   default :
   car_action = FREE_DROP;
      break;
}
}

void Update() //
{
int x,y;
if (car_action == FREE_DROP) {
     free_drop_count--;
  if(free_drop_count < 0) //
  {
   free_drop_count = free_drop_delay; //
   car_action = MOVE_DOWN;// Move down ?
  }
}
if(car_action == MOVE_DOWN){ //
      car_y++;
}

switch (mycar_action) //
{
   case MOVE_LEFT:
   if(mycar_x>0) {
        mycar_x--;
    if (IsCollision()) GameOver=1;}
   mycar_action= FREE_DROP;
      break;
   case MOVE_RIGHT:
    mycar_x++;
if (IsCollision()) GameOver=1;
mycar_action= FREE_DROP;
          break;
   default :
    break;
}
}

int getch(void)
{
//static int wakeup=0;
//static int tamper=0;
//static int user=0;

static int joy_up_count=0;
static int joy_down_count=0;
static int joy_left_count=0;
static int joy_right_count=0;


static int joy_up_state=J_INITIAL;
static int joy_down_state=J_INITIAL;
static int joy_left_state=J_INITIAL;
static int joy_right_state=J_INITIAL;

  //if(wakeup==0 && WAKEUP) {
  //wakeup=1;
//}
//else if(wakeup==1 && !WAKEUP) {
// wakeup=0;
  //return ESC;
//}
  //if(tamper==0 && !TAMPER) {
   //tamper=1;
//}
//else if(tamper==1 && TAMPER) {
  //tamper=0;
  //return ENTER;
// }
  //if(user==0 && !USER) {
  //user=1;
// }
//else if(user==1 && USER) {
  //user=0;
  //return SPACE;
//}
/*
if (JOY_GetKeys() == JOY_LEFT) {
    //if ((++joy_left_count) == TH) {
    // joy_left_count= 0;
    //}
    joy_left_state= DOWN;
   }

   else if (JOY_GetKeys() == JOY_RIGHT) {
    if ((++joy_right_count) == TH) {
     joy_right_count= 0;
    }
    joy_right_state= DOWN;
   }
   
   else {
    if (joy_left_state==DOWN) {
     //joy_left_count= 0;

     joy_left_state= UP;
     return LEFT;
    }
    else if (joy_right_state==DOWN) {
     joy_right_count= 0;
     joy_right_state= UP;
     return RIGHT;
    }
   }
return 5;//
*/

if(JOY_GetKeys() == JOY_LEFT){
joy_left_state = DOWN;
}
else if(JOY_GetKeys() != JOY_LEFT)
{
if(joy_left_state == DOWN){
// if(mycar_x>0) mycar_x--;    
    joy_left_state = UP;
  return LEFT;
}
}

if(JOY_GetKeys() == JOY_RIGHT){
joy_right_state = DOWN;
}
else if(JOY_GetKeys() != JOY_RIGHT)
{
if(joy_right_state == DOWN){
// if(mycar_x>0) mycar_x--;    
    joy_right_state = UP;
  return RIGHT;
}
}

return 5;

}

Explanation / Answer

if (win[car_y+y][car_x+x] != 0)// !=0

correct (I think)

if (win[car_y+y][car_x+x] <= 0)// !=0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote