-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathExample_-_Breadcrumb.c
131 lines (111 loc) · 4.22 KB
/
Example_-_Breadcrumb.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
#define MAX_PATH 32
#include "raylib.h"
#include <math.h>
static int mapWidth = 20;
static int mapHeight = 10;
static float tileWidth;
static float tileHeight;
// This is our tile map. Note that [y][x]!!
int map[10][20] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1},
{1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1},
{1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
typedef struct player{
bool active;
int x;
int y;
int pathx[MAX_PATH];
int pathy[MAX_PATH];
}player;
static player p;
static void dropbreadcrumb(void);
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib example.");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
p.active = true;
p.x = 5;
p.y = 5;
for(int i=MAX_PATH-1;i>-1;i-=1){
p.pathx[i]=-1;
p.pathy[i]=-1;
}
dropbreadcrumb();
tileWidth = abs((float)screenWidth/(float)mapWidth);
tileHeight = abs((float)screenHeight/(float)mapHeight);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if(IsKeyReleased(KEY_UP) && map[p.y-1][p.x]==0){
p.y-=1;
dropbreadcrumb();
}
if(IsKeyReleased(KEY_DOWN) && map[p.y+1][p.x]==0){
p.y+=1;
dropbreadcrumb();
}
if(IsKeyReleased(KEY_LEFT) && map[p.y][p.x-1]==0){
p.x-=1;
dropbreadcrumb();
}
if(IsKeyReleased(KEY_RIGHT) && map[p.y][p.x+1]==0){
p.x+=1;
dropbreadcrumb();
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
//
// Draw our tilemap.
for(int y=0;y<10;y++){
for(int x=0;x<20;x++){
if(map[y][x]==1){
DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLACK);
}
}
}
// Draw our breadcrumb
for(int i=0;i<MAX_PATH;i++){
if(p.pathx[i]>-1)
DrawRectangle( p.pathx[i]*tileWidth+tileWidth/4,p.pathy[i]*tileHeight+tileHeight/4,
tileWidth-tileWidth/2,tileHeight-tileHeight/2,(Color){50,50,50,255-(255.0f/32.0f)*i});
}
// Draw our player
DrawRectangle(p.x*tileWidth,p.y*tileHeight,tileWidth,tileHeight,BLUE);
//
//
DrawText("BreadCrumb Example. Use cursor keys to move around.",0,0,20,GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
void dropbreadcrumb(){
// first shift the path one step down.
for(int i=MAX_PATH-1;i>0;i--){
p.pathx[i] = p.pathx[i-1];
p.pathy[i] = p.pathy[i-1];
}
// insert the current position to the top of the path array.
p.pathx[0] = p.x;
p.pathy[0] = p.y;
}