-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.cpp
103 lines (84 loc) · 2.39 KB
/
ball.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
#include "ball.h"
SDL_Surface *paddleSprite = NULL ;
SDL_Surface *leftPaddle = NULL ;
SDL_Surface *ball = NULL ;
SDL_Surface *screen = NULL ;
SDL_Surface *button = NULL ;
SDL_Surface *button2 = NULL ;
SDL_Surface *score1 = NULL ;
SDL_Surface *score2 = NULL;
SDL_Surface *winningMessage = NULL ;
Ball::Ball()
{
x = SCREEN_WIDTH/2;
y = SCREEN_HEIGHT/2;
xVelocity = 300;
yVelocity = 150;
Player1Score = 0;
Player2Score = 0;
}
void Ball::move(float xLeftPaddle, float yLeftPaddle,float xpaddleSprite, float ypaddleSprite, Uint32 deltaTicks)
{
y += yVelocity * ( deltaTicks / 1000.f );
x += xVelocity * ( deltaTicks / 1000.f );
if( y + BALL_HEIGHT > SCREEN_HEIGHT )//Bottom wall
{
Mix_PlayChannel(-1,collisionSound, 0);
x = x-1;
y = y-1;
yVelocity = -yVelocity;
}
else if( y < 0 )//Top wall
{
Mix_PlayChannel(-1,collisionSound, 0);
x = x+1;
y = y+1;
yVelocity = -yVelocity ;
}
else if(x + BALL_WIDTH >= xpaddleSprite && y <= ypaddleSprite + PADDLE_HEIGHT && y >=ypaddleSprite && x < xpaddleSprite + PADDLE_WIDTH )//Right Paddle collision
{
x = x-1;
y = y-1;
xVelocity = -xVelocity;
Mix_PlayChannel(-1,collisionSound, 0);
if( y + BALL_WIDTH < ypaddleSprite + (PADDLE_HEIGHT)/2 && yVelocity > 0)
{
yVelocity = -yVelocity;
}
if( y + BALL_WIDTH >= ypaddleSprite + (PADDLE_HEIGHT)/2 && yVelocity < 0)
{
yVelocity = -yVelocity;
}
}
else if( x <= xLeftPaddle + PADDLE_WIDTH && y <= yLeftPaddle + PADDLE_HEIGHT && y >= yLeftPaddle && x > xLeftPaddle )//Left Paddle collision
{
x = x + 1;
y = y + 1;
Mix_PlayChannel(-1,collisionSound, 0);
xVelocity = -xVelocity;
if( y < yLeftPaddle + (PADDLE_HEIGHT)/2 && yVelocity > 0)
{
yVelocity = -yVelocity;
}
if( y >= yLeftPaddle + (PADDLE_HEIGHT)/2 && yVelocity < 0)
{
yVelocity = -yVelocity;
}
}
else if(x < 0 )//Reset position if out of bounds
{
Player2Score++;
x = SCREEN_WIDTH/2;
y = SCREEN_HEIGHT/2;
}
else if(x > SCREEN_WIDTH)
{
Player1Score++;
x = SCREEN_WIDTH/2;
y = SCREEN_HEIGHT/2;
}
}
void Ball::show()
{
apply_surface( (int)x, (int)y, ball, screen );
}