-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaddle.cpp
More file actions
executable file
·63 lines (49 loc) · 1.94 KB
/
Paddle.cpp
File metadata and controls
executable file
·63 lines (49 loc) · 1.94 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
//Headers to include
#include "Paddle.h"
Paddle::Paddle(SDL_Renderer* renderer, bool isRightPaddleSet, bool isAiSet)
{
isRightPaddle = isRightPaddleSet;
isAi = isAiSet;
rendererInstance = renderer;
width = height / (paddleImgSrcRect.h / paddleImgSrcRect.w); //Calculates the width of the paddle using the height and size of the texture, so that the texture isn't stretched
position.x = position.y = 0;
speed = 5.f;
edgeOffset = 25.f;
if (isRightPaddle)
{
position.x = SCREEN_WIDTH - width - edgeOffset;
position.y = (SCREEN_HEIGHT / 2) - (height / 2);
}
else
{
position.x = 0 + edgeOffset;
position.y = (SCREEN_HEIGHT / 2) - (height / 2);
}
paddleRect.x = (int)position.x; paddleRect.y = (int)position.y;
paddleRect.w = width; paddleRect.h = height;
testThing.x = position.x;
testThing.y = 0;
testThing.w = paddleRect.w;
testThing.h = SCREEN_HEIGHT;
paddleTexture = SDL_CreateTextureFromSurface(rendererInstance, paddleTempSurface);
if (!paddleTexture) std::cerr << "couldn't load paddle textrue";
SDL_FreeSurface(paddleTempSurface);
}
void Paddle::Update(float frameSpeed)
{
//Y vel is subtracted because position is from top right, and positive moving up is more common and makes more sense
if (!isAi)
position.y -= (speed * direction) * frameSpeed;
//Old code to stop paddles from going off screen
/*if (position.y < 0)
position.y = 0;
else if (position.y + height > SCREEN_HEIGHT)
position.y = SCREEN_HEIGHT - height;*/
paddleRect.x = position.x; paddleRect.y = position.y;
}
void Paddle::SetDirection(int directionSet)
{
//If statement only moves the paddles if the movement won't cause them to go off screen. More efficient than checking if the paddle is off screen and setting it to be back on screen
if(directionSet == 0 || (directionSet > 0 && position.y > 0) || (directionSet < 0 && position.y < SCREEN_HEIGHT - height))
direction = directionSet;
}