Yang Zhenye 201199680
Dependencies: mbed
RoonyEngine/RoonyEngine.cpp@3:30c81344beff, 2020-05-08 (annotated)
- Committer:
- yangzhenye
- Date:
- Fri May 08 09:09:49 2020 +0000
- Revision:
- 3:30c81344beff
- Parent:
- 2:baca89f466bc
improve project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yangzhenye | 3:30c81344beff | 1 | #include "RonnyEngine.h" |
yangzhenye | 3:30c81344beff | 2 | |
yangzhenye | 3:30c81344beff | 3 | |
yangzhenye | 3:30c81344beff | 4 | RonnyEngine::RonnyEngine(); |
yangzhenye | 3:30c81344beff | 5 | RonnyEngine::~RonnyEngine(); |
yangzhenye | 3:30c81344beff | 6 | void RoonyEngine::init() { |
yangzhenye | 3:30c81344beff | 7 | reset(); |
yangzhenye | 3:30c81344beff | 8 | _start_flag = true; |
yangzhenye | 3:30c81344beff | 9 | } |
yangzhenye | 3:30c81344beff | 10 | |
yangzhenye | 3:30c81344beff | 11 | void RoonyEngine::reset() { |
yangzhenye | 3:30c81344beff | 12 | _player_x=38; |
yangzhenye | 3:30c81344beff | 13 | _player_y=5; |
yangzhenye | 3:30c81344beff | 14 | _collisicon_flag = false; |
yangzhenye | 3:30c81344beff | 15 | _player_score = 0; |
yangzhenye | 3:30c81344beff | 16 | _x = WIDTH/2 - _size/2; |
yangzhenye | 3:30c81344beff | 17 | _y = HEIGHT/2 - _size/2; |
yangzhenye | 3:30c81344beff | 18 | } |
yangzhenye | 3:30c81344beff | 19 | |
yangzhenye | 3:30c81344beff | 20 | void RonnyEngine::check_reset(N5110 &lcd, Gamepad &gamepad) { |
yangzhenye | 3:30c81344beff | 21 | if(get_reset_flag()){ |
yangzhenye | 3:30c81344beff | 22 | gamepad.leds_on(); |
yangzhenye | 3:30c81344beff | 23 | lcd.clear(); |
yangzhenye | 3:30c81344beff | 24 | for (int i = 0; i<60; i++) { |
yangzhenye | 3:30c81344beff | 25 | lcd.printString("lose the game", 0, 2); |
yangzhenye | 3:30c81344beff | 26 | lcd.printString("sccore:", 0, 4); |
yangzhenye | 3:30c81344beff | 27 | lcd,refresh(); |
yangzhenye | 3:30c81344beff | 28 | wait(0.00001); |
yangzhenye | 3:30c81344beff | 29 | sprintf(buffer,"%2d",_player_score); |
yangzhenye | 3:30c81344beff | 30 | lcd.printString(buffer,16,4); |
yangzhenye | 3:30c81344beff | 31 | reset(); |
yangzhenye | 3:30c81344beff | 32 | } |
yangzhenye | 3:30c81344beff | 33 | } |
yangzhenye | 3:30c81344beff | 34 | |
yangzhenye | 3:30c81344beff | 35 | void RoonyEngine::check_start(N5110 &lcd, Gamepad &gamepad) { |
yangzhenye | 3:30c81344beff | 36 | if(_start_flag == true) { |
yangzhenye | 3:30c81344beff | 37 | check_reset(lcd, gamepad);} |
yangzhenye | 3:30c81344beff | 38 | _start_flag = get_start_flag(); |
yangzhenye | 3:30c81344beff | 39 | } |
yangzhenye | 3:30c81344beff | 40 | |
yangzhenye | 3:30c81344beff | 41 | void RoonyEngine::check_collision(Gamepad &gamepad) { |
yangzhenye | 3:30c81344beff | 42 | if ((_player_x == _x() - 2) && (_player_y == _y() - 5)) { |
yangzhenye | 3:30c81344beff | 43 | _collision_flag = true; |
yangzhenye | 3:30c81344beff | 44 | _player_score++; |
yangzhenye | 3:30c81344beff | 45 | } |
yangzhenye | 3:30c81344beff | 46 | |
yangzhenye | 3:30c81344beff | 47 | void RoonyEngine::get_score() { |
yangzhenye | 3:30c81344beff | 48 | return _player_score; |
yangzhenye | 3:30c81344beff | 49 | } |
yangzhenye | 3:30c81344beff | 50 | |
yangzhenye | 3:30c81344beff | 51 | void RoonyEngine::get_sprite() { |
yangzhenye | 3:30c81344beff | 52 | lcd.drawLine(unsigned int const x0, |
yangzhenye | 3:30c81344beff | 53 | unsigned int const y0, |
yangzhenye | 3:30c81344beff | 54 | unsigned int const x1, |
yangzhenye | 3:30c81344beff | 55 | unsigned int const y1, |
yangzhenye | 3:30c81344beff | 56 | FullBlack); |
yangzhenye | 3:30c81344beff | 57 | lcd.drawSprite(_player_x, _player_y, 5, 10, (int *)get_form(ronnybody)); |
yangzhenye | 3:30c81344beff | 58 | lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
yangzhenye | 3:30c81344beff | 59 | } |
yangzhenye | 3:30c81344beff | 60 | |
yangzhenye | 3:30c81344beff | 61 | void RoonyEngine::update_lcd(N5110 &lcd) { |
yangzhenye | 3:30c81344beff | 62 | get_sprite(); |
yangzhenye | 3:30c81344beff | 63 | sprintf(buffer,"%2d",_player_score); |
yangzhenye | 3:30c81344beff | 64 | lcd.printString("score",0,0); |
yangzhenye | 3:30c81344beff | 65 | lcd.printString(buffer,30,0); |
yangzhenye | 3:30c81344beff | 66 | } |
yangzhenye | 3:30c81344beff | 67 | |
yangzhenye | 3:30c81344beff | 68 | void RoonyEngine::get_ronny_x(Gamepad &gamepad) { |
yangzhenye | 3:30c81344beff | 69 | _speed=speed; |
yangzhenye | 3:30c81344beff | 70 | if (joy_x < float(-0.5){ |
yangzhenye | 3:30c81344beff | 71 | _speed=-0.5; |
yangzhenye | 3:30c81344beff | 72 | _player_x=38+_speed; |
yangzhenye | 3:30c81344beff | 73 | else if (x<=0){ |
yangzhenye | 3:30c81344beff | 74 | _speed = 0 |
yangzhenye | 3:30c81344beff | 75 | } |
yangzhenye | 3:30c81344beff | 76 | } |
yangzhenye | 3:30c81344beff | 77 | if (joy_x > float(0.5){ |
yangzhenye | 3:30c81344beff | 78 | _speed=0.5; |
yangzhenye | 3:30c81344beff | 79 | _player_x=38+_speed; |
yangzhenye | 3:30c81344beff | 80 | else if (x>=80){ |
yangzhenye | 3:30c81344beff | 81 | _speed = 0 |
yangzhenye | 3:30c81344beff | 82 | } |
yangzhenye | 3:30c81344beff | 83 | } |
yangzhenye | 3:30c81344beff | 84 | |
yangzhenye | 3:30c81344beff | 85 | bool RoonyEngine::get_reset_flag() { |
yangzhenye | 3:30c81344beff | 86 | return _reset_flag; |
yangzhenye | 3:30c81344beff | 87 | } |
yangzhenye | 3:30c81344beff | 88 | |
yangzhenye | 3:30c81344beff | 89 | bool RoonyEngine::get_start_flag() { |
yangzhenye | 3:30c81344beff | 90 | return _start_flag; |
yangzhenye | 3:30c81344beff | 91 | } |
yangzhenye | 3:30c81344beff | 92 | |
yangzhenye | 3:30c81344beff | 93 | void RoonyEngine::run_engine(N5110 &lcd, Gamepad &gamepad) { |
yangzhenye | 3:30c81344beff | 94 | init(); |
yangzhenye | 3:30c81344beff | 95 | reset(); |
yangzhenye | 3:30c81344beff | 96 | check_reset(N5110 &lcd, Gamepad &gamepad); |
yangzhenye | 3:30c81344beff | 97 | check_start(N5110 &lcd, Gamepad &gamepad); |
yangzhenye | 3:30c81344beff | 98 | check_collision(Gamepad &gamepad); |
yangzhenye | 3:30c81344beff | 99 | _player_score=get_score(); |
yangzhenye | 3:30c81344beff | 100 | get_sprite(); |
yangzhenye | 3:30c81344beff | 101 | update_lcd(N5110 &lcd); |
yangzhenye | 3:30c81344beff | 102 | get_reset_flag(); |
yangzhenye | 3:30c81344beff | 103 | get_start_flag(); |
yangzhenye | 3:30c81344beff | 104 | get_ronny_x(Gamepad &gamepad); |
yangzhenye | 3:30c81344beff | 105 | } |
yangzhenye | 3:30c81344beff | 106 | |
yangzhenye | 3:30c81344beff | 107 | |
yangzhenye | 3:30c81344beff | 108 | |
yangzhenye | 3:30c81344beff | 109 | |
yangzhenye | 3:30c81344beff | 110 | |
yangzhenye | 3:30c81344beff | 111 | |
yangzhenye | 3:30c81344beff | 112 | |
yangzhenye | 3:30c81344beff | 113 | |
yangzhenye | 3:30c81344beff | 114 | |
yangzhenye | 3:30c81344beff | 115 |