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