Xiandi Bao
/
Idea
A shooting game
main.cpp@4:592da144458e, 2017-05-05 (annotated)
- Committer:
- el16x2b
- Date:
- Fri May 05 00:36:01 2017 +0000
- Revision:
- 4:592da144458e
- Parent:
- 2:cf1d42dbef67
A shooting game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el16x2b | 1:4eb47ae6b0d4 | 1 | ///////// pre-processor directives //////// |
el16x2b | 0:c129ba5610f8 | 2 | #include "mbed.h" |
el16x2b | 0:c129ba5610f8 | 3 | #include "N5110.h" |
el16x2b | 0:c129ba5610f8 | 4 | #include "Gamepad.h" |
el16x2b | 0:c129ba5610f8 | 5 | #include "ShootEngine.h" |
el16x2b | 0:c129ba5610f8 | 6 | |
el16x2b | 1:4eb47ae6b0d4 | 7 | |
el16x2b | 1:4eb47ae6b0d4 | 8 | /////////////// structs ///////////////// |
el16x2b | 0:c129ba5610f8 | 9 | struct UserInput { |
el16x2b | 0:c129ba5610f8 | 10 | Direction d; |
el16x2b | 0:c129ba5610f8 | 11 | float mag; |
el16x2b | 0:c129ba5610f8 | 12 | }; |
el16x2b | 0:c129ba5610f8 | 13 | |
el16x2b | 0:c129ba5610f8 | 14 | |
el16x2b | 1:4eb47ae6b0d4 | 15 | /////////////// objects /////////////// |
el16x2b | 0:c129ba5610f8 | 16 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el16x2b | 0:c129ba5610f8 | 17 | Gamepad pad; |
el16x2b | 0:c129ba5610f8 | 18 | ShootEngine shoot; |
el16x2b | 0:c129ba5610f8 | 19 | |
el16x2b | 1:4eb47ae6b0d4 | 20 | ///////////// prototypes /////////////// |
el16x2b | 0:c129ba5610f8 | 21 | void init(); |
el16x2b | 0:c129ba5610f8 | 22 | void update_game(UserInput input); |
el16x2b | 0:c129ba5610f8 | 23 | void render(); |
el16x2b | 0:c129ba5610f8 | 24 | void welcome(); |
el16x2b | 1:4eb47ae6b0d4 | 25 | void count(); |
el16x2b | 1:4eb47ae6b0d4 | 26 | ///////////// functions //////////////// |
el16x2b | 0:c129ba5610f8 | 27 | int main() |
el16x2b | 0:c129ba5610f8 | 28 | { |
el16x2b | 0:c129ba5610f8 | 29 | int fps = 8; // frames per second |
el16x2b | 0:c129ba5610f8 | 30 | |
el16x2b | 0:c129ba5610f8 | 31 | init(); |
el16x2b | 0:c129ba5610f8 | 32 | welcome(); |
el16x2b | 1:4eb47ae6b0d4 | 33 | lcd.clear(); |
el16x2b | 1:4eb47ae6b0d4 | 34 | count(); |
el16x2b | 0:c129ba5610f8 | 35 | |
el16x2b | 0:c129ba5610f8 | 36 | render(); // draw initial frame |
el16x2b | 0:c129ba5610f8 | 37 | wait(1.0f/fps); |
el16x2b | 0:c129ba5610f8 | 38 | |
el16x2b | 1:4eb47ae6b0d4 | 39 | //gameloop: receive, update and display |
el16x2b | 0:c129ba5610f8 | 40 | while (1) { |
el16x2b | 0:c129ba5610f8 | 41 | shoot.read_input(pad); |
el16x2b | 1:4eb47ae6b0d4 | 42 | shoot.update(pad,lcd); |
el16x2b | 0:c129ba5610f8 | 43 | render(); |
el16x2b | 0:c129ba5610f8 | 44 | wait(1.0f/fps); |
el16x2b | 1:4eb47ae6b0d4 | 45 | |
el16x2b | 0:c129ba5610f8 | 46 | } |
el16x2b | 0:c129ba5610f8 | 47 | } |
el16x2b | 0:c129ba5610f8 | 48 | |
el16x2b | 0:c129ba5610f8 | 49 | void init() |
el16x2b | 0:c129ba5610f8 | 50 | { |
el16x2b | 1:4eb47ae6b0d4 | 51 | //initialise LCD and Gamepad |
el16x2b | 0:c129ba5610f8 | 52 | lcd.init(); |
el16x2b | 0:c129ba5610f8 | 53 | pad.init(); |
el16x2b | 0:c129ba5610f8 | 54 | |
el16x2b | 0:c129ba5610f8 | 55 | // initialise the game |
el16x2b | 1:4eb47ae6b0d4 | 56 | shoot.init(pad); |
el16x2b | 0:c129ba5610f8 | 57 | |
el16x2b | 0:c129ba5610f8 | 58 | } |
el16x2b | 0:c129ba5610f8 | 59 | |
el16x2b | 0:c129ba5610f8 | 60 | void render() |
el16x2b | 0:c129ba5610f8 | 61 | { |
el16x2b | 1:4eb47ae6b0d4 | 62 | lcd.clear(); //clear the screen |
el16x2b | 1:4eb47ae6b0d4 | 63 | shoot.draw(lcd); // draw the component of the game |
el16x2b | 1:4eb47ae6b0d4 | 64 | lcd.refresh(); // display on the screen |
el16x2b | 0:c129ba5610f8 | 65 | } |
el16x2b | 0:c129ba5610f8 | 66 | |
el16x2b | 0:c129ba5610f8 | 67 | void welcome() { |
el16x2b | 0:c129ba5610f8 | 68 | |
el16x2b | 0:c129ba5610f8 | 69 | lcd.printString(" The snipper! ",0,1); |
el16x2b | 0:c129ba5610f8 | 70 | lcd.printString(" Press Start ",0,4); |
el16x2b | 0:c129ba5610f8 | 71 | lcd.refresh(); |
el16x2b | 0:c129ba5610f8 | 72 | |
el16x2b | 0:c129ba5610f8 | 73 | // wait flashing LEDs until start button is pressed |
el16x2b | 0:c129ba5610f8 | 74 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
el16x2b | 0:c129ba5610f8 | 75 | pad.leds_on(); |
el16x2b | 0:c129ba5610f8 | 76 | wait(0.1); |
el16x2b | 0:c129ba5610f8 | 77 | pad.leds_off(); |
el16x2b | 0:c129ba5610f8 | 78 | wait(0.1); |
el16x2b | 0:c129ba5610f8 | 79 | } |
el16x2b | 0:c129ba5610f8 | 80 | |
el16x2b | 0:c129ba5610f8 | 81 | } |
el16x2b | 1:4eb47ae6b0d4 | 82 | // 3 seconds counting down will display on the screen when 'start' is pressed |
el16x2b | 1:4eb47ae6b0d4 | 83 | void count() |
el16x2b | 1:4eb47ae6b0d4 | 84 | { |
el16x2b | 1:4eb47ae6b0d4 | 85 | lcd.printString(" &&&&& ",0,0); |
el16x2b | 1:4eb47ae6b0d4 | 86 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 87 | lcd.printString(" & ",0,1); |
el16x2b | 1:4eb47ae6b0d4 | 88 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 89 | lcd.printString(" &&&&& ",0,2); |
el16x2b | 1:4eb47ae6b0d4 | 90 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 91 | lcd.printString(" & ",0,3); |
el16x2b | 1:4eb47ae6b0d4 | 92 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 93 | lcd.printString(" &&&&& ",0,4); |
el16x2b | 1:4eb47ae6b0d4 | 94 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 95 | pad.tone(300.0,0.1); |
el16x2b | 1:4eb47ae6b0d4 | 96 | pad.leds_on(); |
el16x2b | 1:4eb47ae6b0d4 | 97 | wait (1.0); |
el16x2b | 1:4eb47ae6b0d4 | 98 | lcd.clear(); |
el16x2b | 1:4eb47ae6b0d4 | 99 | |
el16x2b | 1:4eb47ae6b0d4 | 100 | lcd.printString(" &&&&&& ",0,0); |
el16x2b | 1:4eb47ae6b0d4 | 101 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 102 | lcd.printString(" && ",0,1); |
el16x2b | 1:4eb47ae6b0d4 | 103 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 104 | lcd.printString(" &&&&&& ",0,2); |
el16x2b | 1:4eb47ae6b0d4 | 105 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 106 | lcd.printString(" && ",0,3); |
el16x2b | 1:4eb47ae6b0d4 | 107 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 108 | lcd.printString(" &&&&&& ",0,4); |
el16x2b | 1:4eb47ae6b0d4 | 109 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 110 | pad.tone(300.0,0.1); |
el16x2b | 1:4eb47ae6b0d4 | 111 | pad.leds_on(); |
el16x2b | 1:4eb47ae6b0d4 | 112 | wait (1.0); |
el16x2b | 1:4eb47ae6b0d4 | 113 | lcd.clear(); |
el16x2b | 1:4eb47ae6b0d4 | 114 | |
el16x2b | 2:cf1d42dbef67 | 115 | lcd.printString(" &&& ",0,0); |
el16x2b | 1:4eb47ae6b0d4 | 116 | lcd.refresh(); |
el16x2b | 2:cf1d42dbef67 | 117 | lcd.printString(" &&& ",0,1); |
el16x2b | 1:4eb47ae6b0d4 | 118 | lcd.refresh(); |
el16x2b | 2:cf1d42dbef67 | 119 | lcd.printString(" &&& ",0,2); |
el16x2b | 1:4eb47ae6b0d4 | 120 | lcd.refresh(); |
el16x2b | 2:cf1d42dbef67 | 121 | lcd.printString(" &&& ",0,3); |
el16x2b | 1:4eb47ae6b0d4 | 122 | lcd.refresh(); |
el16x2b | 2:cf1d42dbef67 | 123 | lcd.printString(" &&& ",0,4); |
el16x2b | 2:cf1d42dbef67 | 124 | lcd.refresh(); |
el16x2b | 1:4eb47ae6b0d4 | 125 | pad.tone(300.0,0.1); |
el16x2b | 1:4eb47ae6b0d4 | 126 | pad.leds_on(); |
el16x2b | 1:4eb47ae6b0d4 | 127 | wait (1.0); |
el16x2b | 1:4eb47ae6b0d4 | 128 | lcd.clear(); |
el16x2b | 1:4eb47ae6b0d4 | 129 | } |