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: mbed
Sprites/Sprites.h
- Committer:
- el17arm
- Date:
- 2019-04-24
- Revision:
- 53:082c22ac2f9a
- Parent:
- 52:715791fc08a1
- Child:
- 54:7fa8c07fdea4
File content as of revision 53:082c22ac2f9a:
#ifndef SPRITES_H #define SPRITES_H #include "mbed.h" #include "N5110.h" #include "Gamepad.h" /////////// All sprite bitmaps//////////////// // right facing miner bitmap const int miner_right[24] = { 1,1,1, 1,1,0, 1,1,1, 0,1,0, 1,1,1, 1,1,1, 0,1,0, 0,1,1, }; // left facing miner bitmap const int miner_left[24] = { 1,1,1, 0,1,1, 1,1,1, 0,1,0, 1,1,1, 1,1,1, 0,1,0, 1,1,0, }; // enemy bitmap const int enemy[15] = { 1,1,1, 1,0,1, 1,1,1, 0,1,0, 1,1,1, }; // key bitmap const int key[12] = { 1,1,0,0, 1,0,1,1, 1,1,0,1, }; // blank bitmap to show key collected const int key_collected[12] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, }; // trap bitmap const int spike[9] = { 1,0,1, 0,1,0, 1,0,1, }; // solid block bitmap const int solid_block[18] = { 1,1,1,1,1,1, 1,0,1,1,0,1, 1,1,1,1,1,1, }; // level exit bitmap const int door[30] = { 1,1,1,1,1, 1,0,1,0,1, 1,1,0,1,1, 1,0,1,0,1, 1,1,0,1,1, 1,0,1,0,1, }; /////////structs for sprite parameters and collision detection////////////// /** Keys position struct */ struct Key_init { bool key[5]; /**< initialise key flag array identifies each key individually */ double kx[5]; /**< key x position*/ double ky[5]; /**< key y position*/ }; /** Enemy position and movement struct */ struct Enemies_init { bool eflag[5]; /**< initialise enemy flag array identifies each key individually */ double ex[5]; /**< enemy x position */ double ey[5]; /**< enemy y position */ int counter[5]; /**< enemy counter, counts pixels moved */ int distance[5];/**< distance enemy will travel before turning round */ }; /** Solid blocks struct */ struct Solid_blocks_init { double bx[5]; /**< block x position array identifies each block individually */ double by[5]; /**< block y position array */ bool collision[5]; /**< collision indicator for each individual block */ }; /////////////structs for populating up to 6 levels////////////////// /** Enemy locations for each level struct */ struct Enemies { double ex[5]; /**< enemy x positions for each level */ double ey[5]; /**< enemy y positions for each level */ int c[5]; /**< enemy counters for each level */ int d[5]; /**< enemy travel distances for each level */ double v[5]; /**< varies enemies velocity */ }; /** Trap locations for each level struct */ struct Traps { double tx[5]; /**< traps x positions for each level */ double ty[5]; /**< traps y positions for each level */ }; /** Key locations for each level struct*/ struct Keys { double kx[5]; /**< keys x positions for each level */ double ky[5]; /**< keys y positions for each level */ }; /** block locations for each level struct*/ struct Solid_blocks { double bx[5]; /**< blocks x positions for each level */ double by[5]; /**< blocks y positions for each level */ }; /** sinking block locations for each level struct*/ struct Soft_blocks { double sx1[5]; /**< sinking blocks x1 positions for each level */ double sy[5]; /**< sinking blocks y positions for each level */ int sx2[5]; /**< sinking blocks x2 (length) positions for each level */ }; /** Exit location for each level struct */ struct Level_exit { double lx[5]; /**< Exit x position for each level */ double ly[5]; /**< Exit y position for each level */ }; /** Sprites Class @author Andrew Milner University of Leeds @brief Draws sprites and sets all collision rules @date April 2019 */ class Sprites { public: /** Constructor */ Sprites(); /** Deconstructor */ ~Sprites(); /** Gets x and y position of player. */ Vector2D get_pos(); /** States starting position of player. @param miner x position. @param miner y position. */ void miner_init(int x, int y); /** Updates player x position. @details Updates x positon depending on direction of joystick. Will only update * position if no collision detected. Function also stops player leaving screen * at the left and right sides of screen. @param Direction enum from Gamepad library. */ void miner_move(Direction d, N5110 &lcd); /** Draws sprite facing direction moving. @details _direction states if player left or right facing, function draws * left or right facing sprite according. */ void miner_draw(N5110 &lcd); /** States when player is on a platform. @details if _jump true then player can jump, this prevents mid-air jumping. * Also states conditions for gravity so player will fall unless on top * of a platform. */ void miner_land(N5110 &lcd); /** Contains jump conditions and updates player position while jumping. @details When A button pressed players y position increases until preset value * is reached. Jump flag is true while player y is increasing and false all other * times, this prevents double jumping. */ void miner_jump(N5110 &lcd, Gamepad &pad); /** Player falls if not on platform. */ void miner_gravity(N5110 &lcd); /** initialises enemies @param enemy index, these will always be 0, 1, 2 so each enemy is treated individually. @param enemy x position. @param enemy y position. @param distance in pixels enemy to travel. */ void enemy_init(int i, int x, int y, int d); /** Updates enemy movement. @param enemy index, these will always be 0, 1, 2 so each enemy is treated individually. @param stets how many pixels enemies will move each loop. */ void enemy_move(int i, double v, N5110 &lcd); /** States conditions for collision between enemy and player. @details uses get_pos() and detects if any overlap between player and enemies. @param enemy index, these will always be 0, 1, 2 so each enemy is treated individually. @return will return true if collision detected. */ bool enemy_collision(int i); /** States conditions for drawing and collection of keys. @details Each key is displayed while key flag is false, once collected, key is deleted * and flag is changed to true. @param each key indexed so they are treated independently. @param keys x position. @param keys y position. */ void key_collect(int k, int x, int y, N5110 &lcd, Gamepad &pad); /** Counts keys collected. @return number of keys collected. */ int keys_collected(); /** Draws level exit and detects player collision @details takes player position from get_pos() and detects if overlap between player and the exit sprite. @param Exit x position. @param Exit y position. */ bool exit_level(int x, int y, N5110 &lcd); /** Draws traps and detects player collision @details takes player position from get_pos() and detects if overlap between player and the trap sprite. @param Trap x position. @param Trap y position. */ bool trap(int x, int y, N5110 &lcd); /** Draws blocks and detects player collision. @param Direction needed so player can turn around if collision with block. @Param each block indexed so collision rules between blocks dont clash. @param Block x position. @param Block y position. */ void blocks(Direction d, int i, int x, int y, N5110 &lcd); /** Checks if player has collided with any blocks. @return If player is contact with any block returns true. */ bool block_collision(); /** Draws sinking blocks and detects player contact. @param Sinking block x starting position. @param Block y position. @param Sinking block x finish position. */ void soft_blocks(int x, int y, int z, N5110 &lcd); Key_init _k; private: int _direction; bool _gravity; bool _jump; int _y; int _x; bool _j_flag; int _j_counter; bool block[5]; bool right_collision; bool left_collision; bool _collision; int _keys; Enemies_init _enem; Solid_blocks_init _b; }; #endif