-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.c
93 lines (71 loc) · 1.84 KB
/
player.c
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
#include "player.h"
void add_tail_child(Player *player){
int score = (*player).coin_score;
Point *p_last, p_before;
p_last = &((*player).tail[score]);
p_before = (*player).tail[score - 1];
(*p_last).x = p_before.x;
(*p_last).y = p_before.y;
}
void move_tail(Player *player){
int i = (*player).coin_score;
for(; i > 0; i--){
(*player).tail[i].x = (*player).tail[i-1].x;
(*player).tail[i].y = (*player).tail[i-1].y;
}
(*player).tail[0].x += (*player).move_x;
(*player).tail[0].y += (*player).move_y;
}
void move_player(Player *player, int x, int y){
if(x != 0 && (*player).move_x == -x){
return;
} else if(y != 0 && (*player).move_y == -y){
return;
}
(*player).move_x = x;
(*player).move_y = y;
}
void move_player_up(Player *player){
move_player(player, 0, -1);
}
void move_player_bottom(Player *player){
move_player(player, 0, 1);
}
void move_player_right(Player *player){
move_player(player, 1, 0);
}
void move_player_left(Player *player){
move_player(player, -1, 0);
}
void reset_player(Player *player, Point position, Point move_to){
(*player).tail = malloc(TAIL_SIZE * sizeof(Point));
(*player).tail[0].x = position.x;
(*player).tail[0].y = position.y;
(*player).move_x = move_to.x;
(*player).move_y = move_to.y;
(*player).coin_score = 0;
}
void reset_players(){
Point position, move_to;
position.x = 1;
position.y = 1;
move_to.x = 1;
move_to.y = 0;
reset_player(&player1, position, move_to);
position.x = MAX_COLUMN - 2;
position.y = MAX_ROW - 2;
move_to.x = -1;
move_to.y = 0;
reset_player(&player2, position, move_to);
}
void initialize_players(){
reset_players();
player1.mark = 'A';
player1.max_coin_score = 0;
player1.life_score = 0;
enter_player_name(&player1, "Player 1");
player2.mark = 'B';
player1.max_coin_score = 0;
player1.life_score = 0;
enter_player_name(&player2, "Player 2");
}