-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePlay.c
executable file
·118 lines (98 loc) · 3.98 KB
/
GamePlay.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
/**
Snake-Game:
A remake of the mobile game from nokia(don't know what model)
Made by Patrick Farias Bassut Souza <[email protected]
File:
Description:
**/
#include "main.h"
void game_play()
{
init_game();
init_settings();
fadeout(50, 256);
BITMAP *snake_head[4] = {
(BITMAP*)img_datafile[HEAD].dat,
create_bitmap(32, 32),
create_bitmap(32, 32),
create_bitmap(32, 32)
};
rectfill(snake_head[1], 0, 0, snake_head[1]->w, snake_head[1]->h, PINK);
rectfill(snake_head[2], 0, 0, snake_head[1]->w, snake_head[1]->h, PINK);
rectfill(snake_head[3], 0, 0, snake_head[1]->w, snake_head[1]->h, PINK);
BITMAP *tail = (BITMAP*)img_datafile[TAIL].dat;
BITMAP *food_image = (BITMAP*)img_datafile[NORMAL_FOOD].dat;
BITMAP *background = (BITMAP*)img_datafile[BACKGROUND].dat;
BITMAP *buffer = create_bitmap(800, 600);
register int i = 0;
for(i = 1; i < 4; i++)
rotate_sprite(snake_head[i], snake_head[0], 0, 0, itofix(64*i));
fadein(background, 50, 256);
clock_t seconds = clock();
while (!key[KEY_ESC])
{
#ifdef DEBUG
if(key[KEY_ASTERISK]) player.tails_number += 1;
if(key[KEY_MINUS_PAD]) player.speed+=100;
if(key[KEY_PLUS_PAD]) if(player.speed!=100) player.speed-=100;
#endif
if((key[KEY_UP]) && (player.direction!=SOUTH))
player.direction = NORTH;
if((key[KEY_DOWN]) && (player.direction!=NORTH))
player.direction = SOUTH;
if((key[KEY_LEFT]) && (player.direction!=EAST))
player.direction = WEST;
if((key[KEY_RIGHT]) && (player.direction!=WEST))
player.direction = EAST;
if(key[KEY_P])
{
fadeout(50, 256);
textprintf_centre(screen, font, SCREEN_W/2, SCREEN_H/2, WHITE, "You have paused the game. Press 'P' again to continue.");
while(!key[KEY_P]);
fadein(buffer, 50, 256),
game.is_paused = false;
}
draw_sprite(buffer, background, 0, 0);
if(((double)(clock() - seconds) / CLOCKS_PER_SEC) < player.speed)
continue;
seconds = clock();
player.prev_snake_pos_x[0] = player.pos.x;
player.prev_snake_pos_y[0] = player.pos.y;
for(i = player.tails_number; i >= 1; i--)
{
player.prev_snake_pos_x[i] = player.prev_snake_pos_x[i - 1];
player.prev_snake_pos_y[i] = player.prev_snake_pos_y[i - 1];
}
if(check_collision(FOOD_COLLISION))
food_image = new_food();
for(i = 0; i <= player.tails_number; i++)
draw_sprite(buffer, tail, player.prev_snake_pos_x[i], player.prev_snake_pos_y[i]);
draw_sprite(buffer, food_image, game.food.x, game.food.y);
if(player.direction == NORTH)
{
player.pos.y -= 33;
draw_sprite(buffer, snake_head[NORTH], player.pos.x, player.pos.y);
}
else if(player.direction == SOUTH)
{
player.pos.y += 33;
draw_sprite(buffer, snake_head[SOUTH], player.pos.x, player.pos.y);
}
else if(player.direction == EAST)
{
player.pos.x += 33;
draw_sprite(buffer, snake_head[EAST], player.pos.x, player.pos.y);
}
else if(player.direction == WEST)
{
player.pos.x -= 33;
draw_sprite(buffer, snake_head[WEST], player.pos.x, player.pos.y);
}
if(check_collision(WALL_COLLISION) || check_collision(ITSELF_COLLISION))
{
snake_crashed(game.points);
return;
}
draw_sprite(screen, buffer, 0, 0);
}
}