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
|
#include "raylib.h"
#include <string>
#define SCREEN_WIDTH (400)
#define SCREEN_HEIGHT (600)
#define WINDOW_TITLE "Hello World"
#define PLAYER_SIZE 50
#define BAR_WIDTH 50
#define BAR_GAP 250
int
main(void)
{
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, WINDOW_TITLE);
SetTargetFPS(60);
int score = 0;
float player_y = 20;
float player_velocity = 0;
float bar_x = SCREEN_WIDTH;
float bar_y = GetRandomValue(BAR_GAP + 50, SCREEN_HEIGHT - 50);
Rectangle player_rec;
Rectangle upper_bar_rec;
Rectangle lower_bar_rec;
while (!WindowShouldClose())
{
player_rec = {
(SCREEN_WIDTH - PLAYER_SIZE) / 2.0,
player_y,
PLAYER_SIZE,
PLAYER_SIZE};
upper_bar_rec = { bar_x, 0, BAR_WIDTH, bar_y - BAR_GAP };
lower_bar_rec = {bar_x, bar_y, BAR_WIDTH, SCREEN_HEIGHT - bar_y};
player_velocity += 1;
bar_x -= 2;
if (IsKeyPressed(KEY_SPACE))
{
player_velocity = -15;
}
player_y += player_velocity;
if (player_y < 0 || player_y > SCREEN_HEIGHT)
score = 0;
if (CheckCollisionRecs(player_rec, upper_bar_rec)
|| CheckCollisionRecs(player_rec, lower_bar_rec))
score = 0;
if (bar_x < -40)
{
bar_x = SCREEN_WIDTH;
bar_y = GetRandomValue(BAR_GAP + 50, SCREEN_HEIGHT - 50);
score++;
}
BeginDrawing();
ClearBackground(BLUE);
DrawRectangleRec(upper_bar_rec, GOLD);
DrawRectangleRec(lower_bar_rec, GOLD);
DrawText(std::to_string(score).c_str(),
(SCREEN_WIDTH / 2) - 25, 50, 50, RAYWHITE);
DrawRectangleRec(player_rec, BLACK);
EndDrawing();
}
CloseWindow();
return 0;
}
|