-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtryGame.cpp
More file actions
143 lines (134 loc) · 3.38 KB
/
Copy pathtryGame.cpp
File metadata and controls
143 lines (134 loc) · 3.38 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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
//test
struct snakeStructure{
int x, y;
struct snakeStructure *before;
};
struct mapDetail{
int W, H, movement, x, y, reLocate;
struct snakeStructure *snake;
};
struct mapDetail *createMap(){
struct mapDetail *map = (struct mapDetail*)malloc(sizeof(struct mapDetail));
map->H = 10;
map->W = 30;
srand(time(0));
map->snake = (struct snakeStructure*)malloc(sizeof(struct snakeStructure));
map->snake->x = rand() % map->W;
map->snake->y = rand() % map->H;
map->x = rand() % map->W;
map->y = rand() % map->H;
while(map->snake->x == map->x && map->snake->y == map->y){
map->x = rand() % map->W;
map->y = rand() % map->H;
}
map->reLocate = 0;
map->snake->before = NULL;
map->movement = rand() % 4 + 1;
return map;
}
void mapDrawer(struct mapDetail *detail){
char map[detail->H][detail->W];
for(int i = 0; i < detail->H; i++){
for(int j = 0; j < detail->W; j++){
map[i][j] = ' ';
}
}
struct snakeStructure *snake = detail->snake;
map[snake->y][snake->x] = 'O';
while(snake->before){
snake = snake->before;
map[snake->y][snake->x] = 'O';
}
for(int i = 0; i < detail->W+2; i++){
printf("#");
}
printf("\n");
for(int i = 0; i < detail->H; i++){
printf("#");
for(int j = 0; j < detail->W; j++){
printf("%c", map[i][j]);
}
printf("#\n");
}
for(int i = 0; i < detail->W+2; i++){
printf("#");
}
printf("\n");
}
void popTail(struct snakeStructure* snake){
struct snakeStructure *deleteThis = snake;
while(deleteThis->before){
if(deleteThis->before->before){
deleteThis = deleteThis->before;
} else {
break;
}
}
free(deleteThis->before);
deleteThis->before = NULL;
}
void relocateFood(struct mapDetail *map){
while(map->snake->x == map->x && map->snake->y == map->y){
map->x = rand() % map->W;
map->y = rand() % map->H;
}
map->reLocate = 1;
}
void moveChar(struct mapDetail *map){
if(kbhit()){
if(getch() == 0){
char h = getch();
switch(h){
case 72 : //up
map->movement = 1;
break;
case 80 : //down
map->movement = 2;
break;
case 75 : //left
map->movement = 3;
break;
case 77 : //right
map->movement = 4;
break;
}
}
}
struct snakeStructure snake = (*map->snake);
switch(map->movement){
case 1:
snake.y -= 1;
break;
case 2:
snake.y += 1;
break;
case 3:
snake.x -= 1;
break;
case 4:
snake.x += 1;
break;
}
snake.before = map->snake;
map->snake = &snake;
if(!map->reLocate){
popTail(map->snake);
}
if(map->snake->x == map->x && map->snake->y == map->y){
relocateFood(map);
}
}
int main(){
struct mapDetail *map = createMap();
while(1){
system("cls");
moveChar(map);
mapDrawer(map);
sleep(1);
}
}