
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!
Snake/Snake.cpp
- Committer:
- Psy1990
- Date:
- 2020-06-05
- Revision:
- 21:e8d66c5f68cc
- Parent:
- 14:8d12bc972cb1
File content as of revision 21:e8d66c5f68cc:
#include "Snake.h" // nothing doing in the constructor and destructor Snake::Snake() { } Snake::~Snake() { } void Snake::init(int x,int y, int score, int speed, int snakeheight, int snakewidth) //Initial values for the snake { _x = WIDTH/2; // Sets the position to be in the middle of the screen _y = HEIGHT/2; _score = score; _speed = speed; // The value that holds the speed of the snake _snakewidth = snakewidth; _snakeheight = snakeheight; } void Snake::draw(N5110 &lcd) { // draw Snake in screen buffer. 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 } void Snake::update(Direction d,float mag) { _x += _move.x; //Needed to move in the x-axis _y += _move.y; //Needed to move in the y-axis _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. //When moving the thubstick this is read by the game and controls the movement of the snake, //moving it a small ammount will make the snake move slowly holding down for too long and your snake will get some speed! if (d == N){ _move.y -=_speed; // Snake Moves Up _move.x = 0; // Makes it so the snake can only go in compass directions (N,E,S,W) same for all directions } if (d == S){ _move.y +=_speed; // Snake moves Down _move.x = 0; } if (d == E){ _move.y = 0; _move.x +=_speed; // Snake moves right } if (d == W){ _move.y = 0; _move.x -=_speed; // Snake moves Left } /* Deadly Wall turned on //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 //him in safely in the game area // Check that the Snakes coordinates aren't greater the the size of the screen which // would mean the snake has gone off the screen if (_x>=(WIDTH-6)) { // 5 pixles for snake and 1 pixle for border _x=(WIDTH-6); } if (_x<=1) { // 1 pixle offset due to border _x = 1; } if (_y >=(HEIGHT-6)) { // 5 pixles for snake and 1 pixle for border _y=(HEIGHT-6); } 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) _y=9; } // End of Save wall Section */ } void Snake::add_score() // Not in use yet but will add to score when apple is ate by the snake { _score++; } int Snake::get_score() //Allows the game to get the value of the score { return _score; } Vector2D Snake::get_move() { // Allows the game to see the value stored in the movement(m) which shows how fast the snake will move Vector2D m = {_move.x, _move.y}; return m; } Vector2D Snake::get_pos() { //Gets the position of the snake this will be used for collision detection soon Vector2D p = {_x,_y}; return p; }