-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
123 lines (122 loc) · 1.88 KB
/
snake.cpp
File metadata and controls
123 lines (122 loc) · 1.88 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
#include<iostream>
#include<termios.h>
#include<fcntl.h>
#include<unistd.h>
#include"snake.h"
#define wall '+';
#define blank ' ';
#define head_gra '@';
#define body_gra '*';
using namespace std;
const int height=20;
const int width=40;
int scanKeyboard()
{
int input;
struct termios new_settings;
struct termios stored_settings;
tcgetattr(0,&stored_settings);
new_settings=stored_settings;
new_settings.c_lflag &=(~ICANON);
new_settings.c_cc[VTIME] = 0;
tcgetattr(0,&stored_settings);
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
input=getchar();
tcsetattr(0,TCSANOW,&stored_settings);
return input;
}
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO,&oldt);
newt=oldt;
newt.c_lflag &=~(ICANON | ECHO);
tcsetattr(STDIN_FILENO,TCSANOW,&newt);
oldf=fcntl(STDIN_FILENO,F_GETFL,0);
fcntl(STDIN_FILENO,F_SETFL,oldf|O_NONBLOCK);
ch=getchar();
tcsetattr(STDIN_FILENO,TCSANOW,&oldt);
fcntl(STDIN_FILENO,F_SETFL,oldf);
if(ch!=EOF)
{
ungetc(ch,stdin);
return 1;
}
return 0;
}
Head::Head(int x,int y)
{
locx=x;
locy=y;
}
void Head::upmove()
{
locx--;
}
void Head::downmove()
{
locx++;
}
void Head::leftmove()
{
locy--;
}
void Head::rightmove()
{
locy++;
}
void Head::changedir(int a)
{
if(a=='w')
dir=1;
else if(a=='a')
dir=2;
else if(a=='s')
dir=3;
else if(a=='d')
dir=4;
}
int Head::getx()
{
return locx;
}
int Head::gety()
{
return locy;
}
int Head::getdir()
{
return dir;
}
void Body::setx(int x)
{
locx=x;
}
void Body::sety(int y)
{
locy=y;
}
void Body::act()
{
activated=true;
}
int Body::getx()
{
return locx;
}
int Body::gety()
{
return locy;
}
void Body::setloc(int x,int y)
{
locx=x;
locy=y;
}
bool Body::ifact()
{
return activated;
}