A simple one-level platform game. Developed as part of ELEC2645 at University of Leeds, spring 2015.
Dependencies: N5110 PinDetect PowerControl mbed
An ARM mbed LPC1768 microcontroller have been used to develop a handheld arcade game in the style of an old-school platformer. This project is entirely my own independent work in all stages of the development; including design, defining project specifications, breadboard prototyping, schematic and PCB layout using CAD, assembly, testing and software development. Due to this being part of the ELEC2645 Embedded Systems Project module at University of Leeds, spring 2015, limitations were given on the available hardware components. Credit is due to the authors of the dependent libraries (N5110, Pin Detect, PowerControl and mbed). I would also like to thank the author of Game Programming Patterns as well as the authors of SFML Game Development for providing me with useful sources for programming design patterns.
Project aims
- Implement simple gameplay:
- A single, fixed (no scrolling) level.
- Player can move left to right, jump and shoot.
- Enemies will drop from the top of the screen.
- The player gets points for shooting enemies.
- The player dies when it gets hits by an enemy.
- Implement a simple menu system.
- Enable the user to adjust the brightness of the display.
- Output sound to enhance the user experience.
Software
The program flow is controlled by a finite state machine. The implemented design was inspired by the State design pattern from the books Game Programming Patterns and SFML Game Development. The StateManager class is responsible for updating and rendering the current selected state. It also changes the state based on request from the current state. The framework built for the state machine used in this project makes it easy to add new screens. The different main states (indicated by the background colour) and how the user interaction is shown below:
Hardware
Schematic:
Printed circuit board (PCB):
Images
A seperate program was written to convert images (png) to text-representation of the maps. Enemies and numbers on the screen are also collected from a sprite-sheet created in the same manner.
Diff: Game.cpp
- Revision:
- 15:d5eb13c4c1c6
- Parent:
- 14:b4fed570abaf
- Child:
- 16:caf613d5b85e
--- a/Game.cpp Sat May 09 14:39:48 2015 +0000 +++ b/Game.cpp Sun May 10 09:37:51 2015 +0000 @@ -4,21 +4,40 @@ void Game::init() { + paused = false; + // Set initial values for the player player.x = 40; // start x player.y = 5; // start y player.width = player.height = 5; // important that this is correct, see size of Image::Player in Resources.h player.onGround = false; - enemy.x = 40; - enemy.y = 5; - enemy.vx = 1; - enemy.width = enemy.height = 5; + enemy = new Enemy(75, 3, 6, 5); + enemy->vx = -2; + enemy->facingLeft = true; } // Functions void Game::update(float dt) { + // Pause button input + if (input->read(Input::ButtonC)) + { + if (releasedBtnC) + { + paused = !paused; + releasedBtnC = false; + } + } + else + releasedBtnC = true; + + // Skip the rest if paused + if (paused) return; + + + + // Handle input, should be its own function switch(input->joystick->getDirection()) { @@ -41,16 +60,23 @@ break; } - // Random jump enemies - if (enemy.onGround && (rand() % 100) > 95) + // Random movement for enemies + if (enemy->onGround && (rand() % 100) > 97) // 3 % chance { - enemy.vy = -3; - enemy.onGround = false; + // jump + enemy->vy = -3; + enemy->onGround = false; + } + else if ((rand() % 100) > 98) // 2% chance + { + // switch direction + enemy->vx *= -1; + enemy->facingLeft = (enemy->vx < 0); } // Gravity player.vy += 1; - enemy.vy += 1; + enemy->vy += 1; // Check if player is trying to jump. Player can only jump if it's on the ground if (input->read(Input::ButtonA) && player.onGround) @@ -63,17 +89,30 @@ if (player.vy > TERMINAL_VELOCITY) player.vy = TERMINAL_VELOCITY; moveWithCollisionTest(&player, map); - moveWithCollisionTest(&enemy, map); + + if (!enemy->dead) + moveWithCollisionTest(enemy, map); + else + { + enemy->y += enemy->vy; + enemy->x += enemy->vx; + } + // else if (enemy->y >= HEIGHT) ; // TODO : delete enemy // Enemy AI - int nextRight = enemy.getRight() + 1; // Next position of right edge if enemy moves to the right - for (int i = 0; i < enemy.height; ++i) // Check for all heighs + if (!enemy->dead) { - // Check if crashing if moving right or left. Bounds should already be limited by moveWithCollisionTest! - if (map[enemy.y + i][nextRight] || map[enemy.y + i][enemy.x - 1]) + int nextRight = enemy->getRight() + 1; // Next position of right edge if enemy moves to the right + for (int i = 0; i < enemy->height; ++i) // Check for all heighs { - enemy.vx *= -1; // move in opposite direction - break; // no further testing required + // Check if crashing if moving right or left. Bounds should already be limited by moveWithCollisionTest! + // will also try to climb a slope if it can find one + if ((map[enemy->y + i][nextRight] && map[enemy->y+i-1][nextRight]) || (map[enemy->y + i][enemy->x - 1] && map[enemy->y + i-1][enemy->x - 1])) + { + enemy->vx *= -1; // move in opposite direction + enemy->facingLeft = !enemy->facingLeft; // toggle direction + break; // no further testing required + } } } @@ -82,7 +121,7 @@ { // Create a new bullet and give it initial values Point* bullet = new Point; - bullet->x = (player.facingLeft) ? (player.x-1) : (player.x + player.width); + bullet->x = (int)(player.x + (player.width / 2)); //(player.facingLeft) ? (player.x-1) : (player.x + player.width); bullet->y = player.y + 2; bullet->vx = (player.facingLeft) ? -4 : 4; bullet->vy = 0; @@ -93,20 +132,57 @@ else if (!input->read(Input::ButtonB)) releasedBtnB = true; - // Loop through bullets and move them + // Loop through bullets and move them + collision test for (std::vector<Point*>::iterator it = bullets.begin(); it != bullets.end();) { - (*it)->x += (*it)->vx; + Point* bullet = *it; + + int x0; // left border of collision rect + int x1; // right border of collision rect + + int oldX = bullet->x; + int newX = bullet->x + bullet->vx; + + x0 = min(oldX, newX); + x1 = max(oldX, newX); + + // Collision rect for bullet in this time step + Rectangle bulletColRect(x0, bullet->y, (x1-x0)+1, 1); - // Check if outside - int x = (*it)->x; - if (x < 0 || x > WIDTH) + bool col = false; + // Delete if outside screen + if (newX < 0 || newX > WIDTH || bulletHitMap(bulletColRect, map)) // if outside screen + { + col = true; + } + else { - delete (*it); + // loop through all enemies + //bullet // point + //enemy // entity + + if (!enemy->dead && bullet->x >= enemy->x && bullet->x <= enemy->getRight() && bullet->y >= enemy->y && bullet->y <= enemy->getBottom()) + { + col = true; + // break; // todo: make for loop - iterate through all enemies + enemy->vx = bullet->vx / 2; // sends the dead enemy in the same direction as the incoming bullet + enemy->vy = -3; // sends the dead enemy upwards in the air, because of impact + + enemy->dead = true; + // remove enemy - todo: make vector + } + } + + if (!col) + { + ++it; // go to next element + bullet->x += bullet->vx; // update position + } + else + { + delete bullet; it = bullets.erase(it); // go to next element } - else - ++it; // go to next element // TODO: Check for collisions // TODO: Go both ways @@ -122,7 +198,7 @@ drawImage(Image::Player, player.x, player.y, false, !player.facingLeft); // Draw enemies - drawImage(Image::Enemy1, enemy.x, enemy.y, false, !enemy.facingLeft); + drawImage(Image::Enemy3, enemy->x, enemy->y, false, !enemy->facingLeft, enemy->dead); /* // Draw player - TODO: Make this a part of sprite class (so they can draw themselves) @@ -154,6 +230,14 @@ if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT) // Boundary check lcd->setPixel(x,y); } + + // Draw pause + if (paused) + { + lcd->drawRect(24, 13, 40, 13, 0); // outline + lcd->drawRect(25, 14, 38, 11, 2); // white fill + lcd->printString("Paused", 27, 2); // text + } } // Collision test between entites and map @@ -176,8 +260,17 @@ { if (map[y+i][entityRight+1]) // If moving to the right leads to collision for given y+i { - collision = true; // Then collision is true - break; // Skip the for loop, no need for further testing + // Slope + allows player to climb to top of platform by going right if it hits close to top of wall. + if (!map[y+i-1][entityRight+1] && entity->onGround) + { + entity->vy = -1; + } + else + { + collision = true; // Then collision is true + break; // Skip the for loop, no need for further testing + } + } } @@ -200,8 +293,15 @@ { if (map[y+i][x-1]) // If solid block { - collision = true; - break; // Collision detected, no further testing required + if (!map[y+i-1][x-1] && entity->onGround) // If slope or close to top of wall (=> can climb by going left). + { + entity->vy = -1; + } + else + { + collision = true; + break; // Collision detected, no further testing required + } } } @@ -242,7 +342,10 @@ break; // Skip the while loop as the entity can not move further downwards } else // Can safely move entity without collision + { ++entityBottom; // Move entity one step down + entity->onGround = false; + } } entity->y = entityBottom - (entity->height - 1); // Update position when done moving, remember that entity.y refers to upper part of the entity @@ -273,4 +376,25 @@ entity->y = y; // Update vertical position of entity } +} + +bool Game::hitTestRect(Rectangle r1, Rectangle r2) +{ + return ((r1.x + r1.width > r2.x) // r1's right edge to the right of r2's left edge + && (r1.x < r2.x + r2.width) // r1's left edge to the left of r2's right edge + && (r1.y + r2.height > r2.y) // r1's bottom lower than r2's top + && (r1.y < r2.y + r2.height)); // r1's top higher than r2's bottom + + // Note: Right border: r1.x + r1.width - 1, but we don't need to subtract 1 as we use > instead of >= +} + +bool Game::bulletHitMap(Rectangle &bulletColRect, const int map[HEIGHT][WIDTH]) +{ + for (int j = 0; j < bulletColRect.width; ++j) + { + if (map[bulletColRect.y][bulletColRect.x + j]) + return true; + } + + return false; } \ No newline at end of file