-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
136 lines (117 loc) · 4.24 KB
/
main.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
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
#include <stdio.h>
#include <stdbool.h>
#include "globals.h"
int DEFAULT_WINDOW_WIDTH = 800;
int DEFAULT_WINDOW_HEIGHT = 600;
int SNAKE_SIZE = 50;
int SNAKE_R = 255;
int SNAKE_G = 255;
int SNAKE_B = 255;
int SNAKE_A = 255;
#include "sdl_cat.h"
#include "snake_logic.h"
int main(int argc, char *argv[])
{
srand(time(NULL));
int x_cells = DEFAULT_WINDOW_WIDTH / SNAKE_SIZE - 1;
int y_cells = DEFAULT_WINDOW_HEIGHT / SNAKE_SIZE - 1;
snake *head = forge_node(rand_range(0, x_cells), rand_range(0, y_cells));
snake *tail = head;
snake *snake_food = forge_node(rand_range(0, x_cells), rand_range(0, y_cells));
SDL_Window *main_window = initMainWindow();
if (main_window == NULL)
{
quit(NULL);
return EXIT_FAILURE;
}
SDL_Renderer *main_window_renderer = SDL_CreateRenderer(main_window, -1, SDL_RENDERER_ACCELERATED);
if (main_window_renderer == NULL)
{
printf("Renderer could not be created!\n");
quit(main_window);
return EXIT_FAILURE;
}
SDL_Event event;
int score = 0;
int is_alive = 1;
int x = 150, y = 150;
int vx = 1, vy = 0;
grow_snake(&tail, vx, vy);
grow_snake(&tail, vx, vy);
// grow_snake(&tail, vx, vy);
clock_t start = 0;
clock_t end;
while (is_alive)
{
SDL_SetRenderDrawColor(main_window_renderer, 0, 0, 0, 10);
SDL_RenderClear(main_window_renderer);
// SDL_RenderPresent(main_window_renderer);
int xcount = DEFAULT_WINDOW_WIDTH / SNAKE_SIZE, ycount = DEFAULT_WINDOW_HEIGHT / SNAKE_SIZE;
for (int i = 0; i < xcount; i++)
lineRGBA(main_window_renderer, i * SNAKE_SIZE, 0, i * SNAKE_SIZE, DEFAULT_WINDOW_HEIGHT, 200, 200, 0, 255);
for (int i = 0; i < ycount; i++)
lineRGBA(main_window_renderer, 0, i * SNAKE_SIZE, DEFAULT_WINDOW_WIDTH, i * SNAKE_SIZE, 200, 200, 0, 255);
// grow_snake(&tail, vx, vy);
// filledCircleRGBA(main_window_renderer, 300, 300, 75, 255, 0, 0, 255);
end = clock();
start = why_move_snake(start, end);
if (!start)
move_snake(&head, &tail, vx, vy);
check_boundary(head, &vx, &vy, x_cells, y_cells);
is_alive = check_colison(head);
eat_if_food(head, &tail, &snake_food, &score);
snake *t = head;
boxRGBA(main_window_renderer, SNAKE_SIZE * t->x, SNAKE_SIZE * t->y, SNAKE_SIZE * t->x + SNAKE_SIZE, SNAKE_SIZE * t->y + SNAKE_SIZE, 255, 0, 0, SNAKE_A);
t=t->next;
while (t)
{
boxRGBA(main_window_renderer, SNAKE_SIZE * t->x, SNAKE_SIZE * t->y, SNAKE_SIZE * t->x + SNAKE_SIZE, SNAKE_SIZE * t->y + SNAKE_SIZE, 0, 255, 0, SNAKE_A);
t = t->next;
}
boxRGBA(main_window_renderer, SNAKE_SIZE * snake_food->x, SNAKE_SIZE * snake_food->y, SNAKE_SIZE * snake_food->x + SNAKE_SIZE, SNAKE_SIZE * snake_food->y + SNAKE_SIZE, snake_food->r, snake_food->g, snake_food->b, SNAKE_A);
SDL_RenderPresent(main_window_renderer);
SDL_Delay(50);
SDL_WaitEventTimeout(&event, 1);
if (event.type == SDL_QUIT)
is_alive = 0;
else if (event.type == SDL_KEYDOWN)
{
// printf("%d\n", event.key.keysym.sym);
if (event.key.keysym.sym == SDLK_ESCAPE)
{
printf("ESC\n");
is_alive = 0;
}
if (event.key.keysym.sym == SDLK_RIGHT && vx != -1)
{
// printf("RIGHT\n");
vx = 1;
vy = 0;
}
if (event.key.keysym.sym == SDLK_LEFT && vx != 1)
{
vx = -1;
vy = 0;
// printf("LEFT\n");
}
if (event.key.keysym.sym == SDLK_UP && vy != 1)
{
vx = 0;
vy = -1;
// printf("UP\n");
}
if (event.key.keysym.sym == SDLK_DOWN && vy != -1)
{
vx = 0;
vy = 1;
// printf("DOWN\n");
}
// grow_snake(&tail, vx, vy);
}
}
SDL_DestroyRenderer(main_window_renderer);
quit(main_window);
printf("YOUR SCORE: %d\n", score);
printf("probably you died\n:c\n");
return EXIT_SUCCESS;
}