-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cpp
More file actions
65 lines (53 loc) · 1.4 KB
/
Ball.cpp
File metadata and controls
65 lines (53 loc) · 1.4 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
#include "Ball.h"
#include <cstdlib>
#include <iostream>
const int SIZE = 10;
const int SPEED = 5;
Ball::Ball(int screenWidth, int screenHeight) : SCREEN_WIDTH(screenWidth),
SCREEN_HEIGHT(screenHeight) {}
void Ball::innit() {
//Set the Y and X axis of the ball to the middle of the screen
ball.h = SIZE;
ball.w = SIZE;
ball.y = (SCREEN_HEIGHT / 2) - SIZE;
ball.x = (SCREEN_WIDTH / 2) - SIZE;
//randomize the velocity of the ball
int lb = 0, ub = 5;
srand(time(0));
int rand = (std::rand() & (ub - lb)) + lb;
switch (rand) {
case 0:
velocity.y = SPEED;
velocity.x = SPEED;
break;
case 1:
velocity.y = -SPEED;
velocity.x = -SPEED;
break;
case 2:
velocity.y = SPEED;
velocity.x = -SPEED;
break;
case 3:
velocity.y = -SPEED;
velocity.x = SPEED;
break;
case 4:
velocity.y = SPEED/2;
velocity.x = SPEED;
break;
case 5:
velocity.y = SPEED/2;
velocity.x = -SPEED;
default:
break;
}
}
void Ball::render(SDL_Renderer *renderer) {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(renderer, &ball);
}
void Ball::update() {
ball.y += velocity.y;
ball.x += velocity.x;
}