
Simon Atkinson 201255483
Dependencies: mbed
Snake Game
Summary
Hello and welcome to my Snake Game. Snake is a simple game where you control a snake and eat an apple which increases your size. Normally touching the walls will kill you, always touching yourself will make you die!
Controls
The Controls for this game are simple the Start button starts the game (who would have thought) this button is the left button on the bottom of the gamepad next to the two blue potentiometers. The Reset button on the right of the potentiometers resets the game. Once the game starts use the left thumbstick to control the movement of the snake.
If you hit the wall with your snake you will die and the game will end.
Bugs/Missing Features
Unfortunatley I wasn't able to get the game eating the apple to work, when I tried I could never get it to detect the collisions I tried a few different ways but ran out of time. Because that doesn't work the score doesn't increase and the apple doesn't spawn in a different place. I will try continue to work on this when I have time as I enjoyed doing this project even though it was very frustrating at times!
Gamepad2/Bitmap.cpp@21:e8d66c5f68cc, 2020-06-05 (annotated)
- Committer:
- Psy1990
- Date:
- Fri Jun 05 22:57:33 2020 +0000
- Revision:
- 21:e8d66c5f68cc
- Parent:
- 0:7423345f87c5
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:7423345f87c5 | 1 | #include "Bitmap.h" |
eencae | 0:7423345f87c5 | 2 | |
eencae | 0:7423345f87c5 | 3 | #include <iostream> |
eencae | 0:7423345f87c5 | 4 | |
eencae | 0:7423345f87c5 | 5 | #include "N5110.h" |
eencae | 0:7423345f87c5 | 6 | |
eencae | 0:7423345f87c5 | 7 | Bitmap::Bitmap(int const *contents, |
eencae | 0:7423345f87c5 | 8 | unsigned int const height, |
eencae | 0:7423345f87c5 | 9 | unsigned int const width) |
eencae | 0:7423345f87c5 | 10 | : |
eencae | 0:7423345f87c5 | 11 | _contents(std::vector<int>(height*width)), |
eencae | 0:7423345f87c5 | 12 | _height(height), |
eencae | 0:7423345f87c5 | 13 | _width(width) |
eencae | 0:7423345f87c5 | 14 | { |
eencae | 0:7423345f87c5 | 15 | // Perform a quick sanity check of the dimensions |
eencae | 0:7423345f87c5 | 16 | if (_contents.size() != height * width) { |
eencae | 0:7423345f87c5 | 17 | std::cerr << "Contents of bitmap has size " << _contents.size() |
eencae | 0:7423345f87c5 | 18 | << " pixels, but its dimensions were specified as " |
eencae | 0:7423345f87c5 | 19 | << width << " * " << height << " = " << width * height << std::endl; |
eencae | 0:7423345f87c5 | 20 | } |
eencae | 0:7423345f87c5 | 21 | |
eencae | 0:7423345f87c5 | 22 | for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i]; |
eencae | 0:7423345f87c5 | 23 | } |
eencae | 0:7423345f87c5 | 24 | |
eencae | 0:7423345f87c5 | 25 | /** |
eencae | 0:7423345f87c5 | 26 | * @returns the value of the pixel at the given position |
eencae | 0:7423345f87c5 | 27 | */ |
eencae | 0:7423345f87c5 | 28 | int Bitmap::get_pixel(unsigned int const row, |
eencae | 0:7423345f87c5 | 29 | unsigned int const column) const |
eencae | 0:7423345f87c5 | 30 | { |
eencae | 0:7423345f87c5 | 31 | // First check that row and column indices are within bounds |
eencae | 0:7423345f87c5 | 32 | if(column >= _width || row >= _height) |
eencae | 0:7423345f87c5 | 33 | { |
eencae | 0:7423345f87c5 | 34 | std::cerr << "The requested pixel with index " << row << "," << column |
eencae | 0:7423345f87c5 | 35 | << "is outside the bitmap dimensions: " << _width << "," |
eencae | 0:7423345f87c5 | 36 | << _height << std::endl; |
eencae | 0:7423345f87c5 | 37 | } |
eencae | 0:7423345f87c5 | 38 | |
eencae | 0:7423345f87c5 | 39 | // Now return the pixel value, using row-major indexing |
eencae | 0:7423345f87c5 | 40 | return _contents[row * _width + column]; |
eencae | 0:7423345f87c5 | 41 | } |
eencae | 0:7423345f87c5 | 42 | |
eencae | 0:7423345f87c5 | 43 | /** |
eencae | 0:7423345f87c5 | 44 | * @brief Prints the contents of the bitmap to the terminal |
eencae | 0:7423345f87c5 | 45 | */ |
eencae | 0:7423345f87c5 | 46 | void Bitmap::print() const |
eencae | 0:7423345f87c5 | 47 | { |
eencae | 0:7423345f87c5 | 48 | for (unsigned int row = 0; row < _height; ++row) |
eencae | 0:7423345f87c5 | 49 | { |
eencae | 0:7423345f87c5 | 50 | // Print each element of the row |
eencae | 0:7423345f87c5 | 51 | for (unsigned int column = 0; column < _width; ++column) |
eencae | 0:7423345f87c5 | 52 | { |
eencae | 0:7423345f87c5 | 53 | int pixel = get_pixel(row, column); |
eencae | 0:7423345f87c5 | 54 | std::cout << pixel; |
eencae | 0:7423345f87c5 | 55 | } |
eencae | 0:7423345f87c5 | 56 | |
eencae | 0:7423345f87c5 | 57 | // And then terminate with a new-line character |
eencae | 0:7423345f87c5 | 58 | std::cout << std::endl; |
eencae | 0:7423345f87c5 | 59 | } |
eencae | 0:7423345f87c5 | 60 | } |
eencae | 0:7423345f87c5 | 61 | |
eencae | 0:7423345f87c5 | 62 | /** |
eencae | 0:7423345f87c5 | 63 | * @brief Renders the contents of the bitmap onto an N5110 screen |
eencae | 0:7423345f87c5 | 64 | * |
eencae | 0:7423345f87c5 | 65 | * @param[in] lcd The screen to use for rendering |
eencae | 0:7423345f87c5 | 66 | * @param[in] x0 The horizontal position in pixels at which to render the bitmap |
eencae | 0:7423345f87c5 | 67 | * @param[in] y0 The vertical position in pixels at which to render the bitmap |
eencae | 0:7423345f87c5 | 68 | * |
eencae | 0:7423345f87c5 | 69 | * @details Note that x0, y0 gives the location of the top-left of the bitmap on |
eencae | 0:7423345f87c5 | 70 | * the screen. |
eencae | 0:7423345f87c5 | 71 | * This function only updates the buffer on the screen. You still need |
eencae | 0:7423345f87c5 | 72 | * to refresh the screen in order to actually see the bitmap. |
eencae | 0:7423345f87c5 | 73 | */ |
eencae | 0:7423345f87c5 | 74 | void Bitmap::render(N5110 &lcd, |
eencae | 0:7423345f87c5 | 75 | unsigned int const x0, |
eencae | 0:7423345f87c5 | 76 | unsigned int const y0) const |
eencae | 0:7423345f87c5 | 77 | { |
eencae | 0:7423345f87c5 | 78 | // Loop through each row of the bitmap image |
eencae | 0:7423345f87c5 | 79 | for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row) |
eencae | 0:7423345f87c5 | 80 | { |
eencae | 0:7423345f87c5 | 81 | // Row index on the screen for rendering the row of pixels |
eencae | 0:7423345f87c5 | 82 | unsigned int screen_row = y0 + bitmap_row; |
eencae | 0:7423345f87c5 | 83 | |
eencae | 0:7423345f87c5 | 84 | // Render each pixel in the row |
eencae | 0:7423345f87c5 | 85 | for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col) |
eencae | 0:7423345f87c5 | 86 | { |
eencae | 0:7423345f87c5 | 87 | // Column index on the screen for rendering this pixel |
eencae | 0:7423345f87c5 | 88 | int screen_col = x0 + bitmap_col; |
eencae | 0:7423345f87c5 | 89 | |
eencae | 0:7423345f87c5 | 90 | // Find the required value of the pixel at the given location within |
eencae | 0:7423345f87c5 | 91 | // the bitmap data and then write it to the LCD screen |
eencae | 0:7423345f87c5 | 92 | int pixel = get_pixel(bitmap_row, bitmap_col); |
eencae | 0:7423345f87c5 | 93 | lcd.setPixel(screen_col, screen_row, pixel); |
eencae | 0:7423345f87c5 | 94 | } |
eencae | 0:7423345f87c5 | 95 | } |
eencae | 0:7423345f87c5 | 96 | } |