-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathred.cpp
More file actions
85 lines (77 loc) · 2.3 KB
/
red.cpp
File metadata and controls
85 lines (77 loc) · 2.3 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
#include "red.h"
#include <cstdio>
Red::Red() : time_of_last_red(0),power(TYPE_OF_POWER),display(0),time_of_last_power_up(0){
frequency_of_power_up = time_between_red;
points_for_red = POINTS_FOR_RED;
last_animation = 0;
state = 0;
food_bmp = SDL_LoadBMP("./bitmaps/czer.bmp");
big_food_bmp = SDL_LoadBMP("./bitmaps/big_czer.bmp");
food_now = food_bmp;
}
int Red::display_red(double time,Snake* snake,SDL_Surface* screen,int* points,double delta){
int wait_time = frequency_of_power_up + time_how_long_red_stays;
if (!time_of_last_red) wait_time = frequency_of_power_up;
progress_bar(screen, time,*snake);
if (!display && (int)(time - time_of_last_red) == wait_time){
position_food(*snake, screen);
display = 1;
time_of_last_red = time;
DrawFood(screen,delta);
power_up(snake, screen, time,points);
return 1;
}
else if (time - time_of_last_red <= time_how_long_red_stays && display){
DrawFood(screen,delta);
power_up(snake, screen, time,points);
return 1;
}
else if (display){
display = 0;
}
return 0;
}
void Red::power_up(Snake* snake, SDL_Surface *screen,double time,int* points){
if (time-time_of_last_power_up < time_how_long_red_stays || !check_if_eaten(*snake))
return ;
time_of_last_power_up = time;
display = 0;
if (power == 0){
power = (rand()%2)+1;
}
if (power == 1){
*points+=points_for_red;
snake->slowdown_snake();
}
else if (power == 2){
*points+=points_for_red;
snake->shrink_snake(removed_parts);
}
}
double Red::progress_bar(SDL_Surface* screen, double time,Snake snake){
double elapsed_time = time - time_of_last_red;
double percent = (time_how_long_red_stays - elapsed_time) / time_how_long_red_stays;
if(time-time_of_last_power_up< frequency_of_power_up){
return 0;
}
else if (time >= frequency_of_power_up && elapsed_time <= time_how_long_red_stays) {
return percent;
}
return 0;
}
void Red::save_red(FILE* fptr){
save(fptr);
fprintf(fptr,",%lf,%lf,%d,%d\n",time_of_last_red,
time_of_last_power_up,power,display);
}
int Red::load_red(FILE* fptr){
if(!load(fptr)) return 0;
if(fscanf(fptr,",%lf,%lf,%d,%d\n",&time_of_last_red,
&time_of_last_power_up,&power,&display))
return 1;
return 0;
}
void Red::load_config(FILE* fptr){
fscanf(fptr,"Punkty za czerwone: %d\n",&points_for_red);
fscanf(fptr,"Czestotliwosc bonusow: %d\n",&frequency_of_power_up);
}