Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE LSM9DS1_Library SDFileSystem mbed-rtos mbed wave_player
Diff: Invader.cpp
- Revision:
- 3:27889fffc2f7
- Parent:
- 0:6a49493943be
diff -r 623f29bad35c -r 27889fffc2f7 Invader.cpp --- a/Invader.cpp Thu Mar 10 22:51:12 2016 +0000 +++ b/Invader.cpp Sat Mar 12 19:59:00 2016 +0000 @@ -1,30 +1,41 @@ #include "Invader.h" +#include <stdlib.h> /* srand, rand */ +#include <time.h> /* time */ -#define WIDTH 6 -#define HEIGHT 6 +#define WIDTH 10 +#define HEIGHT 10 -Invader::Invader(uLCD_4DGL* startScreen, int startX, int startY, int spdX, int spdY) +Invader::Invader(uLCD_4DGL* startScreen, int w, int h) { + /* initialize random seed: */ + srand (time(NULL)); + + windowWidth = w; + windowHeight = h; screen = startScreen; - x = startX; - y = startY; - lastX = x; - lastY = y; - spdX = spdX; - spdY = spdY; + resetLocationAndSpeed(); } void Invader::update() { + printf("x: %d, y: %d, spdY: %d\n\r", x,y, spdY); + lastX = x; + lastY = y; + x += spdX; y += spdY; + + if (x <= -10 || y <= -10 || x+WIDTH >= windowWidth + 10 || y+HEIGHT >= windowHeight + 10) { + // Then it is beyond the starting location for the box and we should reset it. + resetLocationAndSpeed(); + } } void Invader::draw() { screen_mutex.lock(); - screen->filled_circle(lastX, lastY, WIDTH/2, BLACK); - screen->filled_circle(x, y, HEIGHT/2, RED); + screen->filled_rectangle(lastX-WIDTH/2-1, lastY-HEIGHT/2-1, lastX+3*WIDTH/2, lastY+3*HEIGHT/2, BLACK); + screen->filled_rectangle(x, y, x+WIDTH, y+HEIGHT, RED); screen_mutex.unlock(); } @@ -32,9 +43,42 @@ { if (((otherX > x && otherX <= x + WIDTH) || (otherX + otherWidth > x && otherX + otherWidth <= x + WIDTH)) && ((otherY > y && otherY <= y + HEIGHT) || (otherY + otherHeight > y && otherY + otherHeight <= y + HEIGHT))) { + printf("Intersects\n"); return true; } else { + printf("Not Intersecting\n\n"); return false; } } + +void Invader::resetLocationAndSpeed() +{ + int side = rand() % 4; + switch(side) { + case 0: // Left + x = -9; + y = (int)(rand() % (windowHeight - HEIGHT)); + spdX = (int)(rand() % 3 + 1); + spdY = (int)(((int)rand())==0 ? -1 : 1)*(rand() % 3); + break; + case 1: // Right + x = windowWidth - 1; + y = (int)(rand() % (windowHeight - HEIGHT)); + spdX = (int) (-1*(rand() % 3 + 1)); + spdY = (int)(((int)rand())==0 ? -1 : 1)*(rand() % 3); + break; + case 2: // Top + x = (int)(rand() % (windowWidth - WIDTH)); + y = -9; + spdX = (int)(((int)rand())==0 ? -1 : 1)*(rand() % 3); + spdY = (int)(rand() % 3 + 1); + break; + case 3: // Bottom + x = (int)(rand() % (windowWidth - WIDTH)); + y = windowWidth-1; + spdX = (int)(((int)rand())==0 ? -1 : 1)*(rand() % 3); + spdY = (int)(-1*(rand() % 3 + 1)); + break; + } +}