Yang Zhenye 201199680
Dependencies: mbed
Diff: RoonyEngine/RoonyEngine.cpp
- Revision:
- 3:30c81344beff
- Parent:
- 2:baca89f466bc
diff -r baca89f466bc -r 30c81344beff RoonyEngine/RoonyEngine.cpp --- a/RoonyEngine/RoonyEngine.cpp Thu Apr 30 07:08:09 2020 +0000 +++ b/RoonyEngine/RoonyEngine.cpp Fri May 08 09:09:49 2020 +0000 @@ -0,0 +1,115 @@ +#include "RonnyEngine.h" + + +RonnyEngine::RonnyEngine(); +RonnyEngine::~RonnyEngine(); +void RoonyEngine::init() { + reset(); + _start_flag = true; + } + +void RoonyEngine::reset() { + _player_x=38; + _player_y=5; + _collisicon_flag = false; + _player_score = 0; + _x = WIDTH/2 - _size/2; + _y = HEIGHT/2 - _size/2; + } + +void RonnyEngine::check_reset(N5110 &lcd, Gamepad &gamepad) { + if(get_reset_flag()){ + gamepad.leds_on(); + lcd.clear(); + for (int i = 0; i<60; i++) { + lcd.printString("lose the game", 0, 2); + lcd.printString("sccore:", 0, 4); + lcd,refresh(); + wait(0.00001); + sprintf(buffer,"%2d",_player_score); + lcd.printString(buffer,16,4); + reset(); + } + } + +void RoonyEngine::check_start(N5110 &lcd, Gamepad &gamepad) { + if(_start_flag == true) { + check_reset(lcd, gamepad);} + _start_flag = get_start_flag(); + } + +void RoonyEngine::check_collision(Gamepad &gamepad) { + if ((_player_x == _x() - 2) && (_player_y == _y() - 5)) { + _collision_flag = true; + _player_score++; + } + +void RoonyEngine::get_score() { + return _player_score; + } + +void RoonyEngine::get_sprite() { + lcd.drawLine(unsigned int const x0, + unsigned int const y0, + unsigned int const x1, + unsigned int const y1, + FullBlack); + lcd.drawSprite(_player_x, _player_y, 5, 10, (int *)get_form(ronnybody)); + lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); + } + +void RoonyEngine::update_lcd(N5110 &lcd) { + get_sprite(); + sprintf(buffer,"%2d",_player_score); + lcd.printString("score",0,0); + lcd.printString(buffer,30,0); + } + +void RoonyEngine::get_ronny_x(Gamepad &gamepad) { + _speed=speed; + if (joy_x < float(-0.5){ + _speed=-0.5; + _player_x=38+_speed; + else if (x<=0){ + _speed = 0 + } + } + if (joy_x > float(0.5){ + _speed=0.5; + _player_x=38+_speed; + else if (x>=80){ + _speed = 0 + } + } + +bool RoonyEngine::get_reset_flag() { + return _reset_flag; + } + +bool RoonyEngine::get_start_flag() { + return _start_flag; + } + +void RoonyEngine::run_engine(N5110 &lcd, Gamepad &gamepad) { + init(); + reset(); + check_reset(N5110 &lcd, Gamepad &gamepad); + check_start(N5110 &lcd, Gamepad &gamepad); + check_collision(Gamepad &gamepad); + _player_score=get_score(); + get_sprite(); + update_lcd(N5110 &lcd); + get_reset_flag(); + get_start_flag(); + get_ronny_x(Gamepad &gamepad); + } + + + + + + + + + + \ No newline at end of file