-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
156 lines (146 loc) · 4.53 KB
/
snake.cpp
File metadata and controls
156 lines (146 loc) · 4.53 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
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
149
150
151
152
153
154
155
#include <stdio.h>
#include <cstdlib>
#include <pthread.h>
#include <curses.h>
#include <unistd.h>
#include <iostream>
#include "snake.hpp"//direction is declared here
using namespace std;
#define gridsize 15
//Used to get directional input while letting main thread run the game
void *getDirection(void *)
{
while(direction != 5 || alive)
{//Input unbuffered directions
char input = getch();
if(input != ERR)
{
switch (input)
{
case 'w': if (direction != 4) direction = 2; break;
case 'a': if (direction != 1) direction = 3; break;
case 's': if (direction != 2) direction = 4; break;
case 'd': if (direction != 3) direction = 1; break;
case 27: alive = 0; break; // ESC to exit
}
}
usleep(150000);
}
return nullptr;
}
int main()
{
int score = start();//Gain 5 score for every fruit ate
cout << "Good Game! You had a score of " << score << '!' << endl;
}
//Function to update the position of head, and check for collisions
snake updateHead(snake snake)
{
switch(direction)
{
case 1: snake.head.x++;break;
case 2: snake.head.y--;break;
case 3: snake.head.x--;break;
case 4: snake.head.y++;break;
default: break;
}if(snake.head.x >= 14 || snake.head.x <= 0 || snake.head.y <= 0 || snake.head.y >= 14)
alive = 0;
for(int i = 0; i < snake.length - 2; i++)
{
if(snake.head == snake.body[i])
{
alive = 0;
}
}
return snake;
}
//Overloading double equal for loc
bool loc::operator==(const loc &f) const {return x == f.x && y == f.y;}
//Creates a location for the fruit on a spot the snake is not currently on
loc spawnFruit(snake snake){
srand(static_cast<unsigned int>(time(NULL)));
bool spotfound = false;
loc fruit;
do
{
int x = (rand() % 13) + 1;
int y = (rand() % 13) + 1;
fruit = {x, y};
spotfound = true;
if(fruit == snake.head)
{
spotfound = false;
continue;
}
for(int i = 0; i < snake.length - 1; i++)
{
if(fruit == snake.body[i])
spotfound = false;
}
}while(!spotfound);
return fruit;
}
int start()
{
int moving = 0;//Off on round start, starts when first button is pressed
snake larry;
larry.body.resize(2);
larry.length = 3;//Since this counts the head, subtract 1 to get to body, then subtract 1 again since we start with 0
larry.head = {2, 7};
larry.body[0] = {1, 7};
larry.body[1] = {0, 7};
loc fruit = spawnFruit(larry);
//Ncurses initialization
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE); // make getch() non-blocking
keypad(stdscr, TRUE); // enable arrow keys if needed
//Ncurses setup done
pthread_t handleDir;
pthread_create(&handleDir, nullptr, &getDirection, nullptr);//Thread 1 to handle direction input
while(alive)
{//Eating will be added later
clear();
for(int i = larry.length - 2; i >= 0; i--)//Update snake body
{
if(i > 0)
{
larry.body[i].x = larry.body[i-1].x;
larry.body[i].y = larry.body[i-1].y;
}else
{
larry.body[i].x = larry.head.x;
larry.body[i].y = larry.head.y;
}
}larry = updateHead(larry);//Function just for updating head location
if(larry.head == fruit)
{
larry.length++;
larry.body.resize(larry.length-1);
larry.body[larry.length-2] = larry.body[larry.length-3];
fruit = spawnFruit(larry);
}
mvaddch(fruit.y, fruit.x, '@');
if(direction == 5) break;
for(int i = 0; i < larry.length - 1; i++)//Draw snake body onto map
{
mvaddch(larry.body[i].y, larry.body[i].x, 'o');
}
mvaddch(larry.head.y, larry.head.x, 'O');
for(int y = 0; y < gridsize; y++)//To print out grid
{
for(int x = 0; x < gridsize; x++)
{//Unsure what to draw for corners
if(x == 0 || x == gridsize-1)
mvaddch(y, x, '|');//Draws the side border
if(y == 0 || y == gridsize-1)
mvaddch(y, x, '_');//Draws the top and bottom border
}
}
refresh();
usleep(150000);
}
endwin();
return larry.length - 3;
}