A game for Lab 4 of ECE 4180
Dependencies: 4DGL-uLCD-SE LSM9DS1_Library SDFileSystem mbed-rtos mbed wave_player
Invader.cpp@3:27889fffc2f7, 2016-03-12 (annotated)
- Committer:
- Dogstopper
- Date:
- Sat Mar 12 19:59:00 2016 +0000
- Revision:
- 3:27889fffc2f7
- Parent:
- 0:6a49493943be
Really good 3PM 3/12/16
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dogstopper | 0:6a49493943be | 1 | #include "Invader.h" |
Dogstopper | 3:27889fffc2f7 | 2 | #include <stdlib.h> /* srand, rand */ |
Dogstopper | 3:27889fffc2f7 | 3 | #include <time.h> /* time */ |
Dogstopper | 0:6a49493943be | 4 | |
Dogstopper | 3:27889fffc2f7 | 5 | #define WIDTH 10 |
Dogstopper | 3:27889fffc2f7 | 6 | #define HEIGHT 10 |
Dogstopper | 0:6a49493943be | 7 | |
Dogstopper | 3:27889fffc2f7 | 8 | Invader::Invader(uLCD_4DGL* startScreen, int w, int h) |
Dogstopper | 0:6a49493943be | 9 | { |
Dogstopper | 3:27889fffc2f7 | 10 | /* initialize random seed: */ |
Dogstopper | 3:27889fffc2f7 | 11 | srand (time(NULL)); |
Dogstopper | 3:27889fffc2f7 | 12 | |
Dogstopper | 3:27889fffc2f7 | 13 | windowWidth = w; |
Dogstopper | 3:27889fffc2f7 | 14 | windowHeight = h; |
Dogstopper | 0:6a49493943be | 15 | screen = startScreen; |
Dogstopper | 3:27889fffc2f7 | 16 | resetLocationAndSpeed(); |
Dogstopper | 0:6a49493943be | 17 | } |
Dogstopper | 0:6a49493943be | 18 | |
Dogstopper | 0:6a49493943be | 19 | void Invader::update() |
Dogstopper | 0:6a49493943be | 20 | { |
Dogstopper | 3:27889fffc2f7 | 21 | printf("x: %d, y: %d, spdY: %d\n\r", x,y, spdY); |
Dogstopper | 3:27889fffc2f7 | 22 | lastX = x; |
Dogstopper | 3:27889fffc2f7 | 23 | lastY = y; |
Dogstopper | 3:27889fffc2f7 | 24 | |
Dogstopper | 0:6a49493943be | 25 | x += spdX; |
Dogstopper | 0:6a49493943be | 26 | y += spdY; |
Dogstopper | 3:27889fffc2f7 | 27 | |
Dogstopper | 3:27889fffc2f7 | 28 | if (x <= -10 || y <= -10 || x+WIDTH >= windowWidth + 10 || y+HEIGHT >= windowHeight + 10) { |
Dogstopper | 3:27889fffc2f7 | 29 | // Then it is beyond the starting location for the box and we should reset it. |
Dogstopper | 3:27889fffc2f7 | 30 | resetLocationAndSpeed(); |
Dogstopper | 3:27889fffc2f7 | 31 | } |
Dogstopper | 0:6a49493943be | 32 | } |
Dogstopper | 0:6a49493943be | 33 | |
Dogstopper | 0:6a49493943be | 34 | void Invader::draw() |
Dogstopper | 0:6a49493943be | 35 | { |
Dogstopper | 0:6a49493943be | 36 | screen_mutex.lock(); |
Dogstopper | 3:27889fffc2f7 | 37 | screen->filled_rectangle(lastX-WIDTH/2-1, lastY-HEIGHT/2-1, lastX+3*WIDTH/2, lastY+3*HEIGHT/2, BLACK); |
Dogstopper | 3:27889fffc2f7 | 38 | screen->filled_rectangle(x, y, x+WIDTH, y+HEIGHT, RED); |
Dogstopper | 0:6a49493943be | 39 | screen_mutex.unlock(); |
Dogstopper | 0:6a49493943be | 40 | } |
Dogstopper | 0:6a49493943be | 41 | |
Dogstopper | 0:6a49493943be | 42 | bool Invader::intersects(int otherX, int otherY, int otherWidth, int otherHeight) |
Dogstopper | 0:6a49493943be | 43 | { |
Dogstopper | 0:6a49493943be | 44 | if (((otherX > x && otherX <= x + WIDTH) || (otherX + otherWidth > x && otherX + otherWidth <= x + WIDTH)) && |
Dogstopper | 0:6a49493943be | 45 | ((otherY > y && otherY <= y + HEIGHT) || (otherY + otherHeight > y && otherY + otherHeight <= y + HEIGHT))) { |
Dogstopper | 3:27889fffc2f7 | 46 | printf("Intersects\n"); |
Dogstopper | 0:6a49493943be | 47 | return true; |
Dogstopper | 0:6a49493943be | 48 | } |
Dogstopper | 0:6a49493943be | 49 | else { |
Dogstopper | 3:27889fffc2f7 | 50 | printf("Not Intersecting\n\n"); |
Dogstopper | 0:6a49493943be | 51 | return false; |
Dogstopper | 0:6a49493943be | 52 | } |
Dogstopper | 0:6a49493943be | 53 | } |
Dogstopper | 3:27889fffc2f7 | 54 | |
Dogstopper | 3:27889fffc2f7 | 55 | void Invader::resetLocationAndSpeed() |
Dogstopper | 3:27889fffc2f7 | 56 | { |
Dogstopper | 3:27889fffc2f7 | 57 | int side = rand() % 4; |
Dogstopper | 3:27889fffc2f7 | 58 | switch(side) { |
Dogstopper | 3:27889fffc2f7 | 59 | case 0: // Left |
Dogstopper | 3:27889fffc2f7 | 60 | x = -9; |
Dogstopper | 3:27889fffc2f7 | 61 | y = (int)(rand() % (windowHeight - HEIGHT)); |
Dogstopper | 3:27889fffc2f7 | 62 | spdX = (int)(rand() % 3 + 1); |
Dogstopper | 3:27889fffc2f7 | 63 | spdY = (int)(((int)rand())==0 ? -1 : 1)*(rand() % 3); |
Dogstopper | 3:27889fffc2f7 | 64 | break; |
Dogstopper | 3:27889fffc2f7 | 65 | case 1: // Right |
Dogstopper | 3:27889fffc2f7 | 66 | x = windowWidth - 1; |
Dogstopper | 3:27889fffc2f7 | 67 | y = (int)(rand() % (windowHeight - HEIGHT)); |
Dogstopper | 3:27889fffc2f7 | 68 | spdX = (int) (-1*(rand() % 3 + 1)); |
Dogstopper | 3:27889fffc2f7 | 69 | spdY = (int)(((int)rand())==0 ? -1 : 1)*(rand() % 3); |
Dogstopper | 3:27889fffc2f7 | 70 | break; |
Dogstopper | 3:27889fffc2f7 | 71 | case 2: // Top |
Dogstopper | 3:27889fffc2f7 | 72 | x = (int)(rand() % (windowWidth - WIDTH)); |
Dogstopper | 3:27889fffc2f7 | 73 | y = -9; |
Dogstopper | 3:27889fffc2f7 | 74 | spdX = (int)(((int)rand())==0 ? -1 : 1)*(rand() % 3); |
Dogstopper | 3:27889fffc2f7 | 75 | spdY = (int)(rand() % 3 + 1); |
Dogstopper | 3:27889fffc2f7 | 76 | break; |
Dogstopper | 3:27889fffc2f7 | 77 | case 3: // Bottom |
Dogstopper | 3:27889fffc2f7 | 78 | x = (int)(rand() % (windowWidth - WIDTH)); |
Dogstopper | 3:27889fffc2f7 | 79 | y = windowWidth-1; |
Dogstopper | 3:27889fffc2f7 | 80 | spdX = (int)(((int)rand())==0 ? -1 : 1)*(rand() % 3); |
Dogstopper | 3:27889fffc2f7 | 81 | spdY = (int)(-1*(rand() % 3 + 1)); |
Dogstopper | 3:27889fffc2f7 | 82 | break; |
Dogstopper | 3:27889fffc2f7 | 83 | } |
Dogstopper | 3:27889fffc2f7 | 84 | } |