Mini game developed for ECE 4180 lab

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Committer:
slandry8
Date:
Mon Mar 14 17:42:07 2016 +0000
Revision:
0:a358215f57b7
Final build of game for ECE 4180

Who changed what in which revision?

UserRevisionLine numberNew contents of line
slandry8 0:a358215f57b7 1 #include "mbed.h"
slandry8 0:a358215f57b7 2 #include "rtos.h"
slandry8 0:a358215f57b7 3 #include "uLCD_4DGL.h"
slandry8 0:a358215f57b7 4 #define DELTA_Y 3
slandry8 0:a358215f57b7 5 #define DELTA_X 3
slandry8 0:a358215f57b7 6 #define DELTA_X_P 2
slandry8 0:a358215f57b7 7 #define COVER_COLOR 0xf0c890
slandry8 0:a358215f57b7 8 #define H_32 32
slandry8 0:a358215f57b7 9 #define H_16 16
slandry8 0:a358215f57b7 10 #define STATE_START 0
slandry8 0:a358215f57b7 11 #define STATE_GAME 1
slandry8 0:a358215f57b7 12 #define STATE_LOSE 2
slandry8 0:a358215f57b7 13 #define STATE_SCORE 4
slandry8 0:a358215f57b7 14 #define MOVE_LEFT 1
slandry8 0:a358215f57b7 15 #define MOVE_RIGHT 2
slandry8 0:a358215f57b7 16 #define MOVE_UP 4
slandry8 0:a358215f57b7 17 #define MOVE_DOWN 8
slandry8 0:a358215f57b7 18
slandry8 0:a358215f57b7 19 class Entity {
slandry8 0:a358215f57b7 20 private:
slandry8 0:a358215f57b7 21 public:
slandry8 0:a358215f57b7 22 //y position of an entity (in pixels)
slandry8 0:a358215f57b7 23 int row;
slandry8 0:a358215f57b7 24 //x position of an entity (in pixels)
slandry8 0:a358215f57b7 25 int col;
slandry8 0:a358215f57b7 26
slandry8 0:a358215f57b7 27 };
slandry8 0:a358215f57b7 28 class Cursor : public Entity {
slandry8 0:a358215f57b7 29 public:
slandry8 0:a358215f57b7 30 //size of the s by s square cursor
slandry8 0:a358215f57b7 31 int s;
slandry8 0:a358215f57b7 32 //space that the cursor will move when the joystick is pressed left or right
slandry8 0:a358215f57b7 33 int delta_x;
slandry8 0:a358215f57b7 34 //space that the cursor will move when the joystick is pressed up or down
slandry8 0:a358215f57b7 35 int delta_y;
slandry8 0:a358215f57b7 36 //buffer that will contain the initials for a new high score
slandry8 0:a358215f57b7 37 char initBuf[3];
slandry8 0:a358215f57b7 38 int c;
slandry8 0:a358215f57b7 39 //flag to see if the user moved the cursor
slandry8 0:a358215f57b7 40 bool hasMoved;
slandry8 0:a358215f57b7 41 //flag to see if the user has pressed the fire button on the high score screen
slandry8 0:a358215f57b7 42 bool letterPressed;
slandry8 0:a358215f57b7 43 //numerical value assoicated with a particular movement
slandry8 0:a358215f57b7 44 int moveFlag;
slandry8 0:a358215f57b7 45 //the last letter that the cursor selected on letterPressed
slandry8 0:a358215f57b7 46 char curChar;
slandry8 0:a358215f57b7 47 //default no-arg constructor
slandry8 0:a358215f57b7 48 Cursor(void);
slandry8 0:a358215f57b7 49 //function to draw the cursor on screen
slandry8 0:a358215f57b7 50 void draw(uLCD_4DGL *u);
slandry8 0:a358215f57b7 51 //function to cover up the previous position of the cursor
slandry8 0:a358215f57b7 52 void coverUp(uLCD_4DGL *u);
slandry8 0:a358215f57b7 53 };
slandry8 0:a358215f57b7 54 class Player : public Entity {
slandry8 0:a358215f57b7 55 public:
slandry8 0:a358215f57b7 56 //Width of the p;ayer in pixels
slandry8 0:a358215f57b7 57 int w;
slandry8 0:a358215f57b7 58 //Height of the player in pixels
slandry8 0:a358215f57b7 59 int h;
slandry8 0:a358215f57b7 60 //(deprecated) color of the player in RGB
slandry8 0:a358215f57b7 61 int color;
slandry8 0:a358215f57b7 62 //default no-arg constructor
slandry8 0:a358215f57b7 63 Player(void);
slandry8 0:a358215f57b7 64 //flag to see if the user has moved using the joystick
slandry8 0:a358215f57b7 65 bool hasMoved;
slandry8 0:a358215f57b7 66 //flag to see if the user is jumping
slandry8 0:a358215f57b7 67 bool isJumping;
slandry8 0:a358215f57b7 68 //flag to see if the user is falling
slandry8 0:a358215f57b7 69 bool isFalling;
slandry8 0:a358215f57b7 70 //Used to keep track of how long until the player reaches the apex of the jump
slandry8 0:a358215f57b7 71 int upCounter;
slandry8 0:a358215f57b7 72 //Used to check how long until the player lands
slandry8 0:a358215f57b7 73 int fallCounter;
slandry8 0:a358215f57b7 74 //value representing some movement of direction
slandry8 0:a358215f57b7 75 int moveFlag;
slandry8 0:a358215f57b7 76 };
slandry8 0:a358215f57b7 77 class Obstacle : public Entity {
slandry8 0:a358215f57b7 78 public:
slandry8 0:a358215f57b7 79 //(deprecated) color of the obstacle in RGB
slandry8 0:a358215f57b7 80 int color;
slandry8 0:a358215f57b7 81 //width of the obstacle in pixels
slandry8 0:a358215f57b7 82 int w;
slandry8 0:a358215f57b7 83 //height of the obstacle in pixels
slandry8 0:a358215f57b7 84 int h;
slandry8 0:a358215f57b7 85 //default constructor; color input variable is deprecated
slandry8 0:a358215f57b7 86 Obstacle(int c);
slandry8 0:a358215f57b7 87 };
slandry8 0:a358215f57b7 88 class Counter : public Entity {
slandry8 0:a358215f57b7 89 };
slandry8 0:a358215f57b7 90 class Screen {
slandry8 0:a358215f57b7 91 private:
slandry8 0:a358215f57b7 92 public:
slandry8 0:a358215f57b7 93 //(deprecated) sector address of the screen's background
slandry8 0:a358215f57b7 94 int bgAddr;
slandry8 0:a358215f57b7 95 //current position, on or off-screen, in pixels
slandry8 0:a358215f57b7 96 int pos;
slandry8 0:a358215f57b7 97 //
slandry8 0:a358215f57b7 98 void drawScreen(void);
slandry8 0:a358215f57b7 99 //increase the position values of each object and screen, so that they appear to be moving
slandry8 0:a358215f57b7 100 void decScreen(void);
slandry8 0:a358215f57b7 101 //default constructor
slandry8 0:a358215f57b7 102 Screen(int bg, int initPos);
slandry8 0:a358215f57b7 103 //main constructor, though the boolean flag serves no purpose and should be removed
slandry8 0:a358215f57b7 104 Screen(int bg, bool randomSpots, int initPos);
slandry8 0:a358215f57b7 105 //the 4 cactus obstacles associated with this screen
slandry8 0:a358215f57b7 106 Obstacle *obs[4];
slandry8 0:a358215f57b7 107 //Class destructor. Needed to delete both the screen and its associated obstacle objects
slandry8 0:a358215f57b7 108 ~Screen(void);
slandry8 0:a358215f57b7 109 };
slandry8 0:a358215f57b7 110 //(Deprecated) draw the rectangles of the obstacles while also checking if they are on-screen
slandry8 0:a358215f57b7 111 void DrawIfOnScreen(Obstacle *o, uLCD_4DGL *u);
slandry8 0:a358215f57b7 112 //draw the cactus image for each obstacle, also checking if any part of the obstacle is off-screen, and drawing the appropriate image if so
slandry8 0:a358215f57b7 113 void DrawSliceBySlice(Obstacle *o, uLCD_4DGL *u);
slandry8 0:a358215f57b7 114 //Draw a filled rectangle with the background color to hide the pixels of the previous position of an object
slandry8 0:a358215f57b7 115 void DrawCoverUp(int row, int col, int w, int h, uLCD_4DGL *u);
slandry8 0:a358215f57b7 116 //Check if any screens are now past the player, and rearrange the screens if so
slandry8 0:a358215f57b7 117 void evalScreens(Screen **s);
slandry8 0:a358215f57b7 118 //draw all objects associated with a screen
slandry8 0:a358215f57b7 119 void drawScreens(uLCD_4DGL *u, Screen **s);
slandry8 0:a358215f57b7 120 //draw the player sprite
slandry8 0:a358215f57b7 121 void drawPlayer(uLCD_4DGL *u, Player *p);
slandry8 0:a358215f57b7 122 //(deprecated) draw the cover up rectangle for x-axis movement of the player
slandry8 0:a358215f57b7 123 void drawPlayerCoverUp(uLCD_4DGL *u, Player *p);
slandry8 0:a358215f57b7 124 //Draw the cover up rectangle for x-axis movement of the player
slandry8 0:a358215f57b7 125 void drawPlayerCoverUp(uLCD_4DGL *u, Player *p, int dir);
slandry8 0:a358215f57b7 126 //draw the cover up rectangle for y-axis movement of the player
slandry8 0:a358215f57b7 127 void drawPlayerAirborneCoverUp(uLCD_4DGL *u, Player *p);
slandry8 0:a358215f57b7 128 //Cover up the small square that occurs if the user moves diagonally
slandry8 0:a358215f57b7 129 void drawPlayerDiagCoverUp(uLCD_4DGL *u, Player *p);
slandry8 0:a358215f57b7 130 //Draw the sun and any other background objects
slandry8 0:a358215f57b7 131 void drawScenery(uLCD_4DGL *u);
slandry8 0:a358215f57b7 132 //Collision Detection
slandry8 0:a358215f57b7 133 //check a collision on the top-right corner of the player
slandry8 0:a358215f57b7 134 bool detectTR(Obstacle *o,Player *p);
slandry8 0:a358215f57b7 135 //check a collision on the bottom-left corner of the player
slandry8 0:a358215f57b7 136 bool detectBL(Obstacle *o,Player *p);
slandry8 0:a358215f57b7 137 //check a collision on the bottom-right corner of the player
slandry8 0:a358215f57b7 138 bool detectBR(Obstacle *o,Player *p);
slandry8 0:a358215f57b7 139 //check a collision on the top-left corner of the player
slandry8 0:a358215f57b7 140 bool detectTL(Obstacle *o, Player *p);
slandry8 0:a358215f57b7 141 //main function for collision detection; runs previous four functions and sets appropriate flags
slandry8 0:a358215f57b7 142 void CollisionDetect(Screen **s, Player *p,bool *flag);
slandry8 0:a358215f57b7 143 //draw the score counter on the right side of the screen
slandry8 0:a358215f57b7 144 void drawScore(uLCD_4DGL *u, int score);
slandry8 0:a358215f57b7 145 //Reset flags and other objects for a new game
slandry8 0:a358215f57b7 146 void initialize(Screen **s, Player *p, int *score, bool* initFlash, bool *scDrawn, bool *collide, int *c, int *r, Cursor *curs);