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.
lib/Donkey/Donkey.cpp@20:c4e6941c98e2, 2019-05-08 (annotated)
- Committer:
 - Kern_EL17KJTF
 - Date:
 - Wed May 08 23:46:49 2019 +0000
 - Revision:
 - 20:c4e6941c98e2
 - Parent:
 - 15:9e8d1b6fe81e
 
Documentation - Donkey Kong Class Complete.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| Kern_EL17KJTF | 10:28575a6eaa13 | 1 | /* | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 2 | ELEC2645 Project | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 3 | Donkey.cpp | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 4 | Class file for Donkey Kong in Donkey Kong game. | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 5 | */ | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 6 | |
| Kern_EL17KJTF | 10:28575a6eaa13 | 7 | #include "Donkey.h" | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 8 | |
| Kern_EL17KJTF | 20:c4e6941c98e2 | 9 | // Constructor - Doesn't require any setup. | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 10 | Donkey::Donkey() | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 11 | { | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 12 | |
| Kern_EL17KJTF | 10:28575a6eaa13 | 13 | } | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 14 | |
| Kern_EL17KJTF | 20:c4e6941c98e2 | 15 | // Deconstructor - Doesn't require any setup. | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 16 | Donkey::~Donkey() | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 17 | { | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 18 | |
| Kern_EL17KJTF | 10:28575a6eaa13 | 19 | } | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 20 | |
| Kern_EL17KJTF | 20:c4e6941c98e2 | 21 | // External variables to be used inside and out of the class. | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 22 | int donkeykong_x = 0; // Donkey Kong's coordinates. | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 23 | int donkeykong_y = 34; | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 24 | int donkey_kong_speed = 10; // Donkey Kong's dropspeed. | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 25 | int donkey_direction; // Joystick input. | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 26 | |
| Kern_EL17KJTF | 20:c4e6941c98e2 | 27 | int game_dk_walking_right_1[14][16] = { // Donkey Kong moving right sprite | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 28 | {0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 29 | {0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 30 | {0,0,0,0,0,0,0,0,1,1,0,1,1,0,1,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 31 | {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 32 | {0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 33 | {0,0,0,0,1,1,0,0,0,1,0,0,1,0,1,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 34 | {0,0,1,1,1,0,0,0,0,0,1,1,1,1,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 35 | {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 36 | {0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 37 | {1,0,0,0,1,1,1,0,0,1,1,1,0,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 38 | {1,0,0,1,0,0,0,1,1,1,0,0,1,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 39 | {1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 40 | {1,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 41 | {1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,}, | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 42 | }; | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 43 | |
| Kern_EL17KJTF | 20:c4e6941c98e2 | 44 | int game_dk_walking_left_1[14][16] = { // Donkey Kong moving left sprite | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 45 | {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 46 | {0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 47 | {0,1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 48 | {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 49 | {1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 50 | {0,1,0,1,0,0,1,0,0,0,1,1,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 51 | {0,0,1,1,1,1,0,0,0,0,0,1,1,1,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 52 | {0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 53 | {0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 54 | {1,0,0,0,1,1,1,0,0,1,1,1,0,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 55 | {1,0,0,1,0,0,1,1,1,0,0,0,1,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 56 | {1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 57 | {1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 58 | {1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,}, | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 59 | }; | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 60 | |
| Kern_EL17KJTF | 20:c4e6941c98e2 | 61 | int game_dk_stationary[14][16] = { // Donkey Kong stationary sprite | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 62 | {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 63 | {0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 64 | {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 65 | {0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 66 | {0,0,0,1,0,1,1,0,1,1,0,1,0,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 67 | {0,0,1,1,0,1,1,0,1,1,0,0,1,0,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 68 | {0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 69 | {0,0,1,0,1,0,0,0,0,0,1,0,0,1,0,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 70 | {0,1,0,0,0,1,1,1,1,1,0,0,0,0,1,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 71 | {0,1,0,0,1,0,0,1,0,0,0,1,0,0,1,0,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 72 | {1,0,0,1,0,1,0,0,0,0,1,0,1,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 73 | {1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 74 | {1,0,1,0,1,0,0,1,1,0,0,1,0,1,0,1,}, | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 75 | {0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,}, | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 76 | }; | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 77 | |
| Kern_EL17KJTF | 20:c4e6941c98e2 | 78 | // Creates the player controlled unit Donkey Kong, then moves it left or right based on joystick input. Points on collision with bananas. Gameover on collision with barrels. | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 79 | void Donkey::donkeykong_movement(Gamepad &pad, N5110 &lcd) { | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 80 | donkey_direction = pad.get_direction(); | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 81 | if (donkey_direction == NE || donkey_direction == E || donkey_direction == SE) { // If joystick moved right, the right position sprite will be loaded and donkey kong will move right. | 
| Kern_EL17KJTF | 12:50a7abf21f18 | 82 | donkeykong_x = donkeykong_x + 3; | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 83 | lcd.drawSprite(donkeykong_x,donkeykong_y,14,16,(int *)game_dk_walking_right_1); | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 84 | } | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 85 | else if (donkey_direction == NW || donkey_direction == W || donkey_direction == SW) { // If joystick moved left, the left position sprite will be loaded and donkey kong will move left. | 
| Kern_EL17KJTF | 12:50a7abf21f18 | 86 | donkeykong_x = donkeykong_x - 3; | 
| Kern_EL17KJTF | 15:9e8d1b6fe81e | 87 | lcd.drawSprite(donkeykong_x,donkeykong_y,14,16,(int *)game_dk_walking_left_1); | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 88 | } | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 89 | else { // If joystick not moved, then stationary sprite will be loaded and no movement occurs. | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 90 | lcd.drawSprite(donkeykong_x,donkeykong_y,14,16,(int *)game_dk_stationary); | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 91 | } | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 92 | lcd.refresh(); | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 93 | if (donkeykong_x > 68) { // Stops donkey kong model going off screen. | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 94 | donkeykong_x = 68; | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 95 | } | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 96 | if (donkeykong_x < 0) { | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 97 | donkeykong_x = 0; | 
| Kern_EL17KJTF | 10:28575a6eaa13 | 98 | } | 
| Kern_EL17KJTF | 20:c4e6941c98e2 | 99 | } |