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.
Dependencies: mbed
main.cpp@0:8fb740fa6356, 2019-05-11 (annotated)
- Committer:
- el17ph
- Date:
- Sat May 11 14:58:43 2019 +0000
- Revision:
- 0:8fb740fa6356
- Child:
- 1:679d7ada8de7
last
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17ph | 0:8fb740fa6356 | 1 | #include "mbed.h" |
el17ph | 0:8fb740fa6356 | 2 | #include "N5110.h" |
el17ph | 0:8fb740fa6356 | 3 | #include "Gamepad.h" |
el17ph | 0:8fb740fa6356 | 4 | #include "MainTank.h" |
el17ph | 0:8fb740fa6356 | 5 | #include "TankEngine.h" |
el17ph | 0:8fb740fa6356 | 6 | |
el17ph | 0:8fb740fa6356 | 7 | /* |
el17ph | 0:8fb740fa6356 | 8 | ELEC2645 Embedded Systems Project |
el17ph | 0:8fb740fa6356 | 9 | School of Electronic & Electrical Engineering |
el17ph | 0:8fb740fa6356 | 10 | University of Leeds |
el17ph | 0:8fb740fa6356 | 11 | Name:Pengxi Huang |
el17ph | 0:8fb740fa6356 | 12 | Username:Pengxi Huang |
el17ph | 0:8fb740fa6356 | 13 | Student ID Number:201199000 |
el17ph | 0:8fb740fa6356 | 14 | Date:11.05.2019 |
el17ph | 0:8fb740fa6356 | 15 | */ |
el17ph | 0:8fb740fa6356 | 16 | |
el17ph | 0:8fb740fa6356 | 17 | struct UserInput { |
el17ph | 0:8fb740fa6356 | 18 | Direction d; |
el17ph | 0:8fb740fa6356 | 19 | float mag; |
el17ph | 0:8fb740fa6356 | 20 | }; |
el17ph | 0:8fb740fa6356 | 21 | |
el17ph | 0:8fb740fa6356 | 22 | void init(); |
el17ph | 0:8fb740fa6356 | 23 | void update_game(UserInput input); |
el17ph | 0:8fb740fa6356 | 24 | void render(); |
el17ph | 0:8fb740fa6356 | 25 | void welcome(); |
el17ph | 0:8fb740fa6356 | 26 | void end_game(int score); |
el17ph | 0:8fb740fa6356 | 27 | void print_level(); |
el17ph | 0:8fb740fa6356 | 28 | int quite(); |
el17ph | 0:8fb740fa6356 | 29 | |
el17ph | 0:8fb740fa6356 | 30 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17ph | 0:8fb740fa6356 | 31 | Gamepad pad; |
el17ph | 0:8fb740fa6356 | 32 | TankEngine Tank; |
el17ph | 0:8fb740fa6356 | 33 | //main function |
el17ph | 0:8fb740fa6356 | 34 | int main() |
el17ph | 0:8fb740fa6356 | 35 | { |
el17ph | 0:8fb740fa6356 | 36 | int fps = 6;//frame per second |
el17ph | 0:8fb740fa6356 | 37 | int life = 0; |
el17ph | 0:8fb740fa6356 | 38 | int score = 0; |
el17ph | 0:8fb740fa6356 | 39 | wait(1.0f/fps); |
el17ph | 0:8fb740fa6356 | 40 | |
el17ph | 0:8fb740fa6356 | 41 | //game loop |
el17ph | 0:8fb740fa6356 | 42 | while(1){ |
el17ph | 0:8fb740fa6356 | 43 | init();//init everthing |
el17ph | 0:8fb740fa6356 | 44 | welcome();//print the starting menu |
el17ph | 0:8fb740fa6356 | 45 | render();//draw the inital image |
el17ph | 0:8fb740fa6356 | 46 | while(1) { |
el17ph | 0:8fb740fa6356 | 47 | Tank.read_input(pad); |
el17ph | 0:8fb740fa6356 | 48 | Tank.update(pad); |
el17ph | 0:8fb740fa6356 | 49 | render(); |
el17ph | 0:8fb740fa6356 | 50 | life = Tank.get_life(); |
el17ph | 0:8fb740fa6356 | 51 | score = Tank.hello_score(); |
el17ph | 0:8fb740fa6356 | 52 | |
el17ph | 0:8fb740fa6356 | 53 | wait(1.0f/fps); |
el17ph | 0:8fb740fa6356 | 54 | if (quite() == 1)//quite to starting menu if back been pressed |
el17ph | 0:8fb740fa6356 | 55 | { |
el17ph | 0:8fb740fa6356 | 56 | break; |
el17ph | 0:8fb740fa6356 | 57 | } |
el17ph | 0:8fb740fa6356 | 58 | if (life == 0)//if lose print end menu and start again |
el17ph | 0:8fb740fa6356 | 59 | { |
el17ph | 0:8fb740fa6356 | 60 | init(); |
el17ph | 0:8fb740fa6356 | 61 | end_game(score); |
el17ph | 0:8fb740fa6356 | 62 | wait(3); |
el17ph | 0:8fb740fa6356 | 63 | break; |
el17ph | 0:8fb740fa6356 | 64 | } |
el17ph | 0:8fb740fa6356 | 65 | } |
el17ph | 0:8fb740fa6356 | 66 | } |
el17ph | 0:8fb740fa6356 | 67 | } |
el17ph | 0:8fb740fa6356 | 68 | |
el17ph | 0:8fb740fa6356 | 69 | //init everthing that needs in the game |
el17ph | 0:8fb740fa6356 | 70 | void init() |
el17ph | 0:8fb740fa6356 | 71 | { |
el17ph | 0:8fb740fa6356 | 72 | //initialise LCD and Gamepad |
el17ph | 0:8fb740fa6356 | 73 | lcd.init(); |
el17ph | 0:8fb740fa6356 | 74 | pad.init(); |
el17ph | 0:8fb740fa6356 | 75 | lcd.setContrast(0.5); |
el17ph | 0:8fb740fa6356 | 76 | |
el17ph | 0:8fb740fa6356 | 77 | // initialise the tank to start the game |
el17ph | 0:8fb740fa6356 | 78 | Tank.init(); |
el17ph | 0:8fb740fa6356 | 79 | |
el17ph | 0:8fb740fa6356 | 80 | } |
el17ph | 0:8fb740fa6356 | 81 | |
el17ph | 0:8fb740fa6356 | 82 | //update the tank iamge in the lcd |
el17ph | 0:8fb740fa6356 | 83 | void render() |
el17ph | 0:8fb740fa6356 | 84 | { |
el17ph | 0:8fb740fa6356 | 85 | // clear screen, re-draw and refresh |
el17ph | 0:8fb740fa6356 | 86 | lcd.clear(); |
el17ph | 0:8fb740fa6356 | 87 | Tank.Draw(lcd); |
el17ph | 0:8fb740fa6356 | 88 | lcd.refresh(); |
el17ph | 0:8fb740fa6356 | 89 | } |
el17ph | 0:8fb740fa6356 | 90 | |
el17ph | 0:8fb740fa6356 | 91 | //start menu with game name |
el17ph | 0:8fb740fa6356 | 92 | void welcome() |
el17ph | 0:8fb740fa6356 | 93 | { |
el17ph | 0:8fb740fa6356 | 94 | lcd.printString("Escape rocket ",0,1); |
el17ph | 0:8fb740fa6356 | 95 | lcd.printString(" Press Start ",0,4); |
el17ph | 0:8fb740fa6356 | 96 | lcd.refresh(); |
el17ph | 0:8fb740fa6356 | 97 | |
el17ph | 0:8fb740fa6356 | 98 | // wait flashing LEDs until start button is pressed |
el17ph | 0:8fb740fa6356 | 99 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
el17ph | 0:8fb740fa6356 | 100 | pad.leds_on(); |
el17ph | 0:8fb740fa6356 | 101 | wait(0.1); |
el17ph | 0:8fb740fa6356 | 102 | pad.leds_off(); |
el17ph | 0:8fb740fa6356 | 103 | wait(0.1); |
el17ph | 0:8fb740fa6356 | 104 | } |
el17ph | 0:8fb740fa6356 | 105 | } |
el17ph | 0:8fb740fa6356 | 106 | |
el17ph | 0:8fb740fa6356 | 107 | // signal of quiting to start menu whenever the back is pressed |
el17ph | 0:8fb740fa6356 | 108 | int quite() |
el17ph | 0:8fb740fa6356 | 109 | { |
el17ph | 0:8fb740fa6356 | 110 | int quite = 0; |
el17ph | 0:8fb740fa6356 | 111 | //vheck the status od back button |
el17ph | 0:8fb740fa6356 | 112 | while ( pad.check_event(Gamepad::BACK_PRESSED) == true) { |
el17ph | 0:8fb740fa6356 | 113 | quite = 1; |
el17ph | 0:8fb740fa6356 | 114 | } |
el17ph | 0:8fb740fa6356 | 115 | return quite; |
el17ph | 0:8fb740fa6356 | 116 | } |
el17ph | 0:8fb740fa6356 | 117 | |
el17ph | 0:8fb740fa6356 | 118 | // end menu when palyer lost all life in the game |
el17ph | 0:8fb740fa6356 | 119 | void end_game(int score) |
el17ph | 0:8fb740fa6356 | 120 | { |
el17ph | 0:8fb740fa6356 | 121 | char buffer1[13]; |
el17ph | 0:8fb740fa6356 | 122 | sprintf(buffer1,"%2d",score); |
el17ph | 0:8fb740fa6356 | 123 | lcd.printString(" Good Game ",0,1); |
el17ph | 0:8fb740fa6356 | 124 | lcd.printString(" Final score is ",0,3); |
el17ph | 0:8fb740fa6356 | 125 | //print its final score |
el17ph | 0:8fb740fa6356 | 126 | lcd.printString(buffer1,35,4); |
el17ph | 0:8fb740fa6356 | 127 | lcd.refresh(); |
el17ph | 0:8fb740fa6356 | 128 | } |
el17ph | 0:8fb740fa6356 | 129 |