Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Committer:
el17oc
Date:
Tue May 26 18:35:44 2020 +0000
Revision:
2:44e4a6ecdbef
Parent:
1:897160a1a3ae
Child:
3:d17d712c2e45
ELEC2645

Who changed what in which revision?

UserRevisionLine numberNew 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 2:44e4a6ecdbef 17 //WIDTH =84 HEIGHT = 42 //snakebody[0] is initalised may have to initilaise the other 2 og snake parts
el17oc 2:44e4a6ecdbef 18 _length = 2; // ROB how do i initalise
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 _direction = right;
el17oc 1:897160a1a3ae 35 }
el17oc 1:897160a1a3ae 36
el17oc 1:897160a1a3ae 37 if (pad.B_pressed()) {
el17oc 1:897160a1a3ae 38 _direction = left; //check these are orrecrt
el17oc 1:897160a1a3ae 39 }
el17oc 1:897160a1a3ae 40
el17oc 1:897160a1a3ae 41 if (pad.X_pressed()) {
el17oc 1:897160a1a3ae 42 _direction = up;
el17oc 1:897160a1a3ae 43 }
el17oc 1:897160a1a3ae 44 if (pad.Y_pressed()) {
el17oc 1:897160a1a3ae 45 _direction = down;
el17oc 1:897160a1a3ae 46 }
el17oc 1:897160a1a3ae 47 else {
el17oc 1:897160a1a3ae 48 _direction = _direction;
el17oc 1:897160a1a3ae 49 }
el17oc 1:897160a1a3ae 50 return _direction;
el17oc 1:897160a1a3ae 51 }
el17oc 1:897160a1a3ae 52
el17oc 1:897160a1a3ae 53
el17oc 2:44e4a6ecdbef 54 void Snake::move_and_draw_snake(Gamepad &pad, N5110 &lcd)
el17oc 1:897160a1a3ae 55 {
el17oc 2:44e4a6ecdbef 56 Vector2D Snakehead = _engine.get_Snakehead();
el17oc 2:44e4a6ecdbef 57 Vector2D oldSnakehead = _engine.get_oldSnakehead();
el17oc 2:44e4a6ecdbef 58
el17oc 2:44e4a6ecdbef 59 Vector2D *_snakebody = new Vector2D [_length]; // ROB im unsure that this code would work- creates dynamic array - each bit having 2 parts (x,y) - number of bits of snakebody = length
el17oc 1:897160a1a3ae 60
el17oc 2:44e4a6ecdbef 61 _snakebody[0].x = Snakehead.x; // snakebody[0] = Snakehead
el17oc 2:44e4a6ecdbef 62 _snakebody[0].y = Snakehead.y;
el17oc 2:44e4a6ecdbef 63 _snakebody[1].x = oldSnakehead.x;
el17oc 2:44e4a6ecdbef 64 _snakebody[1].y = oldSnakehead.y;
el17oc 1:897160a1a3ae 65
el17oc 1:897160a1a3ae 66 while(1) {
el17oc 2:44e4a6ecdbef 67 if (_direction == up) { // /-/
el17oc 2:44e4a6ecdbef 68 Snakehead.y++; ///// alters Snakehead initial value
el17oc 2:44e4a6ecdbef 69 oldSnakehead.y = (Snakehead.y - 1);
el17oc 2:44e4a6ecdbef 70 oldSnakehead.x = (Snakehead.x); // Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function
el17oc 1:897160a1a3ae 71 }
el17oc 2:44e4a6ecdbef 72 if (_direction == down) { // Direction should remain after button is pressed - should not just change the snakehead coordinate once
el17oc 1:897160a1a3ae 73 Snakehead.y--;
el17oc 2:44e4a6ecdbef 74 oldSnakehead.y = (Snakehead.y + 1);
el17oc 2:44e4a6ecdbef 75 oldSnakehead.x = (Snakehead.x); //
el17oc 1:897160a1a3ae 76 }
el17oc 1:897160a1a3ae 77 if (_direction == left) {
el17oc 1:897160a1a3ae 78 Snakehead.x--;
el17oc 2:44e4a6ecdbef 79 oldSnakehead.x = (Snakehead.x + 1);
el17oc 2:44e4a6ecdbef 80 oldSnakehead.y = (Snakehead.y);
el17oc 1:897160a1a3ae 81 }
el17oc 1:897160a1a3ae 82 if (_direction == right) {
el17oc 2:44e4a6ecdbef 83 Snakehead.x++;
el17oc 2:44e4a6ecdbef 84 oldSnakehead.x = (Snakehead.x - 1); //updates the head position before rendering so when it draws i=1 the 0 position will be new
el17oc 2:44e4a6ecdbef 85 oldSnakehead.y = (Snakehead.y);
el17oc 1:897160a1a3ae 86 }
el17oc 1:897160a1a3ae 87
el17oc 2:44e4a6ecdbef 88
el17oc 2:44e4a6ecdbef 89 for(int i=2; i<=_length; i++) { //0 being head of snake so snakepos[1]=snakepos[0] which is the head - moving up one place
el17oc 2:44e4a6ecdbef 90 _snakebody[i].x = _snakebody[i-1].x; //Snake needs to follow itself - low numbers lead the way
el17oc 2:44e4a6ecdbef 91 _snakebody[i].y = _snakebody[i-1].y;
el17oc 2:44e4a6ecdbef 92 if(_snakebody[i].x == Snakehead.x && _snakebody[i].y == Snakehead.y) { // ROB Unsure how to initially define the snake with (3 bits, when score =0) starting position *without* redeclaring Vector2D *snakebody = new Vector2D [_length];
el17oc 1:897160a1a3ae 93 _gameover = true;
el17oc 1:897160a1a3ae 94
el17oc 1:897160a1a3ae 95 }
el17oc 1:897160a1a3ae 96 }
el17oc 2:44e4a6ecdbef 97
el17oc 2:44e4a6ecdbef 98 for(int i = 0; i <= _length; i++) {
el17oc 2:44e4a6ecdbef 99 lcd.setPixel(_snakebody[i].x, _snakebody[i].y, 1);
el17oc 2:44e4a6ecdbef 100
el17oc 2:44e4a6ecdbef 101 //ROB is snakebody[0] being plotted? do you think this would work?
el17oc 2:44e4a6ecdbef 102 // ideally would define snakebody[0], snakebody[1], snake[2] so the snake starts as 3 bits and bit 1 knows to go to bit zero, bit 2 know to go to bit 1 position
el17oc 2:44e4a6ecdbef 103
el17oc 2:44e4a6ecdbef 104 }
el17oc 1:897160a1a3ae 105 }
el17oc 2:44e4a6ecdbef 106 }
el17oc 1:897160a1a3ae 107
el17oc 1:897160a1a3ae 108
el17oc 2:44e4a6ecdbef 109
el17oc 1:897160a1a3ae 110
el17oc 1:897160a1a3ae 111
el17oc 1:897160a1a3ae 112 void Snake::gameover_true(N5110 &lcd) //taking action if crash has occured
el17oc 1:897160a1a3ae 113 {
el17oc 1:897160a1a3ae 114 if (_gameover == true) {
el17oc 1:897160a1a3ae 115
el17oc 1:897160a1a3ae 116 lcd.clear();
el17oc 1:897160a1a3ae 117 lcd.refresh();
el17oc 1:897160a1a3ae 118 lcd.printString( " Game Over L ", 0, 2 );
el17oc 2:44e4a6ecdbef 119 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 120
el17oc 1:897160a1a3ae 121 }
el17oc 1:897160a1a3ae 122
el17oc 1:897160a1a3ae 123 else {
el17oc 1:897160a1a3ae 124
el17oc 1:897160a1a3ae 125
el17oc 1:897160a1a3ae 126 }
el17oc 1:897160a1a3ae 127 }
el17oc 1:897160a1a3ae 128
el17oc 1:897160a1a3ae 129
el17oc 1:897160a1a3ae 130
el17oc 2:44e4a6ecdbef 131
el17oc 1:897160a1a3ae 132 void Snake::check_if_scored(N5110 &lcd, Gamepad &pad)
el17oc 1:897160a1a3ae 133 {
el17oc 1:897160a1a3ae 134 Vector2D Snakehead = _engine.get_Snakehead();
el17oc 1:897160a1a3ae 135 Vector2D Applepos = _engine.get_Applepos(); //need to code clear apple and make sure apple isnt spawning every time
el17oc 1:897160a1a3ae 136
el17oc 1:897160a1a3ae 137 if(Snakehead.x == Applepos.x && Snakehead.y == Applepos.y) {
el17oc 1:897160a1a3ae 138
el17oc 2:44e4a6ecdbef 139 lcd.setPixel(Applepos.x, Applepos.y, 0); // Problem:: Wanted to make sure that this didnt clear the pixel of the snake as it passes through it
el17oc 2:44e4a6ecdbef 140 _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 141 _length = _length++;
el17oc 1:897160a1a3ae 142 _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 143 pad.tone(1500.0,0.5); //need to clear apple
el17oc 1:897160a1a3ae 144 pad.leds_on();
el17oc 1:897160a1a3ae 145 wait(0.5);
el17oc 1:897160a1a3ae 146 pad.leds_off();
el17oc 1:897160a1a3ae 147
el17oc 1:897160a1a3ae 148 }
el17oc 1:897160a1a3ae 149 }
el17oc 1:897160a1a3ae 150
el17oc 1:897160a1a3ae 151 void Snake::check_wall_collisions() // Vector2D *&snakebody hopefully this points to values stored in snakebody then can compare to snakehead
el17oc 1:897160a1a3ae 152 {
el17oc 1:897160a1a3ae 153 Vector2D Snakehead = _engine.get_Snakehead();
el17oc 1:897160a1a3ae 154
el17oc 1:897160a1a3ae 155
el17oc 1:897160a1a3ae 156 if (Snakehead.x == 0 || Snakehead.x == 84 || Snakehead.y == 0 || Snakehead.y == 42) { //how do i access snakehead.headx
el17oc 1:897160a1a3ae 157
el17oc 1:897160a1a3ae 158 _gameover = true;
el17oc 1:897160a1a3ae 159 } else {
el17oc 1:897160a1a3ae 160 _gameover = false;
el17oc 1:897160a1a3ae 161
el17oc 1:897160a1a3ae 162 }
el17oc 1:897160a1a3ae 163 }
el17oc 1:897160a1a3ae 164
el17oc 1:897160a1a3ae 165
el17oc 1:897160a1a3ae 166