-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
55 lines (43 loc) · 1.42 KB
/
Player.cpp
File metadata and controls
55 lines (43 loc) · 1.42 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
#include "Player.h"
const int PLAYER_MOVEMENT_SPEED = 4;
Player::Player(int screenWidth, int screenHeight) : isMoving(false),SCREEN_WIDTH(screenWidth), SCREEN_HEIGHT(screenHeight){
velocity.x = 0;
velocity.y = 0;
}
void Player::render(SDL_Renderer *renderer) {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(renderer, &paddle);
}
void Player::innit(bool isPlayer1) {
if (!isPlayer1)
//Set the X axis of the paddle to the left of the screen
paddle.x = SCREEN_WIDTH - 20;
else
//Set the X axis of the paddle to the right of the screen;
paddle.x = 0;
//Set the Y axis of the paddle to the middle of the screen
paddle.y = (SCREEN_HEIGHT / 2) - 50;
paddle.w = 20;
paddle.h = 100;
}
void Player::update(){
if(isMoving){
// Update the player's position based on velocity and deltaTime
paddle.y += velocity.y;
// Add boundary checks to ensure the paddle stays within the screen
if (paddle.y < 0) {
paddle.y = 0;
} else if (paddle.y + paddle.h > SCREEN_HEIGHT) {
paddle.y = SCREEN_HEIGHT - paddle.h;
}
}
}
void Player::move(bool up) {
isMoving = true;
//change the velocity of the paddle
velocity.y = up ? -PLAYER_MOVEMENT_SPEED : PLAYER_MOVEMENT_SPEED;
}
void Player::stopMoving() {
this->isMoving = false;
velocity.x = 0; // Stop vertical movement
}