-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
277 lines (231 loc) · 8.48 KB
/
main.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "include/raylib.h"
#include <iostream>
#include <string>
#include <stdlib.h>
const int screenWidth = 1280;
const int screenHeight = 720;
int ball_speed = 10;
// o = right , 1 = left
int ball_direction = 0;
Vector2 ball = {screenWidth/2 , screenHeight/2};
int ball_rad = 10;
int y_angle = 0;
Rectangle ball_hitbox = {ball.x, ball.y, ball_rad *2, ball_rad *2};
struct powerup{
Texture2D texture;
bool spawn = false;
Vector2 pos;
bool collison = false;
std::string name;
};
void selection(powerup& selected)
{
if(selected.spawn == false){
std::cout << "spawn";
selected.spawn = true;
selected.pos.x = rand () %screenWidth;
if (selected.pos.x > screenWidth){
selected.pos.x = screenWidth;
}
selected.pos.y = rand () %screenHeight;
if (selected.pos.y > screenHeight){
selected.pos.y = screenHeight;
}
std::cout << selected.pos.x << selected.pos.y;
std::cout << selected.name;
}
}
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
Image icon = LoadImage("sprites/icon.png");
//audio
InitWindow(screenWidth, screenHeight, "GAME THAT I CANT THINK OF A NAME FOR");
SetWindowIcon(icon);
InitAudioDevice(); // Initialize audio device
//Sound point1 = LoadSound("audio/point.wav");
// start x , start y , width , hieght
int rec_player1_pos_y = screenHeight/2 - 100;
int vertical_speed = 5;
int rectangle_height = 200;
bool wrapping = false;
//ball
//scores
int score = 0;
//ui
int font_size = 50;
//colisons
bool collision1 = false;
//rectangles
Rectangle rec1 = {};
//power ups
int powerup_spawn;
powerup speed;
speed.texture = LoadTexture("sprites/speed.png");
speed.name = "speed";
powerup slow;
slow.texture = LoadTexture("sprites/slow.png");
slow.name = "slow";
powerup paddle_up;
paddle_up.texture = LoadTexture("sprites/paddle_up.png");
paddle_up.name = "paddle_up";
powerup paddle_down;
paddle_down.texture = LoadTexture("sprites/paddle_down.png");
paddle_down.name = "paddle_down";
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
if(wrapping == false){
if (IsKeyDown(KEY_UP)){
if(rec_player1_pos_y <= 0){
}
else{
rec_player1_pos_y -= vertical_speed;
}
}
if (IsKeyDown(KEY_DOWN)){
if(rec_player1_pos_y + rectangle_height >= screenHeight){
}
else{
rec_player1_pos_y += vertical_speed;
}
}
}
//lose
if (ball.x <= 0){
ball = {screenWidth/2 , screenHeight/2};
y_angle = 0;
score = 0;
ball_speed = 10;
vertical_speed = 5;
speed.spawn = false;
slow.spawn = false;
paddle_up.spawn = false;
paddle_down.spawn = false;
}
rec1 = {25, rec_player1_pos_y , 50 ,rectangle_height};
ball_hitbox = {ball.x - ball_rad, ball.y - ball_rad, ball_rad * 2, ball_rad *2};
if (ball.x >= screenWidth){
ball_direction = 0;
//PlaySound(point1);
}
if (ball.y <= 0){
y_angle *= -1;
}
if (ball.y >= screenHeight){
y_angle *= -1;
}
collision1 = CheckCollisionRecs(rec1, ball_hitbox);
if (collision1){
ball_direction = 1;
y_angle = rand() %10 + 1;
y_angle -= 5;
score +=1;
}
//ball movment
if(ball_direction == 0){
ball.x -= ball_speed;
ball.y += y_angle;
}
else{
ball.x += ball_speed;
ball.y += y_angle;
}
powerup_spawn = rand() %2000;
switch (powerup_spawn)
{
case 53:
std::cout << "called";
selection(speed);
break;
case 785:
selection(slow);
std::cout << "called";
case 892:
selection(paddle_up);
std::cout << "called";
case 26:
selection(paddle_down);
std::cout << "called";
default:
break;
}
if(speed.spawn){
speed.collison = CheckCollisionRecs({speed.pos.x,speed.pos.y,50,50}, ball_hitbox);
if(speed.collison){
ball_speed +=3;
std::cout << "hit";
speed.spawn = false;
}
}
if(slow.spawn){
slow.collison = CheckCollisionRecs({slow.pos.x,slow.pos.y,50,50}, ball_hitbox);
if(slow.collison){
ball_speed -=3;
std::cout << "hit";
slow.spawn = false;
}
}
if(paddle_up.collison){
paddle_up.collison = CheckCollisionRecs({paddle_up.pos.x,paddle_up.pos.y,50,50}, ball_hitbox);
if(paddle_up.collison){
vertical_speed += 3;
std::cout << "hit";
paddle_up.spawn = false;
}
}
if(paddle_down.spawn){
paddle_down.collison = CheckCollisionRecs({paddle_down.pos.x,paddle_down.pos.y,50,50}, ball_hitbox);
if(paddle_down.collison){
vertical_speed -= 3;
std::cout << "hit";
paddle_down.spawn = false;
}
}
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BLACK);
//DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
DrawRectangleRec(rec1, WHITE);
//DrawRectangleRec(ball_hitbox,RED);
DrawCircleV(ball, ball_rad , WHITE);
//definatly better way
std::string s = std::to_string(score);
char const *score1 = s.c_str();
std::string t = std::to_string(ball_speed);
char const *ball_speed2 = t.c_str();
std::string z = std::to_string(vertical_speed);
char const *vertical_speed2 = z.c_str();
DrawText(score1, 200 ,50, font_size, WHITE);
DrawText(ball_speed2, 50,50, font_size, WHITE);
DrawText(vertical_speed2, 350,50, font_size, WHITE);
if (speed.spawn){
DrawTexture(speed.texture, speed.pos.x - speed.texture.width/2, speed.pos.y - speed.texture.height/2, WHITE);
}
if (slow.spawn){
DrawTexture(slow.texture, slow.pos.x - slow.texture.width/2, slow.pos.y - slow.texture.height/2, WHITE);
}
if (paddle_up.spawn){
DrawTexture(paddle_up.texture, paddle_up.pos.x - paddle_up.texture.width/2, paddle_up.pos.y - paddle_up.texture.height/2, WHITE);
}
if (paddle_down.spawn){
DrawTexture(paddle_down.texture, paddle_down.pos.x - paddle_down.texture.width/2, paddle_down.pos.y - paddle_down.texture.height/2, WHITE);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
CloseAudioDevice();
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}