-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.h
More file actions
144 lines (117 loc) · 3.11 KB
/
Copy pathplayer.h
File metadata and controls
144 lines (117 loc) · 3.11 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
140
141
142
143
144
#ifndef PLAYER_
#define PLAYER_H
#include "raylib.h"
class Ball{
public:
int cpu_score = 0;
int player_score = 0;
float x, y;
int speedx, speedy;
int radius;
Ball(float, float, int, int, int);
void draw(){
DrawCircle(x, y, radius, RAYWHITE);
}
bool update(){
x = x+speedx;
y = y+speedy;
if(x<=0){
x = 400.0;
y = 225.0;
speedy = 0;
speedx= -7;
cpu_score ++;
return true;
}
if (x >= GetScreenWidth()){
x = 400.0;
y = 225.0;
speedy = 0;
speedx= 7;
player_score++;
return true;
}
if(y+radius >= GetScreenHeight() || y-radius <= 0){
speedy *= -1;
}
return false;
}
void directionAfterCollisionWithPlayer(int player_y, int add_speedx){
float collision_point = y - player_y;
if(collision_point <= 40 ){
if(collision_point <= 10)
{
speedy = +7;
speedx += add_speedx;
} else
{
speedy = +5;
}
}
if((collision_point <= 80) && (y - player_y > 40)){
speedy = GetRandomValue(-2, 2);
}
if((collision_point >= 80) && (y - player_y <= 120)){
if(collision_point >= 110)
{
speedy = -7;
speedx += add_speedx;
} else
{
speedy = -5;
}
}
}
};
class Player{
public:
float x, y;
float width, height;
int speed;
Player(float, float, int, float, float);
void draw(){
DrawRectangle(x, y, width, height, WHITE);
}
void update(){
if(IsKeyDown(KEY_UP) && y>0){
y = y - speed;
}
if(IsKeyDown(KEY_DOWN) && y+height<GetScreenHeight()){
y = y + speed;
}
}
void restart(){
y = (GetScreenHeight()/2 - height/2);
};
};
class Cpu: public Player{
public:
Cpu(float, float, int, float, float);
void move(float ball_y, float ball_x, int screen_width, int screen_height, int paddle_height, int cpuReactionTime){
if(ball_x>cpuReactionTime){
if((y+60 > ball_y) && (y>0)){
y = y - speed;
}
if ((y+60 < ball_y) && (y+paddle_height<screen_height)){
y = y + speed;
}
}
}
};
// CONSTRUCTORES
Ball::Ball(float _x, float _y, int _radius, int _speedx, int _speedy){
x = _x;
y = _y;
radius = _radius;
speedx = _speedx;
speedy = _speedy;
}
Player::Player(float _x, float _y, int _speed, float _width, float _height){
x = _x;
y = _y;
speed = _speed;
width = _width;
height = _height;
}
Cpu::Cpu(float _x, float _y, int _speed, float _width, float _height): Player(_x, _y, _speed, _width, _height){}
#endif