-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
192 lines (165 loc) · 3.35 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include<conio.h>
#include <stdlib.h>
#include<time.h>
#include <windows.h>
using namespace std;
int score;
int y;
int y2;
int x;
int x2;
int const width=20,height=20;
bool gameOver;
struct point {
int x,y ;
};
point player,fruit;
point tail[400];
int n_tail;
enum direction {STOP=0,LEFT,RIGHT,UP,DOWN};
direction dir;
void setup(){
player.x=width/2;
player.y=height/2;
fruit.x=rand()%(width-2)+1;
fruit.y=rand()%(height-2)+1;
gameOver=false;
score=0;
y=0;
y2=0;
x=0;
x2=0;
dir=STOP;
n_tail=0;
}
void draw(){
system("cls");
int i,j;
for(i=0 ; i<width ; i++)
cout<<"*";
cout<<endl;
for(i=0 ; i<height ; i++){
for(j=0 ; j<width;j++){
if(j==0||j==width-1)
cout<<"*";
else if( j==player.x && i==player.y)
cout<<"O";
else if( j==fruit.x && i==fruit.y)
cout<<"X";
else{
bool flag =false;
for(int k=0;k<n_tail;k++)
if (j==tail[k].x&&i==tail[k].y){
cout<<"o";
flag=true;
}
if (!flag)
cout<<" ";
}
}
cout <<endl;
}
for(i=0;i<width;i++)
cout<<"*";
cout <<"\n score:"<<score;
}
void input(){
if (kbhit()){
switch(getche()){
case'w' :
case 'W':
dir=UP;
break;
case's' :
case 'S':
dir=DOWN;
break;
case'a' :
case 'A':
dir=LEFT;
break;
case'd' :
case 'D':
dir=RIGHT;
break;
default:
break;
}
}
}
void lagic(){
for(int i=n_tail-1;i>=1;i--){
tail[i].x=tail[i-1].x;
tail[i].y=tail[i-1].y;
}
tail[0].x=player.x;
tail[0].y=player.y;
switch (dir){
case LEFT:
y = 0;
y2 = 0;
x2 = 1;
if(x < x2)
player.x=(player.x-1+width)%width;
else
player.x=(player.x+1+height)%height;
break;
case RIGHT:
y = 0;
y2 = 0;
x = 1;
if(x > x2)
player.x=(player.x+1)%width;
else
player.x=(player.x-1)%width;
break;
case UP:
y2 = 1;
x = 0;
x2 = 0;
if(y < y2)
player.y=(player.y-1+height)%height;
else
player.y=(player.y+1+height)%height;
break;
case DOWN:
y = 1;
x = 0;
x2 = 0;
if(y > y2)
player.y=(player.y+1)%height;
else
player.y=(player.y-1+height)%height;
break;
default:
break;
}
if (player.x==fruit.x&&player.y==fruit.y)
{
score+=10;
Beep(500,50);
fruit.x=rand()%(width-2)+1;
fruit.y=rand()%(height-2)+1;
n_tail++;
}
for(int i=n_tail-1;i>=1;i--)
{
if (player.x==tail[i].x&&player.y==tail[i].y)
gameOver = TRUE;
}
}
int main(){
srand(time(NULL));
setup();
while(!gameOver)
{
draw();
input();
lagic();
Sleep(50);
}
cout << "\nGame Over :(" <<endl;
getch();
return 0;
}