![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Owen Cavender 201159294
Dependencies: mbed
snake.cpp@1:897160a1a3ae, 2020-05-26 (annotated)
- Committer:
- el17oc
- Date:
- Tue May 26 12:17:59 2020 +0000
- Revision:
- 1:897160a1a3ae
- Child:
- 2:ffbfd3f53ee2
ELEC2645
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17oc | 1:897160a1a3ae | 1 | #include "snake.h" |
el17oc | 1:897160a1a3ae | 2 | |
el17oc | 1:897160a1a3ae | 3 | Snake::Snake() |
el17oc | 1:897160a1a3ae | 4 | { |
el17oc | 1:897160a1a3ae | 5 | |
el17oc | 1:897160a1a3ae | 6 | } |
el17oc | 1:897160a1a3ae | 7 | |
el17oc | 1:897160a1a3ae | 8 | Snake::~Snake() |
el17oc | 1:897160a1a3ae | 9 | { |
el17oc | 1:897160a1a3ae | 10 | |
el17oc | 1:897160a1a3ae | 11 | } |
el17oc | 1:897160a1a3ae | 12 | |
el17oc | 1:897160a1a3ae | 13 | |
el17oc | 1:897160a1a3ae | 14 | void Snake::init () |
el17oc | 1:897160a1a3ae | 15 | { |
el17oc | 1:897160a1a3ae | 16 | |
el17oc | 1:897160a1a3ae | 17 | //WIDTH =84 HEIGHT = 42 //snakebody[0] is initialised may have to initilaise the other 2 og snake parts |
el17oc | 1:897160a1a3ae | 18 | _length = 1; |
el17oc | 1:897160a1a3ae | 19 | _gameover = false; |
el17oc | 1:897160a1a3ae | 20 | _score = 0; |
el17oc | 1:897160a1a3ae | 21 | _direction = up; |
el17oc | 1:897160a1a3ae | 22 | |
el17oc | 1:897160a1a3ae | 23 | } |
el17oc | 1:897160a1a3ae | 24 | |
el17oc | 1:897160a1a3ae | 25 | |
el17oc | 1:897160a1a3ae | 26 | |
el17oc | 1:897160a1a3ae | 27 | int Snake::set_direction(Gamepad &pad) //int type as Directions is an enum |
el17oc | 1:897160a1a3ae | 28 | { |
el17oc | 1:897160a1a3ae | 29 | |
el17oc | 1:897160a1a3ae | 30 | Directions _direction; |
el17oc | 1:897160a1a3ae | 31 | |
el17oc | 1:897160a1a3ae | 32 | |
el17oc | 1:897160a1a3ae | 33 | if (pad.A_pressed()) { |
el17oc | 1:897160a1a3ae | 34 | |
el17oc | 1:897160a1a3ae | 35 | _direction = right; |
el17oc | 1:897160a1a3ae | 36 | |
el17oc | 1:897160a1a3ae | 37 | |
el17oc | 1:897160a1a3ae | 38 | } |
el17oc | 1:897160a1a3ae | 39 | |
el17oc | 1:897160a1a3ae | 40 | if (pad.B_pressed()) { |
el17oc | 1:897160a1a3ae | 41 | |
el17oc | 1:897160a1a3ae | 42 | _direction = left; //check these are orrecrt |
el17oc | 1:897160a1a3ae | 43 | |
el17oc | 1:897160a1a3ae | 44 | } |
el17oc | 1:897160a1a3ae | 45 | |
el17oc | 1:897160a1a3ae | 46 | if (pad.X_pressed()) { |
el17oc | 1:897160a1a3ae | 47 | |
el17oc | 1:897160a1a3ae | 48 | _direction = up; |
el17oc | 1:897160a1a3ae | 49 | |
el17oc | 1:897160a1a3ae | 50 | } |
el17oc | 1:897160a1a3ae | 51 | |
el17oc | 1:897160a1a3ae | 52 | if (pad.Y_pressed()) { |
el17oc | 1:897160a1a3ae | 53 | |
el17oc | 1:897160a1a3ae | 54 | |
el17oc | 1:897160a1a3ae | 55 | _direction = down; |
el17oc | 1:897160a1a3ae | 56 | |
el17oc | 1:897160a1a3ae | 57 | } |
el17oc | 1:897160a1a3ae | 58 | |
el17oc | 1:897160a1a3ae | 59 | else { |
el17oc | 1:897160a1a3ae | 60 | |
el17oc | 1:897160a1a3ae | 61 | _direction = _direction; |
el17oc | 1:897160a1a3ae | 62 | } |
el17oc | 1:897160a1a3ae | 63 | return _direction; |
el17oc | 1:897160a1a3ae | 64 | } |
el17oc | 1:897160a1a3ae | 65 | |
el17oc | 1:897160a1a3ae | 66 | |
el17oc | 1:897160a1a3ae | 67 | void Snake::move_and_draw_snake(N5110 &lcd) |
el17oc | 1:897160a1a3ae | 68 | { |
el17oc | 1:897160a1a3ae | 69 | Vector2D Snakehead = _engine.get_Snakehead(); //initialises Snakehead value |
el17oc | 1:897160a1a3ae | 70 | Vector2D *snakebody = new Vector2D [_length]; |
el17oc | 1:897160a1a3ae | 71 | |
el17oc | 1:897160a1a3ae | 72 | snakebody[0].x = Snakehead.x; |
el17oc | 1:897160a1a3ae | 73 | snakebody[0].y = Snakehead.y; |
el17oc | 1:897160a1a3ae | 74 | |
el17oc | 1:897160a1a3ae | 75 | while(1) { |
el17oc | 1:897160a1a3ae | 76 | if (_direction == up) { |
el17oc | 1:897160a1a3ae | 77 | Snakehead.y++; // alters Snakehead initial value |
el17oc | 1:897160a1a3ae | 78 | } |
el17oc | 1:897160a1a3ae | 79 | if (_direction == down) { //this part is needed to keep the snake running continually |
el17oc | 1:897160a1a3ae | 80 | Snakehead.y--; |
el17oc | 1:897160a1a3ae | 81 | } |
el17oc | 1:897160a1a3ae | 82 | if (_direction == left) { |
el17oc | 1:897160a1a3ae | 83 | Snakehead.x--; |
el17oc | 1:897160a1a3ae | 84 | } |
el17oc | 1:897160a1a3ae | 85 | if (_direction == right) { |
el17oc | 1:897160a1a3ae | 86 | Snakehead.x++; //updates the head position before rendering so when it draws i=1 the 0 position will be new |
el17oc | 1:897160a1a3ae | 87 | } |
el17oc | 1:897160a1a3ae | 88 | |
el17oc | 1:897160a1a3ae | 89 | for(int i=1; i<=_length; i++) { //0 being head of snake so snakepos[1]=snakepos[0] which is the head - moving up one place |
el17oc | 1:897160a1a3ae | 90 | snakebody[i].x = snakebody[i-1].x; |
el17oc | 1:897160a1a3ae | 91 | snakebody[i].y = snakebody[i-1].y; |
el17oc | 1:897160a1a3ae | 92 | lcd.setPixel(snakebody[i].x, snakebody[i].y, 1); |
el17oc | 1:897160a1a3ae | 93 | if(snakebody[i].x == Snakehead.x && snakebody[i].y == Snakehead.y) { //is snakebody[0] being plotted |
el17oc | 1:897160a1a3ae | 94 | _gameover = true; |
el17oc | 1:897160a1a3ae | 95 | |
el17oc | 1:897160a1a3ae | 96 | } |
el17oc | 1:897160a1a3ae | 97 | } |
el17oc | 1:897160a1a3ae | 98 | } |
el17oc | 1:897160a1a3ae | 99 | |
el17oc | 1:897160a1a3ae | 100 | |
el17oc | 1:897160a1a3ae | 101 | } |
el17oc | 1:897160a1a3ae | 102 | |
el17oc | 1:897160a1a3ae | 103 | |
el17oc | 1:897160a1a3ae | 104 | void Snake::gameover_true(N5110 &lcd) //taking action if crash has occured |
el17oc | 1:897160a1a3ae | 105 | { |
el17oc | 1:897160a1a3ae | 106 | if (_gameover == true) { |
el17oc | 1:897160a1a3ae | 107 | |
el17oc | 1:897160a1a3ae | 108 | lcd.clear(); |
el17oc | 1:897160a1a3ae | 109 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 110 | lcd.printString( " Game Over L ", 0, 2 ); |
el17oc | 1:897160a1a3ae | 111 | lcd.printString ("score: _score ",15, 15); //Need to add button to return to main screen / restart |
el17oc | 1:897160a1a3ae | 112 | |
el17oc | 1:897160a1a3ae | 113 | } |
el17oc | 1:897160a1a3ae | 114 | |
el17oc | 1:897160a1a3ae | 115 | else { |
el17oc | 1:897160a1a3ae | 116 | |
el17oc | 1:897160a1a3ae | 117 | |
el17oc | 1:897160a1a3ae | 118 | } |
el17oc | 1:897160a1a3ae | 119 | } |
el17oc | 1:897160a1a3ae | 120 | |
el17oc | 1:897160a1a3ae | 121 | |
el17oc | 1:897160a1a3ae | 122 | |
el17oc | 1:897160a1a3ae | 123 | void Snake::check_if_scored(N5110 &lcd, Gamepad &pad) |
el17oc | 1:897160a1a3ae | 124 | { |
el17oc | 1:897160a1a3ae | 125 | Vector2D Snakehead = _engine.get_Snakehead(); |
el17oc | 1:897160a1a3ae | 126 | Vector2D Applepos = _engine.get_Applepos(); //need to code clear apple and make sure apple isnt spawning every time |
el17oc | 1:897160a1a3ae | 127 | |
el17oc | 1:897160a1a3ae | 128 | if(Snakehead.x == Applepos.x && Snakehead.y == Applepos.y) { |
el17oc | 1:897160a1a3ae | 129 | |
el17oc | 1:897160a1a3ae | 130 | |
el17oc | 1:897160a1a3ae | 131 | _score++; |
el17oc | 1:897160a1a3ae | 132 | _length = _length++; |
el17oc | 1:897160a1a3ae | 133 | _engine.set_Applepos(lcd); //randomises new values for _apx,_apy and draws on lcd -- the position is not needed until we compare it to Snakehead |
el17oc | 1:897160a1a3ae | 134 | pad.tone(1500.0,0.5); //need to clear apple |
el17oc | 1:897160a1a3ae | 135 | pad.leds_on(); |
el17oc | 1:897160a1a3ae | 136 | wait(0.5); |
el17oc | 1:897160a1a3ae | 137 | pad.leds_off(); |
el17oc | 1:897160a1a3ae | 138 | |
el17oc | 1:897160a1a3ae | 139 | } |
el17oc | 1:897160a1a3ae | 140 | } |
el17oc | 1:897160a1a3ae | 141 | |
el17oc | 1:897160a1a3ae | 142 | void Snake::check_wall_collisions() // Vector2D *&snakebody hopefully this points to values stored in snakebody then can compare to snakehead |
el17oc | 1:897160a1a3ae | 143 | { |
el17oc | 1:897160a1a3ae | 144 | Vector2D Snakehead = _engine.get_Snakehead(); |
el17oc | 1:897160a1a3ae | 145 | |
el17oc | 1:897160a1a3ae | 146 | |
el17oc | 1:897160a1a3ae | 147 | if (Snakehead.x == 0 || Snakehead.x == 84 || Snakehead.y == 0 || Snakehead.y == 42) { //how do i access snakehead.headx |
el17oc | 1:897160a1a3ae | 148 | |
el17oc | 1:897160a1a3ae | 149 | _gameover = true; |
el17oc | 1:897160a1a3ae | 150 | } else { |
el17oc | 1:897160a1a3ae | 151 | _gameover = false; |
el17oc | 1:897160a1a3ae | 152 | |
el17oc | 1:897160a1a3ae | 153 | } |
el17oc | 1:897160a1a3ae | 154 | } |
el17oc | 1:897160a1a3ae | 155 | |
el17oc | 1:897160a1a3ae | 156 | |
el17oc | 1:897160a1a3ae | 157 |