ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp@5:d716013c6a18, 2020-05-27 (annotated)
- Committer:
- el17oc
- Date:
- Wed May 27 10:27:34 2020 +0000
- Revision:
- 5:d716013c6a18
- Parent:
- 3:d17d712c2e45
- Child:
- 6:bf90044188d0
not final
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 | 5:d716013c6a18 | 14 | void Snake::init (GameEngine &engine) |
el17oc | 1:897160a1a3ae | 15 | { |
el17oc | 1:897160a1a3ae | 16 | |
el17oc | 2:44e4a6ecdbef | 17 | //WIDTH =84 HEIGHT = 42 //snakebody[0] is initalised may have to initilaise the other 2 og snake parts |
el17oc | 5:d716013c6a18 | 18 | Vector2D SnakeHead = engine.get_Snakehead(); |
el17oc | 5:d716013c6a18 | 19 | |
el17oc | 3:d17d712c2e45 | 20 | _length = 2; // how do i initalise |
el17oc | 1:897160a1a3ae | 21 | _gameover = false; |
el17oc | 1:897160a1a3ae | 22 | _score = 0; |
el17oc | 1:897160a1a3ae | 23 | _direction = up; |
el17oc | 5:d716013c6a18 | 24 | Vector2D S0 = SnakeHead; |
el17oc | 5:d716013c6a18 | 25 | |
el17oc | 5:d716013c6a18 | 26 | // Vector2D S1 = engine.get_oldSnakehead(); |
el17oc | 5:d716013c6a18 | 27 | // Vector2D &Snakehead = {42, 24}; |
el17oc | 5:d716013c6a18 | 28 | // Vector2D &oldSnakehead = {42, 23}; |
el17oc | 1:897160a1a3ae | 29 | } |
el17oc | 1:897160a1a3ae | 30 | |
el17oc | 1:897160a1a3ae | 31 | |
el17oc | 1:897160a1a3ae | 32 | |
el17oc | 1:897160a1a3ae | 33 | |
el17oc | 1:897160a1a3ae | 34 | void Snake::gameover_true(N5110 &lcd) //taking action if crash has occured |
el17oc | 1:897160a1a3ae | 35 | { |
el17oc | 1:897160a1a3ae | 36 | if (_gameover == true) { |
el17oc | 1:897160a1a3ae | 37 | |
el17oc | 1:897160a1a3ae | 38 | lcd.clear(); |
el17oc | 1:897160a1a3ae | 39 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 40 | lcd.printString( " Game Over L ", 0, 2 ); |
el17oc | 2:44e4a6ecdbef | 41 | lcd.printString ("score: _score ",15, 15); //Need to add button to return to main screen / restart //NEED to return score without ruining structure of lcd.PrintString( "string", x, y) |
el17oc | 1:897160a1a3ae | 42 | |
el17oc | 1:897160a1a3ae | 43 | } |
el17oc | 1:897160a1a3ae | 44 | |
el17oc | 1:897160a1a3ae | 45 | else { |
el17oc | 1:897160a1a3ae | 46 | |
el17oc | 1:897160a1a3ae | 47 | |
el17oc | 1:897160a1a3ae | 48 | } |
el17oc | 1:897160a1a3ae | 49 | } |
el17oc | 1:897160a1a3ae | 50 | |
el17oc | 1:897160a1a3ae | 51 | |
el17oc | 1:897160a1a3ae | 52 | |
el17oc | 2:44e4a6ecdbef | 53 | |
el17oc | 5:d716013c6a18 | 54 | void Snake::check_scored(N5110 &lcd, Gamepad &pad, GameEngine &engine) |
el17oc | 1:897160a1a3ae | 55 | { |
el17oc | 5:d716013c6a18 | 56 | Vector2D Applepos = engine.get_Applepos(); |
el17oc | 5:d716013c6a18 | 57 | //need to code clear apple and make sure apple isnt spawning every time |
el17oc | 1:897160a1a3ae | 58 | |
el17oc | 5:d716013c6a18 | 59 | if(S0.x == Applepos.x && S0.y == Applepos.y) { |
el17oc | 1:897160a1a3ae | 60 | |
el17oc | 3:d17d712c2e45 | 61 | lcd.setPixel(Applepos.x, Applepos.y, 0); // Plem:: Wanted to make sure that this didnt clear the pixel of the snake as it passes through it |
el17oc | 2:44e4a6ecdbef | 62 | _score++; //by waiting unil the snake has passed through before clearing the old apple however the new apple spawn needs to be ASAP after collection |
el17oc | 1:897160a1a3ae | 63 | _length = _length++; |
el17oc | 5:d716013c6a18 | 64 | engine.set_Applepos(lcd); //randomises new values for _apx,_apy and draws on lcd -- draws on next loop-- the position is not needed until we compare it to Snakehead |
el17oc | 1:897160a1a3ae | 65 | pad.tone(1500.0,0.5); //need to clear apple |
el17oc | 1:897160a1a3ae | 66 | pad.leds_on(); |
el17oc | 1:897160a1a3ae | 67 | wait(0.5); |
el17oc | 1:897160a1a3ae | 68 | pad.leds_off(); |
el17oc | 1:897160a1a3ae | 69 | |
el17oc | 1:897160a1a3ae | 70 | } |
el17oc | 5:d716013c6a18 | 71 | lcd.setPixel(Applepos.x, Applepos.y,1); |
el17oc | 5:d716013c6a18 | 72 | } |
el17oc | 5:d716013c6a18 | 73 | |
el17oc | 5:d716013c6a18 | 74 | |
el17oc | 5:d716013c6a18 | 75 | |
el17oc | 5:d716013c6a18 | 76 | int Snake::set_direction(Gamepad &pad) //int type as Directions is an enum |
el17oc | 5:d716013c6a18 | 77 | { |
el17oc | 5:d716013c6a18 | 78 | |
el17oc | 5:d716013c6a18 | 79 | Directions _direction; |
el17oc | 5:d716013c6a18 | 80 | |
el17oc | 5:d716013c6a18 | 81 | |
el17oc | 5:d716013c6a18 | 82 | if (pad.A_pressed()) { |
el17oc | 5:d716013c6a18 | 83 | _direction = right; |
el17oc | 5:d716013c6a18 | 84 | } |
el17oc | 5:d716013c6a18 | 85 | |
el17oc | 5:d716013c6a18 | 86 | if (pad.B_pressed()) { |
el17oc | 5:d716013c6a18 | 87 | _direction = left; //check these are orrecrt |
el17oc | 5:d716013c6a18 | 88 | } |
el17oc | 5:d716013c6a18 | 89 | |
el17oc | 5:d716013c6a18 | 90 | if (pad.X_pressed()) { |
el17oc | 5:d716013c6a18 | 91 | _direction = up; |
el17oc | 5:d716013c6a18 | 92 | } |
el17oc | 5:d716013c6a18 | 93 | if (pad.Y_pressed()) { |
el17oc | 5:d716013c6a18 | 94 | _direction = down; |
el17oc | 5:d716013c6a18 | 95 | } else { |
el17oc | 5:d716013c6a18 | 96 | _direction = _direction; |
el17oc | 5:d716013c6a18 | 97 | } |
el17oc | 5:d716013c6a18 | 98 | return _direction; |
el17oc | 1:897160a1a3ae | 99 | } |
el17oc | 1:897160a1a3ae | 100 | |
el17oc | 5:d716013c6a18 | 101 | |
el17oc | 5:d716013c6a18 | 102 | |
el17oc | 5:d716013c6a18 | 103 | void Snake::move_snakebody(GameEngine &engine, N5110 &lcd) |
el17oc | 5:d716013c6a18 | 104 | { |
el17oc | 5:d716013c6a18 | 105 | lcd.refresh(); |
el17oc | 5:d716013c6a18 | 106 | |
el17oc | 5:d716013c6a18 | 107 | Vector2D *_snakebody = new Vector2D [_length]; |
el17oc | 5:d716013c6a18 | 108 | _snakebody[0].x = S0.x; //assign _sb0[0] before changing value |
el17oc | 5:d716013c6a18 | 109 | _snakebody[0].y = S0.y; //engine.get_oldSnakehead; // _engine.get_oldSnakehead(); |
el17oc | 5:d716013c6a18 | 110 | |
el17oc | 5:d716013c6a18 | 111 | |
el17oc | 5:d716013c6a18 | 112 | while(1) { |
el17oc | 5:d716013c6a18 | 113 | if (_direction == up) { // /-/ |
el17oc | 5:d716013c6a18 | 114 | S0.y++; ///// alters Snakehead initial value |
el17oc | 5:d716013c6a18 | 115 | // Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function |
el17oc | 5:d716013c6a18 | 116 | } |
el17oc | 5:d716013c6a18 | 117 | if (_direction == down) { // Direction should remain after button is pressed - should not just change the snakehead coordinate once |
el17oc | 5:d716013c6a18 | 118 | S0.y--; |
el17oc | 5:d716013c6a18 | 119 | } |
el17oc | 5:d716013c6a18 | 120 | if (_direction == left) { |
el17oc | 5:d716013c6a18 | 121 | S0.x--; |
el17oc | 5:d716013c6a18 | 122 | } |
el17oc | 5:d716013c6a18 | 123 | |
el17oc | 5:d716013c6a18 | 124 | if (_direction == right) { |
el17oc | 5:d716013c6a18 | 125 | S0.x++; |
el17oc | 5:d716013c6a18 | 126 | } |
el17oc | 5:d716013c6a18 | 127 | lcd.setPixel(S0.x, S0.y, 1); |
el17oc | 5:d716013c6a18 | 128 | //updates the head position before rendering so when it draws i=1 the 0 position will be new |
el17oc | 5:d716013c6a18 | 129 | 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 | 5:d716013c6a18 | 130 | _snakebody[i].x = _snakebody[i-1].x; |
el17oc | 5:d716013c6a18 | 131 | _snakebody[i].y = _snakebody[i-1].y; //plotting of snake |
el17oc | 5:d716013c6a18 | 132 | |
el17oc | 5:d716013c6a18 | 133 | lcd.setPixel(_snakebody->x, _snakebody->y, 1); //pointer used - need to point to x and y |
el17oc | 5:d716013c6a18 | 134 | //Snake needs to follow itself - low numbers lead the way |
el17oc | 5:d716013c6a18 | 135 | |
el17oc | 5:d716013c6a18 | 136 | if((_snakebody[i].x == _snakebody[0].x) && (_snakebody[i].x == _snakebody[0].x)) { // Unsure how to initially define the snake with (3 bits, when score =0) starting position *without* redeclaring Vector2D *snakebody = new Vector2D [_length]; |
el17oc | 5:d716013c6a18 | 137 | _gameover = true; |
el17oc | 5:d716013c6a18 | 138 | } |
el17oc | 5:d716013c6a18 | 139 | } |
el17oc | 5:d716013c6a18 | 140 | |
el17oc | 5:d716013c6a18 | 141 | } |
el17oc | 5:d716013c6a18 | 142 | } |
el17oc | 1:897160a1a3ae | 143 | void Snake::check_wall_collisions() // Vector2D *&snakebody hopefully this points to values stored in snakebody then can compare to snakehead |
el17oc | 1:897160a1a3ae | 144 | { |
el17oc | 1:897160a1a3ae | 145 | |
el17oc | 5:d716013c6a18 | 146 | if (S0.x == 0 || S0.x == 84 || S0.y == 0 || S0.y == 42) { //how do i access snakehead.headx |
el17oc | 1:897160a1a3ae | 147 | |
el17oc | 1:897160a1a3ae | 148 | _gameover = true; |
el17oc | 1:897160a1a3ae | 149 | } else { |
el17oc | 5:d716013c6a18 | 150 | |
el17oc | 5:d716013c6a18 | 151 | _gameover = _gameover; |
el17oc | 1:897160a1a3ae | 152 | } |
el17oc | 5:d716013c6a18 | 153 | } |