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.
Diff: main.cpp
- Revision:
- 6:478f81e79d9b
- Parent:
- 5:5667c4ec3d7e
- Child:
- 7:ffbc921c20f7
--- a/main.cpp Sun May 05 03:29:31 2019 +0000 +++ b/main.cpp Tue May 07 15:19:56 2019 +0000 @@ -13,11 +13,169 @@ #include "main.h" +N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); +Gamepad pad; +Options opt; + int fps = 24; int direction; int menu_option_pos = 0; int arrow_pos = 0; +// Game State-------------------------------------------------------------- + +void game_engine_run() { + wait_ms(250); + + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + //printf("Game State"); + lcd.clear(); + lcd.printString("Game",0,0); + donkeykong_x_movement(); + wait_ms(1.0f/fps); + } +} + + +// Donkey ----------- +int donkeykong_x = 0; +int donkeykong_y = 32; +int donkey_kong_speed = 25; +void donkeykong_x_movement() { + direction = pad.get_direction(); + if (direction == NE || direction == E || direction == SE) { + donkeykong_x = donkeykong_x + 1; + lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_walking_right_1); + wait_ms(donkey_kong_speed); + lcd.refresh(); + } + else if (direction == NW || direction == W || direction == SW) { + donkeykong_x = donkeykong_x - 1; + lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_walking_left_1); + wait_ms(donkey_kong_speed); + lcd.refresh(); + } + else { + lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_stationary); + lcd.refresh(); + } + if (donkeykong_x > 68) { + donkeykong_x = 68; + } + if (donkeykong_x < 0) { + donkeykong_x = 0; + } +} +// Barrel ----------- + +// Banana ----------- + +// Controls State---------------------------------------------------------- +void controls_run() { + wait_ms(250); + + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + //printf("Control State"); + lcd.clear(); + lcd.printString("Controls",19,0); + lcd.refresh(); + wait_ms(1.0f/fps); + } +} + +// Instructions State------------------------------------------------------ +void instructions_run() { + wait_ms(250); + + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + //printf("Instructions State"); + lcd.clear(); + lcd.printString("Instructions",7,0); + lcd.refresh(); + wait_ms(1.0f/fps); + } +} + +// Options State----------------------------------------------------------- +float opt_brightness = 0.5; +float opt_contrast = 0.396; +int opt_volume = 1; + +Options::Options() +{ + +} + +Options::~Options() +{ + +} + +void Options::options_run(Gamepad &pad, N5110 &lcd) { + wait_ms(250); + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + //printf("Options State"); + lcd.clear(); + lcd.printString("Options",21,0); + options_brightness(pad, lcd); + options_contrast(pad, lcd); + options_volume(pad, lcd); + lcd.refresh(); + wait_ms(1.0f/fps); + } +} + +void Options::options_brightness(Gamepad &pad, N5110 &lcd) { + if (pad.check_event(Gamepad::B_PRESSED) == true) { + opt_brightness = 0; + } + if (pad.check_event(Gamepad::A_PRESSED) == true) { + opt_brightness = 0.5; + } + lcd.setBrightness(opt_brightness); + lcd.printString("A/B = BackLite",0,2); +} + +void Options::options_contrast(Gamepad &pad, N5110 &lcd) { + opt_contrast = pad.read_pot(); + lcd.setContrast(opt_contrast); + lcd.printString("Pot = Contrast",0,3); + printf("Contrast = %f", opt_contrast); +} + +void Options::options_volume(Gamepad &pad, N5110 &lcd) { + if (pad.check_event(Gamepad::Y_PRESSED) == true) { + opt_volume = 0; + } + if (pad.check_event(Gamepad::X_PRESSED) == true) { + opt_volume = 1; + pad.tone(2400, 0.2); + wait_ms(200); + pad.tone(2400, 0.2); + wait_ms(200); + pad.tone(2400, 0.2); + } + lcd.printString("X/Y = Volume",0,4); + if (opt_volume == 0) { + lcd.printString("Off",36,5); + } else { + lcd.printString("On",36,5); + } + +} + +// High Score State-------------------------------------------------------- +void high_scores_run() { + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + //printf("High Score State"); + lcd.clear(); + lcd.printString("High Scores",12,0); + lcd.refresh(); + wait_ms(1.0f/fps); + } +} + +// Main Menu -------------------------------------------------------------- int main() { init(); // initialise peripherals welcome(); // display welcome message @@ -76,7 +234,7 @@ } if (menu_option_pos == 3) { // printf("Options"); - options_run(); + opt.options_run(pad, lcd); } if (menu_option_pos == 4) { // printf("High Scores"); @@ -112,141 +270,3 @@ lcd.refresh(); wait(1.0); } - -// Game State-------------------------------------------------------------- -int donkeykong_x = 0; -int donkeykong_y = 32; -int donkey_kong_speed = 25; -void game_engine_run() { - wait_ms(250); - - while (pad.check_event(Gamepad::BACK_PRESSED) == false) { - //printf("Game State"); - lcd.clear(); - lcd.printString("Game",0,0); - donkeykong_x_movement(); - wait_ms(1.0f/fps); - } -} - -void donkeykong_x_movement() { - direction = pad.get_direction(); - if (direction == NE || direction == E || direction == SE) { - donkeykong_x = donkeykong_x + 1; - lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_walking_right_1); - wait_ms(donkey_kong_speed); - lcd.refresh(); - } - else if (direction == NW || direction == W || direction == SW) { - donkeykong_x = donkeykong_x - 1; - lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_walking_left_1); - wait_ms(donkey_kong_speed); - lcd.refresh(); - } - else { - lcd.drawSprite(donkeykong_x,donkeykong_y,16,16,(int *)game_dk_stationary); - lcd.refresh(); - } - if (donkeykong_x > 68) { - donkeykong_x = 68; - } - if (donkeykong_x < 0) { - donkeykong_x = 0; - } -} - - -// Controls State---------------------------------------------------------- -void controls_run() { - wait_ms(250); - - while (pad.check_event(Gamepad::BACK_PRESSED) == false) { - //printf("Control State"); - lcd.clear(); - lcd.printString("Controls",19,0); - lcd.refresh(); - wait_ms(1.0f/fps); - } -} - -// Instructions State------------------------------------------------------ -void instructions_run() { - wait_ms(250); - - while (pad.check_event(Gamepad::BACK_PRESSED) == false) { - //printf("Instructions State"); - lcd.clear(); - lcd.printString("Instructions",7,0); - lcd.refresh(); - wait_ms(1.0f/fps); - } -} - -// Options State----------------------------------------------------------- -float brightness = 0.5; -float contrast = 0.396; -int volume = 1; - -void options_run() { - wait_ms(250); - while (pad.check_event(Gamepad::BACK_PRESSED) == false) { - //printf("Options State"); - lcd.clear(); - lcd.printString("Options",21,0); - options_brightness(); - options_contrast(); - options_volume(); - lcd.refresh(); - wait_ms(1.0f/fps); - } -} - -void options_brightness() { - if (pad.check_event(Gamepad::B_PRESSED) == true) { - brightness = 0; - } - if (pad.check_event(Gamepad::A_PRESSED) == true) { - brightness = 0.5; - } - lcd.setBrightness(brightness); - lcd.printString("A/B = BackLite",0,2); -} - -void options_contrast() { - contrast = pad.read_pot(); - lcd.setContrast(contrast); - lcd.printString("Pot = Contrast",0,3); - printf("Contrast = %f", contrast); -} - -void options_volume() { - if (pad.check_event(Gamepad::Y_PRESSED) == true) { - volume = 0; - } - if (pad.check_event(Gamepad::X_PRESSED) == true) { - volume = 1; - pad.tone(2400, 0.2); - wait_ms(200); - pad.tone(2400, 0.2); - wait_ms(200); - pad.tone(2400, 0.2); - } - lcd.printString("X/Y = Volume",0,4); - if (volume == 0) { - lcd.printString("Off",36,5); - } else { - lcd.printString("On",36,5); - } - -} - -// High Score State-------------------------------------------------------- -void high_scores_run() { - while (pad.check_event(Gamepad::BACK_PRESSED) == false) { - //printf("High Score State"); - lcd.clear(); - lcd.printString("High Scores",12,0); - lcd.refresh(); - wait_ms(1.0f/fps); - } -} \ No newline at end of file