-
Notifications
You must be signed in to change notification settings - Fork 7
/
Example_-_2D_ShootBullets.c
131 lines (106 loc) · 4.16 KB
/
Example_-_2D_ShootBullets.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
#include "raylib.h"
#include <stdlib.h>
// math.h is needed for cos and sin.
#include <math.h>
#define MAX_BULLETS 100
#define MAX_BATCH_ELEMENTS 8192
typedef struct Bullets{
Vector2 position;
bool active;
float angle;
} Bullets;
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib example.");
Bullets *mybullets = (Bullets *)malloc(MAX_BULLETS*sizeof(Bullets)); // Bullets array
mybullets[0].active=false;
// Create a Image in memory
RenderTexture2D target = LoadRenderTexture(32, 32);
int sprite[8][8] = {
{0,2,1,0,0,1,2,0},
{2,1,0,2,2,0,1,2},
{1,0,2,1,1,2,0,1},
{0,2,1,1,1,1,2,0},
{0,2,1,1,1,1,2,0},
{1,0,2,1,1,2,0,1},
{2,1,0,2,2,0,1,2},
{0,2,1,0,0,1,2,0}};
// Clear our texture(image) before entering the game loop
BeginTextureMode(target);
ClearBackground(BLANK); // Make the entire Sprite Transparent.
EndTextureMode();
// Draw something on it.
for (int y=0;y<8;y++)
{
for (int x=0;x<8; x++)
{
// Our sprite color 1
if (sprite[y][x]==1)
{
BeginTextureMode(target);
DrawRectangle(x*4,y*4,4,4,WHITE);
EndTextureMode();
}
// Our sprite color 2
if (sprite[y][x]==2)
{
BeginTextureMode(target);
DrawRectangle(x*4,y*4,4,4,RED);
EndTextureMode();
}
}
}
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Update the bullets
// Deactivate if outside window.
for(int i=0;i<MAX_BULLETS;i++){
if(mybullets[i].active){
mybullets[i].position.x+=(float)cos(mybullets[i].angle)*3;
mybullets[i].position.y+=(float)sin(mybullets[i].angle)*3;
if(mybullets[i].position.x<-50 || mybullets[i].position.x>screenWidth+50 || mybullets[i].position.y<-50 || mybullets[i].position.y>screenHeight+50){
mybullets[i].active = false;
}
}
}
// Shoot a bullet
if(IsMouseButtonReleased(0)){
for(int i=0;i<MAX_BULLETS;i++){
if(mybullets[i].active==false){
mybullets[i].active=true;
mybullets[i].angle = GetRandomValue(0,359);
mybullets[i].position = GetMousePosition();
break;
}
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Press the left Mouse Button to Shoot button from mouse Pos.",0,100,20,BLACK);
for(int i=0;i<MAX_BULLETS;i++){
if(mybullets[i].active){
DrawTexture(target.texture,mybullets[i].position.x,mybullets[i].position.y,WHITE);
}}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
free(mybullets); // Unload points data array
UnloadRenderTexture(target); // Unload render texture
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}