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.
MazeEngine/MazeEngine.cpp@21:bcc84d5cb068, 2018-05-08 (annotated)
- Committer:
- ahmedhedait
- Date:
- Tue May 08 12:20:04 2018 +0000
- Revision:
- 21:bcc84d5cb068
- Parent:
- 20:041affa5e242
- Child:
- 22:745b4d352183
implemented the code in which when the ball reach the goal, bravo is printed and maze disappears.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ahmedhedait | 17:68d4b4095d80 | 1 | #include "MazeEngine.h" |
ahmedhedait | 17:68d4b4095d80 | 2 | // nothing doing in the constructor and destructor |
ahmedhedait | 17:68d4b4095d80 | 3 | MazeEngine::MazeEngine() |
ahmedhedait | 17:68d4b4095d80 | 4 | { |
ahmedhedait | 17:68d4b4095d80 | 5 | |
ahmedhedait | 17:68d4b4095d80 | 6 | } |
ahmedhedait | 17:68d4b4095d80 | 7 | |
ahmedhedait | 17:68d4b4095d80 | 8 | MazeEngine::~MazeEngine() |
ahmedhedait | 17:68d4b4095d80 | 9 | { |
ahmedhedait | 17:68d4b4095d80 | 10 | |
ahmedhedait | 19:c6ebd1394bda | 11 | } |
ahmedhedait | 19:c6ebd1394bda | 12 | |
ahmedhedait | 19:c6ebd1394bda | 13 | void MazeEngine::init() |
ahmedhedait | 19:c6ebd1394bda | 14 | { |
ahmedhedait | 20:041affa5e242 | 15 | _ball.init(); |
ahmedhedait | 19:c6ebd1394bda | 16 | } |
ahmedhedait | 19:c6ebd1394bda | 17 | |
ahmedhedait | 19:c6ebd1394bda | 18 | void MazeEngine::read_input(Gamepad &pad) |
ahmedhedait | 19:c6ebd1394bda | 19 | { |
ahmedhedait | 20:041affa5e242 | 20 | _dir = pad.get_direction(); |
ahmedhedait | 20:041affa5e242 | 21 | } |
ahmedhedait | 20:041affa5e242 | 22 | |
ahmedhedait | 20:041affa5e242 | 23 | void MazeEngine::update(Gamepad &pad) |
ahmedhedait | 20:041affa5e242 | 24 | { |
ahmedhedait | 20:041affa5e242 | 25 | _ball.update(_dir); |
ahmedhedait | 21:bcc84d5cb068 | 26 | check_goal(pad); |
ahmedhedait | 19:c6ebd1394bda | 27 | } |
ahmedhedait | 19:c6ebd1394bda | 28 | |
ahmedhedait | 19:c6ebd1394bda | 29 | void MazeEngine::draw(N5110 &lcd) |
ahmedhedait | 19:c6ebd1394bda | 30 | { |
ahmedhedait | 19:c6ebd1394bda | 31 | // draw the elements in the LCD buffer |
ahmedhedait | 19:c6ebd1394bda | 32 | // maze |
ahmedhedait | 19:c6ebd1394bda | 33 | _maze.draw(lcd); |
ahmedhedait | 20:041affa5e242 | 34 | |
ahmedhedait | 20:041affa5e242 | 35 | // ball |
ahmedhedait | 20:041affa5e242 | 36 | _ball.draw(lcd); |
ahmedhedait | 21:bcc84d5cb068 | 37 | |
ahmedhedait | 21:bcc84d5cb068 | 38 | // HERE IS A SIMPLE CODE THAT WHEN THE BALL PASS THROUGH THE OPENING THEN THE SCREEN SHOULD BE CLEARED IN WHICH BRAVO IS PRINTED TO |
ahmedhedait | 21:bcc84d5cb068 | 39 | // TELL THE USER THE GAME IS FINISHED. |
ahmedhedait | 21:bcc84d5cb068 | 40 | if (ball_pos.x > 83 & ball_pos.y == 27) { |
ahmedhedait | 21:bcc84d5cb068 | 41 | print_win(lcd); |
ahmedhedait | 21:bcc84d5cb068 | 42 | } |
ahmedhedait | 21:bcc84d5cb068 | 43 | } |
ahmedhedait | 21:bcc84d5cb068 | 44 | |
ahmedhedait | 21:bcc84d5cb068 | 45 | void MazeEngine::check_goal(Gamepad &pad) |
ahmedhedait | 21:bcc84d5cb068 | 46 | { |
ahmedhedait | 21:bcc84d5cb068 | 47 | ball_pos = _ball.get_pos(); |
ahmedhedait | 21:bcc84d5cb068 | 48 | // WHEN THE BALL REACHES THE Y-AXIS NEEDED WHICH IS 27, THEN THE JOYSTICK FREELY MOVE THE BALL RIGHT THROUGH THE OPENING OF THE SMAZE WALL, |
ahmedhedait | 21:bcc84d5cb068 | 49 | // HOWEVER, IF THE BALL IS NOT EQUAL TO THE Y-AXIS NEEDED, THEN THE BALL MUST BE RESTRICTED TO MOVING SO THAT IT DOES NOT PASS THE WALLS. |
ahmedhedait | 21:bcc84d5cb068 | 50 | if (ball_pos.y == 27) { |
ahmedhedait | 21:bcc84d5cb068 | 51 | if (ball_pos.x > WIDTH) { |
ahmedhedait | 21:bcc84d5cb068 | 52 | ball_pos.x = WIDTH; |
ahmedhedait | 21:bcc84d5cb068 | 53 | } |
ahmedhedait | 21:bcc84d5cb068 | 54 | } else if (ball_pos.x > 80) { |
ahmedhedait | 21:bcc84d5cb068 | 55 | ball_pos.x = 80; |
ahmedhedait | 21:bcc84d5cb068 | 56 | } |
ahmedhedait | 21:bcc84d5cb068 | 57 | } |
ahmedhedait | 21:bcc84d5cb068 | 58 | |
ahmedhedait | 21:bcc84d5cb068 | 59 | void MazeEngine::print_win(N5110 &lcd) |
ahmedhedait | 21:bcc84d5cb068 | 60 | { |
ahmedhedait | 21:bcc84d5cb068 | 61 | lcd.clear(); |
ahmedhedait | 21:bcc84d5cb068 | 62 | lcd.printString(" Bravo! ",12,2); |
ahmedhedait | 17:68d4b4095d80 | 63 | } |