Yang Zhenye 201199680
Dependencies: mbed
RooneyEngine/RooneyEngine.cpp@4:eb8dca1dc341, 2020-05-13 (annotated)
- Committer:
- yangzhenye
- Date:
- Wed May 13 14:55:07 2020 +0000
- Revision:
- 4:eb8dca1dc341
- Parent:
- RoonyEngine/RoonyEngine.cpp@3:30c81344beff
- Child:
- 5:fcad75e9b9e1
advance
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yangzhenye | 4:eb8dca1dc341 | 1 | #include "RonneyEngine.h" |
yangzhenye | 3:30c81344beff | 2 | |
yangzhenye | 3:30c81344beff | 3 | |
yangzhenye | 4:eb8dca1dc341 | 4 | RooneyEngine::RooneyEngine(); |
yangzhenye | 4:eb8dca1dc341 | 5 | RooneyEngine::~RooneyEngine(); |
yangzhenye | 4:eb8dca1dc341 | 6 | void RooneyEngine::init() { |
yangzhenye | 3:30c81344beff | 7 | reset(); |
yangzhenye | 3:30c81344beff | 8 | _start_flag = true; |
yangzhenye | 3:30c81344beff | 9 | } |
yangzhenye | 3:30c81344beff | 10 | |
yangzhenye | 4:eb8dca1dc341 | 11 | void RooneyEngine::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 | 4:eb8dca1dc341 | 20 | void RonneyEngine::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 | 4:eb8dca1dc341 | 35 | void RooneyEngine::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 | 4:eb8dca1dc341 | 41 | void RooneyEngine::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 | 4:eb8dca1dc341 | 47 | void RooneyEngine::get_score() { |
yangzhenye | 3:30c81344beff | 48 | return _player_score; |
yangzhenye | 3:30c81344beff | 49 | } |
yangzhenye | 3:30c81344beff | 50 | |
yangzhenye | 4:eb8dca1dc341 | 51 | void RooneyEngine::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 | 4:eb8dca1dc341 | 61 | void RooneyEngine::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 | 4:eb8dca1dc341 | 68 | void RooneyEngine::get_ronney_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 | 4:eb8dca1dc341 | 85 | bool RooneyEngine::get_reset_flag() { |
yangzhenye | 3:30c81344beff | 86 | return _reset_flag; |
yangzhenye | 3:30c81344beff | 87 | } |
yangzhenye | 3:30c81344beff | 88 | |
yangzhenye | 4:eb8dca1dc341 | 89 | bool RooneyEngine::get_start_flag() { |
yangzhenye | 3:30c81344beff | 90 | return _start_flag; |
yangzhenye | 3:30c81344beff | 91 | } |
yangzhenye | 3:30c81344beff | 92 | |
yangzhenye | 4:eb8dca1dc341 | 93 | void RooneyEngine::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 |