-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (125 loc) · 3.43 KB
/
Copy pathmain.cpp
File metadata and controls
139 lines (125 loc) · 3.43 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
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
#include <raylib.h>
#include<iostream>
#include<cmath>
using namespace std;
int screenwidth=900,screenheight=900;
Color Ball_Color ={232,188,232,100};
Color ayush = {74,72,74,209};
Color Red = {55,106,130,255};
class rectangle{ //Rectangle
public:
float x,y,len,bre,speed;
void draw(){
DrawRectangle(x,y,len,bre,ayush);
}
void update1(){
if(IsKeyDown(KEY_W)){
y = y-speed;
}
if(IsKeyDown(KEY_S)){
y = y+speed;
}
if(y <= 0){
y = 0;
}
if(y >=screenheight-bre){
y = screenheight-bre;
}
}
void update2(){
if(IsKeyDown(KEY_E)){
y = y-speed;
}
if(IsKeyDown(KEY_D)){
y = y+speed;
}
if(y <= 0){
y = 0;
}
if(y >=screenheight-bre){
y = screenheight-bre;
}
}
};
rectangle player1;
rectangle player2;
class ball{
private:
int radius=20;
float speedx = 2.06,speedy=4.03;
float ball_pos_x=screenwidth/2;
float ball_pos_y=screenheight/2;
public:
int scorex=0;
int scorey=0;
void ballx(){
DrawCircle(ball_pos_x,ball_pos_y,radius,WHITE);
}
void update(){
ball_pos_x = ball_pos_x+speedx;
ball_pos_y = ball_pos_y+speedy;
if(ball_pos_y + radius >= screenheight||ball_pos_y-radius <= 0){
speedy *= -1;
}
if(ball_pos_x-radius <= 0){
reset();
speedx = speedx*-1;
scorey++;
}
if(ball_pos_x + radius >= screenwidth){
reset();
scorex++;
speedx = speedx*-1;
}
if(CheckCollisionCircleRec(Vector2{ball_pos_x,ball_pos_y},radius,Rectangle{player1.x,player1.y,player1.len,player1.bre})){
speedx *= -1;
speedx = fabs(speedx);
}
if(CheckCollisionCircleRec(Vector2{ball_pos_x,ball_pos_y},radius,Rectangle{player2.x,player2.y,player2.len,player2.bre})){
speedx *= -1;
speedx = -fabs(speedx);
}
}
void reset(){
ball_pos_x = screenwidth/2;
ball_pos_y = screenheight/2;
speedx *= -1;
}
};
int main(){
// cout<<"Enter screen_width and Height";
// cin>>screenwidth;
// cin>>screenheight;
ball Ballxy;
player1.x = 10;
player1.bre=150;
player1.y = screenheight/2-(player1.bre/2);
player1.len = 30;
player1.speed=5;
player2.x = screenwidth - 40;
player2.bre=150;
player2.y = screenheight/2-(player2.bre/2);
player2.len = 30;
player2.speed=5;
InitWindow(screenwidth,screenheight,"Cpp");
SetTargetFPS(100);
while(WindowShouldClose() == false){
BeginDrawing();
ClearBackground(Red);
DrawCircle(screenwidth/2,screenheight/2,screenheight/10,Ball_Color);
Ballxy.ballx();
Ballxy.update();
DrawText(TextFormat("Score:%d",Ballxy.scorex),10,10,40,WHITE);
DrawText(TextFormat("Score:%d",Ballxy.scorey),screenwidth-250,10,40,WHITE);
player1.draw();
player1.update1();
player2.draw();
player2.update2();
// Ballxy.bounce();
DrawLine(screenwidth/2,0,screenwidth/2,screenheight,WHITE);
// DrawRectangle(10,10,screenheight-20,screenwidth-20,BLACK);
EndDrawing();
}
CloseWindow();
return 0;
}