-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
70 lines (65 loc) · 1.78 KB
/
Copy pathSnake.cpp
File metadata and controls
70 lines (65 loc) · 1.78 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
#include "Snake.h"
#include <iostream>
#include <cstdlib>
#include "conutils.h"
#include <windows.h>
Snake::Snake(char **g, int h, int w, int len)
: alive(1), direction(RIGHT), trail(-1,-1), height(h), width(w)
{
grid = g;
if (len>width) len = width;
for (int i=len; i>0; --i)
{
snake.push_back(Coord(i,height));
grid[height][i] = SNAKE;
}
}
Snake::~Snake()
{
}
void Snake::move()
{
//get next coord
Coord next(snake.front());
if (direction == UP) --next.y;
else if (direction == DOWN) ++next.y;
else if (direction == LEFT) --next.x;
else ++next.x;
//move the body and tail
trail = snake.back();
for (int i = snake.size()-1; i > 0; --i)
snake[i] = snake[i-1];
//delete the trail on grid
grid[trail.y][trail.x] = NONE;
if (grid[next.y][next.x] == WALL || grid[next.y][next.x] == SNAKE)
{
alive = false;
collision = next;
}
else if (grid[next.y][next.x] == FOOD)
{
snake[0] = next;
grid[next.y][next.x] = SNAKE;
//append a new part
snake.push_back(trail);
grid[trail.y][trail.x] = SNAKE;
//wipe out trail
trail.x = trail.y = -1;
}
else
{
snake[0] = next;
grid[next.y][next.x] = SNAKE;
}
}
void Snake::detectDirection()
{
if ((GetAsyncKeyState(0x65) || GetAsyncKeyState(VK_UP)) && direction != DOWN)
direction = UP;
else if ((GetAsyncKeyState(0x62) || GetAsyncKeyState(VK_DOWN)) && direction != UP)
direction = DOWN;
else if ((GetAsyncKeyState(0x61) || GetAsyncKeyState(VK_LEFT)) && direction != RIGHT)
direction = LEFT;
else if ((GetAsyncKeyState(0x63) || GetAsyncKeyState(VK_RIGHT)) && direction != LEFT)
direction = RIGHT;
}