-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.cpp~
146 lines (105 loc) · 1.94 KB
/
snake.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
//to compile
//g++ -g snake.cpp -lcurses
//to run ./a.out
#include <curses.h>
#include <cmath>
#include <stdlib.h>
using namespace std;
struct pos
{int x,y;
};
class queue
{
public:
pos a[20];
int rear;
int front ;
int size;
queue ()
{size=20;rear =-1;front =-1;}
void enQ (pos x);
pos deQ();
pos frnt_ele ();
};
void delay()
{int c = 1, d = 1;
for ( c = 1 ; c <= 32767 ; c++ )
for ( d = 1 ; d <= 3276 ; d++ )
{}
}
int main()
{
WINDOW *hg;
hg=initscr();
wborder(hg, 3, 10, 10, 10, 0, 0, 0, 0);
cbreak();
clear();
pos head,tail,temp,fruit;
queue q;
for (int i=0;i<10;i++)
{temp.x=i+70;temp.y=20;
q.enQ(temp);
}
int snake=10;
head.x=75,head.y=20,tail.x=70,tail.y=20;
char initial[]="*****";
//attron(A_STANDOUT);
mvaddstr(20,70,initial);
move (20,79);
fruit.x=60;
fruit.y=15;
mvaddstr(15,60,"@");
char control='d';
//for (int r=0;r<10;r++)
while (1)
{
nodelay(hg, TRUE);
noecho();
char temp;
delay();
if ((temp= getch()) == ERR);
else {control=temp;}
if (control=='s')
head.y++;
else if (control=='d')
head.x++;
else if (control=='w')
head.y--;
else if (control=='a')
head.x--;
//else if (control=='p')
//getch();
q.enQ(head);
mvaddstr(head.y,head.x,"*");
if (fruit.x==head.x&&fruit.y==head.y)
{mvaddstr(abs(tail.y-10),abs(head.x-20),"@");
tail=q.frnt_ele();}
else {tail=q.deQ();}
mvaddstr(tail.y,tail.x," ");
tail.x++;
wrefresh(stdscr);
}
refresh();
getch();
endwin();
return 0;
}
void queue::enQ (pos x )
{
rear=(rear+1)%size;
a[rear]=x;
if (front==-1)
{front++;}
}
pos queue::deQ()
{
pos temp=a[front];
if(front==rear)
{front=-1;rear=-1;}
else
{front=(front+1)%size;}
return temp;
}
pos queue :: frnt_ele()
{return a[front];
}