Simon Atkinson 201255483

Dependencies:   mbed

Snake Game

Summary

Hello and welcome to my Snake Game. Snake is a simple game where you control a snake and eat an apple which increases your size. Normally touching the walls will kill you, always touching yourself will make you die!

Controls

The Controls for this game are simple the Start button starts the game (who would have thought) this button is the left button on the bottom of the gamepad next to the two blue potentiometers. The Reset button on the right of the potentiometers resets the game. Once the game starts use the left thumbstick to control the movement of the snake.

If you hit the wall with your snake you will die and the game will end.

Bugs/Missing Features

Unfortunatley I wasn't able to get the game eating the apple to work, when I tried I could never get it to detect the collisions I tried a few different ways but ran out of time. Because that doesn't work the score doesn't increase and the apple doesn't spawn in a different place. I will try continue to work on this when I have time as I enjoyed doing this project even though it was very frustrating at times!

Committer:
Psy1990
Date:
Fri Jun 05 22:57:33 2020 +0000
Revision:
21:e8d66c5f68cc
Parent:
14:8d12bc972cb1
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Psy1990 8:32825d724856 1 #include "Snake.h"
Psy1990 8:32825d724856 2
Psy1990 8:32825d724856 3 // nothing doing in the constructor and destructor
Psy1990 8:32825d724856 4 Snake::Snake()
Psy1990 8:32825d724856 5 {
Psy1990 8:32825d724856 6
Psy1990 8:32825d724856 7 }
Psy1990 8:32825d724856 8
Psy1990 8:32825d724856 9 Snake::~Snake()
Psy1990 8:32825d724856 10 {
Psy1990 8:32825d724856 11
Psy1990 8:32825d724856 12 }
Psy1990 8:32825d724856 13
Psy1990 13:c20acb3b1adf 14 void Snake::init(int x,int y, int score, int speed, int snakeheight, int snakewidth) //Initial values for the snake
Psy1990 8:32825d724856 15 {
Psy1990 10:3e37b58e8600 16 _x = WIDTH/2; // Sets the position to be in the middle of the screen
Psy1990 9:25597bc0cecc 17 _y = HEIGHT/2;
Psy1990 14:8d12bc972cb1 18 _score = score;
Psy1990 10:3e37b58e8600 19 _speed = speed; // The value that holds the speed of the snake
Psy1990 13:c20acb3b1adf 20 _snakewidth = snakewidth;
Psy1990 13:c20acb3b1adf 21 _snakeheight = snakeheight;
Psy1990 10:3e37b58e8600 22
Psy1990 8:32825d724856 23
Psy1990 8:32825d724856 24 }
Psy1990 8:32825d724856 25
Psy1990 8:32825d724856 26 void Snake::draw(N5110 &lcd)
Psy1990 8:32825d724856 27 {
Psy1990 10:3e37b58e8600 28
Psy1990 8:32825d724856 29 // draw Snake in screen buffer.
Psy1990 13:c20acb3b1adf 30 lcd.drawRect(_x,_y,_snakewidth,_snakeheight,FILL_BLACK); // Draws the initial snake head is a 5x5 Square matches well with the cirlce of the apple when set to 2
Psy1990 8:32825d724856 31
Psy1990 8:32825d724856 32 }
Psy1990 8:32825d724856 33
Psy1990 10:3e37b58e8600 34
Psy1990 10:3e37b58e8600 35 void Snake::update(Direction d,float mag)
Psy1990 10:3e37b58e8600 36 {
Psy1990 10:3e37b58e8600 37
Psy1990 10:3e37b58e8600 38 _x += _move.x; //Needed to move in the x-axis
Psy1990 10:3e37b58e8600 39 _y += _move.y; //Needed to move in the y-axis
Psy1990 10:3e37b58e8600 40 _speed = int(mag*1.5f); // Sets the scaling for the speed the snake moves, by adjusting this you can change the speed of the snake.
Psy1990 10:3e37b58e8600 41
Psy1990 10:3e37b58e8600 42 //When moving the thubstick this is read by the game and controls the movement of the snake,
Psy1990 10:3e37b58e8600 43 //moving it a small ammount will make the snake move slowly holding down for too long and your snake will get some speed!
Psy1990 10:3e37b58e8600 44 if (d == N){
Psy1990 10:3e37b58e8600 45 _move.y -=_speed; // Snake Moves Up
Psy1990 10:3e37b58e8600 46 _move.x = 0; // Makes it so the snake can only go in compass directions (N,E,S,W) same for all directions
Psy1990 10:3e37b58e8600 47 }
Psy1990 10:3e37b58e8600 48 if (d == S){
Psy1990 10:3e37b58e8600 49 _move.y +=_speed; // Snake moves Down
Psy1990 10:3e37b58e8600 50 _move.x = 0;
Psy1990 10:3e37b58e8600 51 }
Psy1990 10:3e37b58e8600 52 if (d == E){
Psy1990 10:3e37b58e8600 53 _move.y = 0;
Psy1990 10:3e37b58e8600 54 _move.x +=_speed; // Snake moves right
Psy1990 10:3e37b58e8600 55 }
Psy1990 10:3e37b58e8600 56 if (d == W){
Psy1990 10:3e37b58e8600 57 _move.y = 0;
Psy1990 10:3e37b58e8600 58 _move.x -=_speed; // Snake moves Left
Psy1990 10:3e37b58e8600 59 }
Psy1990 10:3e37b58e8600 60
Psy1990 12:8eb40a18f15d 61
Psy1990 12:8eb40a18f15d 62 /* Deadly Wall turned on
Psy1990 12:8eb40a18f15d 63
Psy1990 12:8eb40a18f15d 64 //Uncomment this and comment the section in SnakeEngine.cpp if you want the snake to not die when he touches the edges as this will keep
Psy1990 12:8eb40a18f15d 65 //him in safely in the game area
Psy1990 12:8eb40a18f15d 66
Psy1990 12:8eb40a18f15d 67
Psy1990 11:ba20e1b516a1 68 // Check that the Snakes coordinates aren't greater the the size of the screen which
Psy1990 11:ba20e1b516a1 69 // would mean the snake has gone off the screen
Psy1990 11:ba20e1b516a1 70 if (_x>=(WIDTH-6)) { // 5 pixles for snake and 1 pixle for border
Psy1990 11:ba20e1b516a1 71 _x=(WIDTH-6);
Psy1990 11:ba20e1b516a1 72 }
Psy1990 11:ba20e1b516a1 73 if (_x<=1) { // 1 pixle offset due to border
Psy1990 11:ba20e1b516a1 74 _x = 1;
Psy1990 11:ba20e1b516a1 75 }
Psy1990 11:ba20e1b516a1 76 if (_y >=(HEIGHT-6)) { // 5 pixles for snake and 1 pixle for border
Psy1990 11:ba20e1b516a1 77 _y=(HEIGHT-6);
Psy1990 11:ba20e1b516a1 78 }
Psy1990 14:8d12bc972cb1 79 if (_y<=9) { // Checks that its in the game area as there is a line (8 pixels missing due to scoredisplay and 1 pixle due to border)
Psy1990 11:ba20e1b516a1 80 _y=9;
Psy1990 11:ba20e1b516a1 81 }
Psy1990 12:8eb40a18f15d 82
Psy1990 12:8eb40a18f15d 83 // End of Save wall Section */
Psy1990 12:8eb40a18f15d 84
Psy1990 12:8eb40a18f15d 85
Psy1990 10:3e37b58e8600 86 }
Psy1990 10:3e37b58e8600 87
Psy1990 10:3e37b58e8600 88 void Snake::add_score() // Not in use yet but will add to score when apple is ate by the snake
Psy1990 8:32825d724856 89 {
Psy1990 8:32825d724856 90 _score++;
Psy1990 8:32825d724856 91 }
Psy1990 10:3e37b58e8600 92 int Snake::get_score() //Allows the game to get the value of the score
Psy1990 8:32825d724856 93 {
Psy1990 8:32825d724856 94 return _score;
Psy1990 8:32825d724856 95 }
Psy1990 8:32825d724856 96
Psy1990 10:3e37b58e8600 97 Vector2D Snake::get_move() { // Allows the game to see the value stored in the movement(m) which shows how fast the snake will move
Psy1990 10:3e37b58e8600 98
Psy1990 10:3e37b58e8600 99 Vector2D m = {_move.x, _move.y};
Psy1990 10:3e37b58e8600 100 return m;
Psy1990 10:3e37b58e8600 101 }
Psy1990 8:32825d724856 102
Psy1990 10:3e37b58e8600 103
Psy1990 10:3e37b58e8600 104 Vector2D Snake::get_pos() { //Gets the position of the snake this will be used for collision detection soon
Psy1990 13:c20acb3b1adf 105 Vector2D p = {_x,_y};
Psy1990 13:c20acb3b1adf 106 return p;
Psy1990 8:32825d724856 107 }