-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroll.c
112 lines (92 loc) · 2.62 KB
/
roll.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
#include <stdbool.h>
#include <stdio.h>
#include <raylib.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#define GRID_SIZE 20
#define screenWidth 800
#define screenHeight 460
#define windowWidth 800
#define windowHeight 550
int main(void) {
InitWindow(windowWidth, windowHeight, "Rock and Roll");
SetTargetFPS(10);
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image worm;
Texture2D texture;
worm = LoadImage("textures/worm1.png");
ImageFlipHorizontal(&worm);
ImageResize(&worm, 6*GRID_SIZE, GRID_SIZE);
texture = LoadTextureFromImage(worm);
UnloadImage(worm);
int pos[6] = {0};
// int tex_index[6] = {0};
bool iswin = false;
int winner = 0;
char dice[5] = "";
int current = -1;
int rollDuration = 20;
int rollCounter = 20;
while (!WindowShouldClose()) {
for(int i=0;i<6;i++) {
if (pos[i] + 6*GRID_SIZE >= (screenWidth-90)) {
iswin = true;
winner = i+1;
}
}
// Update
if (rollCounter < rollDuration) {
// Simulate dice roll animation by updating dice face frequently
current = (GetRandomValue(1, 6) - 1);
rollCounter++;
} else {
sprintf(dice,"%d", current);
if (IsKeyPressed(KEY_SPACE)) {
pos[current] += 50;
rollCounter = 0;
}
}
// Draw
BeginDrawing();
ClearBackground(WHITE);
if (!iswin) {
if (rollCounter < rollDuration) {
// Display the rolling animation
DrawText(TextFormat("Rolling...%d",current), windowWidth / 2 - 80, windowHeight - 60, 30, BLACK);
}else {
sprintf(dice, "%d",current + 1);
DrawText(dice, windowWidth / 2 - 40, windowHeight - 60, 60, DARKGRAY);
}
DrawRectangle(screenWidth-90, 0, 60, screenHeight, DARKGRAY);
for(int i=0;i<6;i++) {
DrawTexture(texture, pos[i], 80*i+10, WHITE);
char num[4];
sprintf(num, "%d", i+1);
DrawText(num,screenWidth-70,80*i+10,30,RAYWHITE);
}
} else {
char msg[50];
sprintf(msg, "Player %d wins", winner);
DrawText(msg,windowWidth / 2 - 100, windowHeight / 2 - 30, 30, GRAY);
if(IsKeyPressed(KEY_SPACE)) {
for(int i = 0;i<6;i++) {
pos[i] = 0;
// tex_index[i] = 0;
}
current = -1;
iswin = false;
winner = 0;
rollCounter = 20;
sprintf(dice,"0");
};
}
EndDrawing();
}
UnloadTexture(texture);
// De-allocate resources
CloseWindow();
return 0;
}