-
Notifications
You must be signed in to change notification settings - Fork 1
/
snakeCPP.cpp
148 lines (122 loc) · 4.19 KB
/
snakeCPP.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// snakeCPP.cpp : This file contains the 'main' function. Program execution begins and ends there.
// Calling libraries and namespaces
#include <iostream>
#include <list>
#include <thread>
using namespace std;
#include <Windows.h>
// Defining the console screen size
int nScreenWidth = 120;
int nScreenHeight = 30;
// Defining the snake position using an structure
struct sSnakeSegment {
int x;
int y;
};
// The game!
int main()
{
// Create Screen Buffer
wchar_t* screen = new wchar_t[nScreenWidth * nScreenHeight];
for (int i = 0; i < nScreenWidth * nScreenHeight; i++) screen[i] = L' ';
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
while (1) {
// Reset to known state
list<sSnakeSegment> snake = { {60,15},{61,15},{62,15},{63,15},{64,15},{65,15},{66,15},{67,15},{68,15},{69,15} };
int nFoodX = 30;
int nFoodY = 15;
int nScore = 0;
int nSnakeDirection = 3;
bool bDead = false;
bool bKeyLeft = false, bKeyRight = false, bKeyLeftOld = false, bKeyRightOld = false;
while (!bDead){
// Frame Timing, compensate for aspect ratio of command line
auto t1 = chrono::system_clock::now();
while ((chrono::system_clock::now() - t1) < ((nSnakeDirection % 2 == 1) ? 120ms : 200ms))
{
// Get Input,
bKeyRight = (0x8000 & GetAsyncKeyState((unsigned char)('\x27'))) != 0;
bKeyLeft = (0x8000 & GetAsyncKeyState((unsigned char)('\x25'))) != 0;
if (bKeyRight && !bKeyRightOld)
{
nSnakeDirection++;
if (nSnakeDirection == 4) nSnakeDirection = 0;
}
if (bKeyLeft && !bKeyLeftOld)
{
nSnakeDirection--;
if (nSnakeDirection == -1) nSnakeDirection = 3;
}
bKeyRightOld = bKeyRight;
bKeyLeftOld = bKeyLeft;
}
// Game logic!
// Update the snake position and move the head to the correct place
switch (nSnakeDirection)
{
// Up
case 0:
snake.push_front({ snake.front().x, snake.front().y - 1 });
break;
// Right
case 1:
snake.push_front({ snake.front().x + 1, snake.front().y });
break;
// Down
case 2:
snake.push_front({ snake.front().x, snake.front().y + 1 });
break;
// Left
case 3:
snake.push_front({ snake.front().x - 1, snake.front().y });
break;
}
//Collision detect snake food(apple)
if (snake.front().x == nFoodX && snake.front().y == nFoodY) {
nScore++;
while (screen[nFoodY * nScreenWidth + nFoodX] != L' ') {
nFoodX = rand() % nScreenWidth;
nFoodY = (rand() % (nScreenHeight - 3)) +3;
}
for (int i = 0; i < 5; i++)
snake.push_back({ snake.back().x, snake.back().y });
}
// Collision detect snake world
if (snake.front().x < 0 || snake.front().x >= nScreenWidth)
bDead = true;
if (snake.front().y < 3 || snake.front().y >= nScreenHeight)
bDead = true;
// Collision detect snake vs snake(Game over!)
for (list<sSnakeSegment>::iterator i = snake.begin(); i != snake.end(); i++)
if (i != snake.begin() && i->x == snake.front().x && i->y == snake.front().y)
bDead = true;
// Snake chops it's own tail
snake.pop_back();
// Screen design(super easy and simple)
// Clear screen
for (int i = 0; i < nScreenWidth * nScreenHeight; i++) screen[i] = L' ';
// Printing stats and borders for the game
for (int i = 0; i < nScreenWidth; i++) {
screen[i] = L' ';
screen[2 * nScreenWidth + i] = L'=';
}
wsprintf(&screen[nScreenWidth + 5], L"www.jpromano.net || SNAKE SCORE: %d", nScore);
// Print the snake
for (auto s : snake)
screen[s.y * nScreenWidth + s.x] = bDead ? L'+' : L'O';
// Print the snake's head
screen[snake.front().y * nScreenWidth + snake.front().x] = bDead ? L'X' : L'@';
// Give it food, mostly apples :)
screen[nFoodY * nScreenWidth + nFoodX] = L'%';
if (bDead)
wsprintf(&screen[15 * nScreenWidth + 40], L" PRESS 'SPACE' TO PLAY AGAIN ");
// Show the game!
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
}
// Wait for the user to hit space to restart the game
while ((0x8000 & GetAsyncKeyState((unsigned char)('\x20'))) == 0);
}
return 0;
}