Skip to content
This repository was archived by the owner on Jan 12, 2021. It is now read-only.

Commit f7efd53

Browse files
authored
Merge pull request #1 from MCWertGaming/snekAlternateVersion
rework of snek
2 parents 3752b08 + 7af7f02 commit f7efd53

File tree

8 files changed

+355
-215
lines changed

8 files changed

+355
-215
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ cmake-build-debug-visual-studio/
2727
cmake-build-debug-cygwin/
2828
cmake-build-minsizerel/
2929
cmake-build-relwithdebinfo/
30+
cmake-build-debug-mingw/
3031
.idea/
32+
# binaries
33+
*.a
34+
*.out
35+
*.exe

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@ project(snek)
44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_STANDARD_REQUIRED true)
66

7-
add_executable(snek src/main.cpp)
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lncurses")
8+
9+
# static link windows version, or it wont run outside MSYS2 or MinGW
10+
# A bad practise, but i doesn't work otherwise
11+
if (WIN32)
12+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
13+
endif (WIN32)
14+
15+
add_executable(snek src/main.cpp src/utils.cpp)

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
# snek
2-
If you hit yourself or the wall, you'll die. If you eat the fruit (F) your score increases and tail gets longer.
2+
snek - A simple snake in the console
3+
4+
# build dependencies
5+
## Linux
6+
- a C++ compiler (gcc and clang are supported)
7+
- ncurses
8+
## Windows
9+
- Msys2 with gcc and ncurses installed
10+
```bash
11+
pacman -Syu gcc ncurses
12+
```
13+
14+
# compiling
15+
## Linux
16+
```bash
17+
cmake
18+
make
19+
```
20+
## Windows
21+
```powershell
22+
g++ /src/main.cpp -o snek.exe -static -lncurses
23+
```

TODO

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// snek TODO
2+
3+
add optional game modifications
4+
add more ways to control the game
5+
add pause
6+
add easter egg
7+
add sound effects
8+
make the field explode on loose
9+
add lose screen
10+
11+
proper readme with compilation, dependencies and requirements
12+
13+
create game menu
14+
15+
add second player

src/main.cpp

Lines changed: 36 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -1,227 +1,50 @@
1-
//by Alexander Prosser
2-
1+
// snake
2+
// TODO add proper header for source files and header with copyright notice
3+
// TODO move from ncurses to low-level system libraries (windows.h and unistd.h)
4+
#include <ncurses/ncurses.h>
35
#include <iostream>
4-
#include <conio.h>
5-
#include <windows.h>
6-
using namespace std;
7-
8-
bool gameOver;
9-
10-
const int width = 50;
11-
const int height = 20;
12-
13-
int x, y, fruitX, fruitY, score;
146

15-
int tailX[100], tailY[100];
16-
int nTail;
17-
18-
enum eDirecrion {STOP = 0, LEFT, RIGHT, UP, DOWN};
19-
eDirecrion dir;
20-
21-
int colorBarrier = 8;
22-
int colorSnake = 10;
23-
int colorFruit = 4;
24-
25-
void setup()
26-
{
27-
gameOver = false;
28-
dir = STOP;
7+
#include "snake.h"
298

30-
x = width / 2;
31-
y = height / 2;
9+
/*
10+
* // TODO rework field like this
11+
* field:
12+
*
13+
* ############# SNEK
14+
* # # by MCWertGaming
15+
* # #
16+
* # # Time: XX:XX // TODO implement time clock
17+
* # # Score: XX
18+
* # #
19+
* # #
20+
* #############
21+
*
22+
*/
3223

33-
fruitX = rand() % width;
34-
fruitY = rand() % height;
24+
// TODO move universal functions into a namespace
25+
// TODO move logic into class // namespace
26+
// TODO change qualified functions to inline functions
3527

36-
score == 0;
37-
}
3828

39-
void draw()
29+
int main()
4030
{
41-
system("cls");
42-
43-
for (int i = 0; i < width + 1; i++)
44-
{
45-
HANDLE hConsole;
46-
47-
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
48-
SetConsoleTextAttribute(hConsole, colorBarrier);
49-
50-
cout << "#";
51-
}
52-
cout << endl;
53-
54-
for (int i = 0; i < height; i++)
55-
{
56-
for (int j = 0; j < width; j++)
57-
{
58-
HANDLE hConsole;
59-
60-
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
61-
SetConsoleTextAttribute(hConsole, colorBarrier);
62-
63-
if (j == 0)
64-
cout << "#";
65-
if (j == width - 1)
66-
cout << "#";
67-
68-
if (i == y && j == x)
69-
{
70-
HANDLE hConsole;
71-
72-
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
73-
SetConsoleTextAttribute(hConsole, colorSnake);
74-
75-
cout << "O";
76-
}
77-
else if (i == fruitY && j == fruitX)
78-
{
79-
HANDLE hConsole;
80-
81-
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
82-
SetConsoleTextAttribute(hConsole, colorFruit);
83-
84-
cout << "F";
85-
}
86-
else
87-
{
88-
bool print = false;
89-
for (int k = 0; k < nTail; k++)
90-
{
91-
if (tailX[k] == j && tailY[k] == i)
92-
{
93-
HANDLE hConsole;
31+
snake::gameSetup();
9432

95-
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
96-
SetConsoleTextAttribute(hConsole, colorSnake);
33+
unsigned short int quit;
9734

98-
print = true;
99-
cout << "o";
100-
}
101-
}
102-
if (!print)
103-
cout << " ";
104-
}
105-
}
106-
cout << endl;
107-
}
108-
109-
for (int i = 0; i < width + 1; i++)
110-
cout << "#";
111-
cout << endl;
112-
113-
HANDLE hConsole;
114-
int colorText = 15;
115-
116-
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
117-
SetConsoleTextAttribute(hConsole, colorText);
118-
119-
cout << "SCORE: " << score << endl;
120-
if (dir == STOP)
121-
cout << "\nStart by moving with WASD. Press SPACE to pause the game. With X you'll quit." << endl;
122-
}
123-
124-
void input()
125-
{
126-
if (_kbhit())
35+
// game loop
36+
while (true)
12737
{
128-
switch (_getch())
129-
{
130-
case 'w':
131-
dir = UP;
38+
quit = snake::moveSnake();
39+
if (quit != 0)
13240
break;
133-
case 'a':
134-
dir = LEFT;
135-
break;
136-
case 's':
137-
dir = DOWN;
138-
break;
139-
case 'd':
140-
dir = RIGHT;
141-
break;
142-
case ' ':
143-
cout << "\nPAUSE\nPress ENTER to resume." << endl;
144-
cin.get();
145-
break;
146-
case 'x':
147-
gameOver = true;
148-
break;
149-
default:
150-
break;
151-
}
152-
}
153-
}
154-
155-
void logic()
156-
{
157-
int prevX = tailX[0];
158-
int prevY = tailY[0];
159-
int prevX2, prevY2;
160-
161-
tailX[0] = x;
162-
tailY[0] = y;
163-
164-
for (int i = 1; i < nTail; i++)
165-
{
166-
prevX2 = tailX[i];
167-
prevY2 = tailY[i];
168-
169-
tailX[i] = prevX;
170-
tailY[i] = prevY;
171-
172-
prevX = prevX2;
173-
prevY = prevY2;
174-
}
175-
176-
switch (dir)
177-
{
178-
case LEFT:
179-
x--;
180-
break;
181-
case RIGHT:
182-
x++;
183-
break;
184-
case UP:
185-
y--;
186-
break;
187-
case DOWN:
188-
y++;
189-
break;
190-
default:
191-
break;
192-
}
193-
194-
if (x > width - 2 || x < 0 || y > height - 1 || y < 0)
195-
gameOver = true;
196-
197-
for (int i = 0; i < nTail; i++)
198-
if (tailX[i] == x && tailY[i] == y && dir != STOP)
199-
gameOver = true;
200-
201-
if (x == fruitX && y == fruitY)
202-
{
203-
nTail++;
204-
score++;
205-
206-
fruitX = rand() % width;
207-
fruitY = rand() % height;
41+
snake::drawSnake(false);
20842
}
209-
}
210-
211-
int main()
212-
{
213-
SetConsoleTitle("snek");
214-
215-
setup();
216-
while (!gameOver)
217-
{
218-
draw();
219-
input();
220-
logic();
221-
}
222-
223-
cout << "\nGame Over\nPress ENTER to quit ..." << endl;
224-
225-
cin.get();
43+
// destroys ncurses
44+
endwin();
45+
if (quit == 1)
46+
std::cout << "Game aborted...\n";
47+
else if (quit == 2)
48+
std::cout << "Game Over!\n";
22649
return 0;
22750
}

0 commit comments

Comments
 (0)