-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGameManager.cpp
More file actions
77 lines (68 loc) · 1.34 KB
/
GameManager.cpp
File metadata and controls
77 lines (68 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "GameManager.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include "Blocks.h"
#include "Food.h"
#include "Snake.h"
#include "pitches.h"
#include "SnakeGame.h"
bool gameOver = false;
GameManager::GameManager(){
_state = 0;
gameOver = false;
score = 0;
_sound = true;
}
void GameManager::initialize(Adafruit_PCD8544 *display, Snake* snake, Food* food){
_display =display;
_snake = snake;
_food = food;
snake->initialize(_display);
food->initialize(_display);
}
void GameManager::moveSnake(byte direction)
{
_snake->setDirection(direction);
}
void GameManager::execute()
{
if(!_snake->_selfTouch)
{
checkForCollision();
_snake->execute();
_food->execute();
draw();
}
else
{
gameOver=true;
}
}
void GameManager::checkForCollision()
{
//check collision with food
if(_food->_x == _snake->_headX &&
_food->_y == _snake->_headY)
{
if(_sound)
{
tone(SPEAKER, NOTE_A7, TONE_FOOD_DURATION);
}
score+=_food->points;
_snake->addBlock(_food->_x, _food->_y);
_food->randomize(WIDTH - WEIGHT, HEIGHT - WEIGHT, WEIGHT);
}
}
void GameManager::setSound(int sound){
_sound = sound;
if(_sound)
{
tone(SPEAKER, NOTE_A7, TONE_FOOD_DURATION);
}
}
void GameManager::draw(){
//draw rectangle border
_display->drawRect(0, 0, WIDTH, HEIGHT, BLACK);
_display->display();
}