Repository navigation
Expand file tree
/
Copy pathSnakeGame.cpp
More file actions
93 lines (80 loc) · 2.27 KB
/
Copy pathSnakeGame.cpp
File metadata and controls
93 lines (80 loc) · 2.27 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <curses.h>
#define UP 3
#define DOWN 2
#define RIGHT 5
#define LEFT 4
// Compile with
// g++ SnakeGame.cpp -o SnakeGame -lncurses
//
// This has been shamelessly being snatched from a forum
// http://cboard.cprogramming.com/c-programming/160991-help-ncurses-snake-game.html
int main(void){
// activate the keypad to read from this terminal
keypad(initscr(),TRUE);
int pout = 0; //pen on/off (if even/odd)
int xPos=1; // initialise the x and y positions of the cursor
int yPos=2;
char map[1000][1000];
char c='\0'; // input character initialise to null
while( c!='q'){ // loop while the user has not pressed 'q'
if(pout % 2 == 0){
mvaddch(yPos,xPos,'*');
map[yPos][xPos] = '*';
}
if(pout % 2 != 0){
mvaddch(yPos,xPos,' ');
}
c=getch(); // read a single character
if (c == 'p'){ //p changes even-->odd etc for toggle
pout = pout + 1;
noecho();
}
switch(c){
case UP:
yPos--;
break;
case DOWN:
yPos++;
break;
case LEFT:
xPos--;
break;
case RIGHT:
xPos++;
}
}
mvdelch(yPos,xPos+1); //delete the q
xPos = 1;
yPos = 3;
while( c!='f'){
mvaddch(yPos,xPos,'O'); //This is the start of snake, snake won't move..
// read a single character
c=getch();
// move according to cursor
switch(c){
case UP:
if (map[yPos-1][xPos]== ' '){
yPos--;
map[yPos][xPos] = 'O';
}
break;
case DOWN:
if (map[yPos+1][xPos]== ' '){
yPos++;
map[yPos][xPos] = 'O';
}
break;
case LEFT:
if (map[yPos][xPos-1]== ' '){
xPos--;
map[yPos][xPos] = 'O';
}
break;
case RIGHT:
if (map[yPos][xPos+1]== ' '){
xPos++;
map[yPos][xPos] = 'O';
}
}
}
}