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
Diff: main.cpp
- Revision:
- 2:16ae940b0aee
- Parent:
- 1:ded7ddd826ce
- Child:
- 3:28a3c118e071
--- a/main.cpp Mon Mar 11 20:27:12 2019 +0000 +++ b/main.cpp Wed Mar 13 15:10:36 2019 +0000 @@ -8,13 +8,13 @@ #include "N5110.h" #include "Gamepad.h" +#include "Skateboarder.h" #include <cmath> N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); -Gamepad gamepad; +Gamepad gamepad; +Skateboarder skater; - -// Sprites for Skateboarder moving right, moving left, standing left and standing right. These will be included in a Skateboarder class next. int skate_right[17][12] = { { 0,0,0,0,1,1,1,0,0,0,0,0 }, @@ -106,75 +106,60 @@ }; -// intialise game function. Process input and refresh LCD function will be implemented next along with the classes. + void init_game(); +void process_inputs(); +void update_lcd(int x, int y, Sprite_value sprite); -// main function. Note: code has not been properly formatted yet - the purpose is to get simple game mechanics to work. int main(){ init_game(); - - //intialise variables for the game loop. These will be implemented more concisely in functions and classes next. int moving_counter = 0; - int direction_state = 0; // integer to keep track of where the skater is facing. 0 for last moving direction was right, 1 if last moving direction was left. - int x; // x position of the skater - int y; // y position of the skater - int jump_counter = 0; // counter for jumping + int x; + int y; + int jump_counter = 0; + Skate_Direction direction; + Sprite_value sprite; while(1){ lcd.clear(); - lcd.drawLine(5,40,80,40,FILL_BLACK); // line for staker to move along - Vector2D coord = gamepad.get_mapped_coord(); // get mapped coordinates of joystick + Vector2D coord = gamepad.get_mapped_coord(); - // first we manipulate the y postion of the skater. - //check if innterupt button A has occured. This means a jump (translation in y position) must take place. - if( gamepad.check_event(Gamepad::A_PRESSED) ){ - jump_counter = 40; - } - // if the jump counter is non zero (e.g. if an interupt has occured) deincriment it every main loop iteration until it is 0 again. - // This means that initally y postion is increased by a lot, and every loop iteration the y postion falls until it is back to its default state. This is a jump of the skater. - if(jump_counter !=0){ - jump_counter--; - } - y = 23 - 0.5*jump_counter; // Using 0.5*jump_counter to scale the fall time. 23 is the y postion when the skater is on the line. - - // now we manipulate the x postion of the skater + skater.set_y_position( gamepad.check_event(Gamepad::A_PRESSED), jump_counter ); + y = skater.get_y_position(); + jump_counter = skater.get_jump_counter(); - //if the joystick is pushed in the horizontal right or left direction, increment or deincriment the moving counter respectivly, and add a scaled version of this onto the x postion. - //set the direction_state to 0 or 1 to indicate that the last moving postion was right or left respectivly. - //print the correct sprite depending on the postion of the joystick ( ie left, right or centered). - //first two conditions are for joystick moving right or left, the second two are for joystick centered with pervious postion being left or right. - if(coord.x > float(0.1)){ - moving_counter++; - x = 40 + 0.4*moving_counter; - direction_state = 0; - lcd.drawSprite(x,y,17,12,(int *)skate_right); - } else if(coord.x < float(-0.1)) { - moving_counter--; - x = 40 + 0.4*moving_counter; - direction_state = 1; - lcd.drawSprite(x,y,17,12,(int *)skate_left); - } else if(direction_state==1) { - x = 40 + 0.4*moving_counter; - lcd.drawSprite(x,y,17,12,(int *)skate_standing_left); - } else { - x = 40 + 0.4*moving_counter; - lcd.drawSprite(x,y,17,12,(int *)skate_standing_right); - } + skater.set_x_position( coord.x, moving_counter, direction ); + x = skater.get_x_position(); + moving_counter = skater.get_moving_counter(); + sprite = skater.get_sprite(); + + update_lcd(x, y, sprite); + lcd.refresh(); - wait(0.01); } } - void init_game() { +void init_game() { + gamepad.init(); + lcd.init(); + lcd.setContrast(0.5); + lcd.normalMode(); + lcd.setBrightness(0.5); + } - gamepad.init();// initalise gamepad - lcd.init(); // initialise LCD - lcd.setContrast(0.5); // set contrast of LCD - lcd.normalMode(); // use normal colour mode - lcd.setBrightness(0.5); //set brightness of LCD +void update_lcd(int x, int y, Sprite_value sprite) { + if(sprite == Skate_right){ + lcd.drawSprite(x,y,17,12,(int *)skate_right); + } else if(sprite == Skate_left){ + lcd.drawSprite(x,y,17,12,(int *)skate_left); + } else if(sprite == Stand_left){ + lcd.drawSprite(x,y,17,12,(int *)skate_standing_left); + } else if (sprite == Stand_right){ + lcd.drawSprite(x,y,17,12,(int *)skate_standing_right); } - - + lcd.drawLine(5,40,80,40,FILL_BLACK); + } + \ No newline at end of file