-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation_using_pixel.cpp
More file actions
80 lines (63 loc) · 1.91 KB
/
Copy pathAnimation_using_pixel.cpp
File metadata and controls
80 lines (63 loc) · 1.91 KB
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
// Star field simulation
#include <raylib.h>
#include <stdlib.h> // For rand() and srand()
#include <time.h> // For time()
#define MAX_STARS 9999
typedef struct Star
{
float x, y;
float speed;
} Star;
void InitStars(Star stars[], int count, int screenWidth, int screenHeight)
{
// Setting the x and y position and speed of the each pixels
for (int i = 0; i < count; i++)
{
stars[i].x = (float)(rand() % screenWidth); // Random horizontal position
stars[i].y = (float)(rand() % screenHeight); // Random vertical position
stars[i].speed = (float)(rand() % 5 + 1); // Random Speed ( up to 5 )
}
}
// Update the star's Positions
void UpdateStars(Star stars[], int count, int screenWidth, int screenHeight)
{
for (int i = 0; i < count; i++)
{
stars[i].y += stars[i].speed; // Move the pixel down
// If any star gone off the screen at downSide
if (stars[i].y > screenHeight)
{
stars[i].x = (float)(rand() % screenWidth);
stars[i].y = 0; // Reset to the upper sode
stars[i].speed = (float)(rand() % 5 + 1);
}
}
}
void DrawStars(Star stars[], int count)
{
// Draw each pixels throw the for loop
for (int i = 0; i < count; i++)
{
DrawPixel((int)stars[i].x, (int)stars[i].y, ORANGE);
}
}
int main()
{
int screenWidth = 300;
int screenHeight = 300;
InitWindow(screenWidth, screenHeight, "Starfield Simulation");
SetTargetFPS(120);
srand(time(NULL)); // Seed the random number generator
Star stars[MAX_STARS]; // Making stars
InitStars(stars, MAX_STARS, screenWidth, screenHeight);
while (!WindowShouldClose())
{
UpdateStars(stars, MAX_STARS, screenWidth, screenHeight);
BeginDrawing();
ClearBackground(BLACK);
DrawStars(stars, MAX_STARS);
EndDrawing();
}
CloseWindow();
return 0;
}