Dependencies: mbed
Diff: Car.cpp
- Revision:
- 0:e1f5bfbb6e53
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Car.cpp Fri Mar 27 10:23:53 2020 +0000 @@ -0,0 +1,101 @@ +#include "Car.h" + +//Global Variables +const int car[7][15]{ + { 0,0,0,0,1,1,1,1,0,0,0,0,0,0,0 }, + { 0,0,0,1,1,0,0,1,1,0,0,0,0,0,0 }, + { 1,1,1,1,0,0,0,0,1,1,0,0,0,0,0 }, + { 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1 }, + { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, + { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, + { 0,0,0,1,1,0,0,0,0,1,1,0,0,0,0 }, + }; + +volatile int x=1; +volatile int y=1; +volatile int xo = 84; +volatile int yo = rand()%24+1; +volatile int xo1 = 84+28; +volatile int yo1 = rand()%24+25; +volatile int xo2 = 84+56; +volatile int yo2 = rand()%24+1; +volatile int xo3 = 84+84; +volatile int yo3 = rand()%24+25; + +// Constructor and destructor +Car::Car() +{ + +} + +Car::~Car() +{ + +} + +void Car::draw(N5110 &lcd) +{ + + lcd.drawSprite(x,y,7,15,(int *)car); +} + +void Car::obstacle(N5110 &lcd) +{ + lcd.drawRect(xo,yo,4,10,FILL_BLACK); + lcd.drawRect(xo1,yo1,4,10,FILL_BLACK); + lcd.drawRect(xo2,yo2,4,10,FILL_BLACK); + lcd.drawRect(xo3,yo3,4,10,FILL_BLACK); +} + +void Car::obupdate() +{ + xo--; + xo1--; + xo2--; + xo3--; + if(xo==-28){ + xo = 84; + } + if(xo1==-28){ + xo1 = 84; + } + if(xo2==-28){ + xo2 = 84; + } + if(xo3==-28){ + xo3 = 84; + } + printf("xo = %i\n",xo); + printf("xo1 = %i\n",xo1); + printf("xo2 = %i\n",xo2); + printf("xo3 = %i\n",xo3); + // Code appears to stop running when any one of the above variables reaches -4 + +} + +void Car::update(Gamepad &pad) +{ + Direction d = pad.get_direction(); + float mag = pad.get_mag(); + + if(d == E && mag > 0.3){ + x = x+2; + wait_ms(10); + } + if(d == W && mag > 0.3){ + x = x-2; + wait_ms(10); + } + if(d == N && mag > 0.3){ + y = y-2; + wait_ms(10); + } + if(d == S && mag > 0.3){ + y = y+2; + wait_ms(10); + } + //printf("x = %i\n",x); + //printf("y = %i\n",y); + +} +