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
Eng.cpp@8:0b9a824c75fe, 2020-05-17 (annotated)
- Committer:
- el18jgb
- Date:
- Sun May 17 19:44:26 2020 +0000
- Revision:
- 8:0b9a824c75fe
- Parent:
- 7:04a7826ff7e4
- Child:
- 9:2836fc3d9ede
nothing isnt working, only having problems on accuracy of buttons. goof point to come back to*
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el18jgb | 5:c37f4ed2cad3 | 1 | |
el18jgb | 5:c37f4ed2cad3 | 2 | #include "Eng.h" |
el18jgb | 4:6f898b000797 | 3 | Eng::Eng() |
el18jgb | 4:6f898b000797 | 4 | { |
el18jgb | 4:6f898b000797 | 5 | |
el18jgb | 4:6f898b000797 | 6 | } |
el18jgb | 4:6f898b000797 | 7 | |
el18jgb | 4:6f898b000797 | 8 | Eng::~Eng() |
el18jgb | 4:6f898b000797 | 9 | { |
el18jgb | 4:6f898b000797 | 10 | |
el18jgb | 4:6f898b000797 | 11 | } |
el18jgb | 4:6f898b000797 | 12 | // objects |
el18jgb | 5:c37f4ed2cad3 | 13 | //Gamepad pad; |
el18jgb | 5:c37f4ed2cad3 | 14 | //N5110 lcd; |
el18jgb | 5:c37f4ed2cad3 | 15 | Aim aim; |
el18jgb | 4:6f898b000797 | 16 | Heston heston; |
el18jgb | 7:04a7826ff7e4 | 17 | Pup pup; |
el18jgb | 4:6f898b000797 | 18 | |
el18jgb | 4:6f898b000797 | 19 | |
el18jgb | 5:c37f4ed2cad3 | 20 | void Eng::init() |
el18jgb | 5:c37f4ed2cad3 | 21 | { |
el18jgb | 5:c37f4ed2cad3 | 22 | |
el18jgb | 5:c37f4ed2cad3 | 23 | aim.init(); |
el18jgb | 5:c37f4ed2cad3 | 24 | heston.init(); |
el18jgb | 7:04a7826ff7e4 | 25 | pup.init(); |
el18jgb | 7:04a7826ff7e4 | 26 | pupon = 0; |
el18jgb | 7:04a7826ff7e4 | 27 | _tok = 0; |
el18jgb | 7:04a7826ff7e4 | 28 | flag = true; |
el18jgb | 4:6f898b000797 | 29 | |
el18jgb | 5:c37f4ed2cad3 | 30 | |
el18jgb | 5:c37f4ed2cad3 | 31 | } |
el18jgb | 5:c37f4ed2cad3 | 32 | |
el18jgb | 5:c37f4ed2cad3 | 33 | //void Eng::read_input(Gamepad &pad); |
el18jgb | 8:0b9a824c75fe | 34 | void Eng::update(Gamepad &pad, int fire, N5110 &lcd) |
el18jgb | 4:6f898b000797 | 35 | { |
el18jgb | 8:0b9a824c75fe | 36 | int shot = fire; |
el18jgb | 8:0b9a824c75fe | 37 | if (shot == 1){ |
el18jgb | 6:d560ecc21ae6 | 38 | aim.draw(lcd, fire); |
el18jgb | 6:d560ecc21ae6 | 39 | lcd.refresh(); |
el18jgb | 8:0b9a824c75fe | 40 | check_hit(pad); |
el18jgb | 6:d560ecc21ae6 | 41 | } |
el18jgb | 7:04a7826ff7e4 | 42 | |
el18jgb | 7:04a7826ff7e4 | 43 | int c_score = heston.checkscore(); |
el18jgb | 7:04a7826ff7e4 | 44 | if (c_score%5 == 0 && flag == false){ |
el18jgb | 7:04a7826ff7e4 | 45 | flag = true; |
el18jgb | 7:04a7826ff7e4 | 46 | powerup(lcd, pad); |
el18jgb | 7:04a7826ff7e4 | 47 | } |
el18jgb | 7:04a7826ff7e4 | 48 | if (c_score%5 == 1){ |
el18jgb | 7:04a7826ff7e4 | 49 | flag = false; |
el18jgb | 7:04a7826ff7e4 | 50 | } |
el18jgb | 7:04a7826ff7e4 | 51 | |
el18jgb | 8:0b9a824c75fe | 52 | int strikes = heston.checkstrike(); |
el18jgb | 8:0b9a824c75fe | 53 | |
el18jgb | 7:04a7826ff7e4 | 54 | |
el18jgb | 5:c37f4ed2cad3 | 55 | heston.update(pad); |
el18jgb | 5:c37f4ed2cad3 | 56 | aim.update(pad); |
el18jgb | 5:c37f4ed2cad3 | 57 | |
el18jgb | 5:c37f4ed2cad3 | 58 | |
el18jgb | 5:c37f4ed2cad3 | 59 | } |
el18jgb | 5:c37f4ed2cad3 | 60 | |
el18jgb | 5:c37f4ed2cad3 | 61 | void Eng::draw(N5110 &lcd) |
el18jgb | 5:c37f4ed2cad3 | 62 | { |
el18jgb | 5:c37f4ed2cad3 | 63 | heston.draw(lcd); |
el18jgb | 6:d560ecc21ae6 | 64 | aim.draw(lcd, 0); |
el18jgb | 7:04a7826ff7e4 | 65 | if (pupon == 1){ |
el18jgb | 7:04a7826ff7e4 | 66 | pup.draw(lcd, 0); |
el18jgb | 7:04a7826ff7e4 | 67 | } |
el18jgb | 8:0b9a824c75fe | 68 | print_score(lcd); |
el18jgb | 4:6f898b000797 | 69 | } |
el18jgb | 5:c37f4ed2cad3 | 70 | |
el18jgb | 5:c37f4ed2cad3 | 71 | void Eng::check_hit(Gamepad &pad) |
el18jgb | 5:c37f4ed2cad3 | 72 | { |
el18jgb | 5:c37f4ed2cad3 | 73 | Vector2D aim_pos = aim.get_pos(); |
el18jgb | 5:c37f4ed2cad3 | 74 | Vector2D h1_pos = heston.get_pos(); |
el18jgb | 7:04a7826ff7e4 | 75 | Vector2D pup_pos = pup.get_pos(); |
el18jgb | 5:c37f4ed2cad3 | 76 | |
el18jgb | 5:c37f4ed2cad3 | 77 | if ( |
el18jgb | 5:c37f4ed2cad3 | 78 | (aim_pos.y >= h1_pos.y) && //top |
el18jgb | 5:c37f4ed2cad3 | 79 | (aim_pos.y <= h1_pos.y + 12) && //bottom |
el18jgb | 5:c37f4ed2cad3 | 80 | (aim_pos.x >= h1_pos.x) && //left |
el18jgb | 5:c37f4ed2cad3 | 81 | (aim_pos.x <= h1_pos.x + 11) //right |
el18jgb | 5:c37f4ed2cad3 | 82 | ) { |
el18jgb | 5:c37f4ed2cad3 | 83 | heston.hit(pad); |
el18jgb | 7:04a7826ff7e4 | 84 | } |
el18jgb | 8:0b9a824c75fe | 85 | else if ( |
el18jgb | 7:04a7826ff7e4 | 86 | (pupon == 1) && |
el18jgb | 7:04a7826ff7e4 | 87 | (aim_pos.y >= pup_pos.y) && //top |
el18jgb | 7:04a7826ff7e4 | 88 | (aim_pos.y <= pup_pos.y + 8) && //bottom |
el18jgb | 7:04a7826ff7e4 | 89 | (aim_pos.x >= pup_pos.x) && //left |
el18jgb | 7:04a7826ff7e4 | 90 | (aim_pos.x <= pup_pos.x + 8) //right |
el18jgb | 7:04a7826ff7e4 | 91 | ) { |
el18jgb | 7:04a7826ff7e4 | 92 | pupon = 0; |
el18jgb | 7:04a7826ff7e4 | 93 | heston.set_speed(0); |
el18jgb | 7:04a7826ff7e4 | 94 | pad.led(2,1); |
el18jgb | 7:04a7826ff7e4 | 95 | pad.led(5,1); |
el18jgb | 7:04a7826ff7e4 | 96 | } |
el18jgb | 7:04a7826ff7e4 | 97 | else { |
el18jgb | 7:04a7826ff7e4 | 98 | heston.miss(pad); |
el18jgb | 7:04a7826ff7e4 | 99 | } |
el18jgb | 8:0b9a824c75fe | 100 | wait(0.4); |
el18jgb | 8:0b9a824c75fe | 101 | pad.leds_off(); |
el18jgb | 5:c37f4ed2cad3 | 102 | } |
el18jgb | 5:c37f4ed2cad3 | 103 | |
el18jgb | 7:04a7826ff7e4 | 104 | void Eng::powerup(N5110 &lcd, Gamepad &pad) |
el18jgb | 7:04a7826ff7e4 | 105 | { |
el18jgb | 7:04a7826ff7e4 | 106 | Vector2D aim_pos = aim.get_pos(); |
el18jgb | 7:04a7826ff7e4 | 107 | int x = aim_pos.x; |
el18jgb | 7:04a7826ff7e4 | 108 | int y = aim_pos.y; |
el18jgb | 7:04a7826ff7e4 | 109 | pup.position(x, y); |
el18jgb | 7:04a7826ff7e4 | 110 | pupon = 1; |
el18jgb | 7:04a7826ff7e4 | 111 | |
el18jgb | 7:04a7826ff7e4 | 112 | |
el18jgb | 5:c37f4ed2cad3 | 113 | } |
el18jgb | 7:04a7826ff7e4 | 114 | |
el18jgb | 7:04a7826ff7e4 | 115 | void Eng::tik() |
el18jgb | 7:04a7826ff7e4 | 116 | { |
el18jgb | 7:04a7826ff7e4 | 117 | _tok = _tok + 1; |
el18jgb | 7:04a7826ff7e4 | 118 | int rem = _tok%50; |
el18jgb | 7:04a7826ff7e4 | 119 | if (rem == 0){ |
el18jgb | 7:04a7826ff7e4 | 120 | heston.set_speed(1); |
el18jgb | 7:04a7826ff7e4 | 121 | } |
el18jgb | 7:04a7826ff7e4 | 122 | } |
el18jgb | 8:0b9a824c75fe | 123 | |
el18jgb | 8:0b9a824c75fe | 124 | void Eng::print_score(N5110 &lcd) |
el18jgb | 8:0b9a824c75fe | 125 | { |
el18jgb | 8:0b9a824c75fe | 126 | // get scores from paddles |
el18jgb | 8:0b9a824c75fe | 127 | int g_score = heston.checkstrike(); |
el18jgb | 8:0b9a824c75fe | 128 | |
el18jgb | 8:0b9a824c75fe | 129 | // print to LCD i |
el18jgb | 8:0b9a824c75fe | 130 | char buffer1[14]; |
el18jgb | 8:0b9a824c75fe | 131 | sprintf(buffer1,"%2d",g_score); |
el18jgb | 8:0b9a824c75fe | 132 | lcd.printString(buffer1,1,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
el18jgb | 8:0b9a824c75fe | 133 | } |