-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfood.cpp
More file actions
77 lines (72 loc) · 1.56 KB
/
food.cpp
File metadata and controls
77 lines (72 loc) · 1.56 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
#include "food.h"
#include <cstddef>
#include <cstdio>
Food::Food() : last_animation(0),state(0){
food_bmp = SDL_LoadBMP("./bitmaps/nieb.bmp");
big_food_bmp = SDL_LoadBMP("./bitmaps/big_nieb.bmp");
food_now = food_bmp;
}
void Food::set_window_size(int width,int height){
screen_width = width;
screen_height = height;
}
void Food::DrawFood(SDL_Surface* screen,double delta){
last_animation+=delta;
if (last_animation >=time_between_food_animations){
last_animation = 0;
if (state == 0){
food_now = big_food_bmp;
state = 1;
}
else if (state == 1) {
food_now = food_bmp;
state = 0;
}
}
int w,h;
SDL_Rect dest;
w = food_now->w;
h = food_now->h;
dest.x = x - w / 2;
dest.y = y - h / 2;
dest.w = w;
dest.h = h;
SDL_BlitSurface(food_now, NULL, screen, &dest);
}
int Food::check_if_eaten(Snake snake){
if (snake.posx == x && snake.posy == y){
return 1;
}
return 0;
}
int Food::position_food(Snake snake,SDL_Surface* screen){
int pos_ok = 1;
int tries = 0;
int positions_x = screen_width/24;
int positions_y = screen_height/24;
int snakex,snakey;
do{
tries++;
pos_ok = 1;
x = rand()%(positions_x-1);
y = rand()%(positions_y-2)+1;
x = x*24 + 12;
y = y*24 + 36;
for (int i = 0;i<snake.length;i++){
snakex = snake.snake_parts[i].x;
snakey = snake.snake_parts[i].y;
if (snakex == x && snakey == y) pos_ok = 0;
}
}while(!pos_ok && tries <1000);
if (pos_ok) return 1;
return 0;
}
void Food::save(FILE* fptr){
fprintf(fptr,"%d,%d",x,y);
}
int Food::load(FILE* fptr){
if(fscanf(fptr,"%d,%d",&x,&y)){
return 1;
}
return 0;
}