-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.java
More file actions
129 lines (114 loc) · 4.79 KB
/
Copy pathBall.java
File metadata and controls
129 lines (114 loc) · 4.79 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This class represents the Ball in the Ping Pong game.
* It handles the movement, collisions, and interactions with other objects.
* @authors (Rupert & Raymond Van Niekerk)
* @student Numbers (222894237 & 221154469)
* @version (1.0)
*/
public class Ball extends Actor {
private int horizontalSpeed = 3; // Speed of the ball horizontally
private int verticalSpeed = 3; // Speed of the ball vertically
private boolean isStuck = true; // If the ball is stuck to the paddle
private Paddle currentPaddle; // The paddle the ball is stuck to
public Ball() {
// Constructor for the ball class
}
public void act() {
if (isStuck) {
// If the ball is stuck to a paddle, check for space key to release it
if (Greenfoot.isKeyDown("space")) {
isStuck = false;
}
else if (currentPaddle != null) {
// Align the ball with the paddle
if (currentPaddle.isPlayerOne) {
setLocation(currentPaddle.getX() + currentPaddle.getImage().getWidth() / 2 + getImage().getWidth() / 2, currentPaddle.getY());
} else {
setLocation(currentPaddle.getX() - currentPaddle.getImage().getWidth() / 2 - getImage().getWidth() / 2, currentPaddle.getY());
}
}
} else {
// Move the ball and check for collisions
move();
checkCollision();
}
}
public void stickToPaddle(Paddle paddle) {
// Stick the ball to the given paddle
isStuck = true;
currentPaddle = paddle;
if (paddle.isPlayerOne) {
setLocation(paddle.getX() + paddle.getImage().getWidth() / 2 + getImage().getWidth() / 2, paddle.getY());
horizontalSpeed = 3;
} else {
setLocation(paddle.getX() - paddle.getImage().getWidth() / 2 - getImage().getWidth() / 2, paddle.getY());
horizontalSpeed = -3;
}
}
public void setSpeed(int horizontalSpeed, int verticalSpeed) {
// Set the speed of the ball
this.horizontalSpeed = horizontalSpeed;
this.verticalSpeed = verticalSpeed;
}
public int getHorizontalSpeed() {
return horizontalSpeed;
}
public int getVerticalSpeed() {
return verticalSpeed;
}
private void move() {
// Move the ball based on its speed
setLocation(getX() + horizontalSpeed, getY() + verticalSpeed);
}
private void checkCollision() {
// Check for collisions with the edges of the world, paddles, and power-ups
if (getY() <= 0 || getY() >= getWorld().getHeight() - 1) {
verticalSpeed = -verticalSpeed;
Greenfoot.playSound("mixkit-arcade-game-jump-coin-216.wav");
}
if (isTouching(Paddle.class)) {
Paddle paddle = (Paddle) getOneIntersectingObject(Paddle.class);
if (getX() < paddle.getX() + paddle.getImage().getWidth() / 2 && getX() > paddle.getX() - paddle.getImage().getWidth() / 2) {
// Ball hit the top or bottom of the paddle
verticalSpeed = -verticalSpeed;
} else {
// Ball hit the side of the paddle
if (paddle.isPlayerOne) {
horizontalSpeed = 5;
} else {
horizontalSpeed = -5;
}
}
Greenfoot.playSound("mixkit-game-ball-tap-2073.wav");
}
if (getX() <= 0 || getX() >= getWorld().getWidth() - 1) {
// Check for scoring when the ball hits the left or right edge
PingPongWorld world = (PingPongWorld) getWorld();
if (getX() <= 0) {
world.scorePoint(2); // Player 2 scores
world.resetBall();
world.resetPaddles();
} else {
world.scorePoint(1); // Player 1 scores
world.resetBall();
world.resetPaddles();
}
}
if (isTouching(PowerUp.class)) {
// Apply power-up effect when the ball touches a power-up
PowerUp powerUp = (PowerUp) getOneIntersectingObject(PowerUp.class);
powerUp.applyEffect(this, (PingPongWorld) getWorld());
Greenfoot.playSound("mixkit-arcade-bonus-alert-767.wav");
getWorld().removeObject(powerUp);
}
if (isTouching(Block.class)) {
// Handle collision with a block
Block block = (Block) getOneIntersectingObject(Block.class);
Greenfoot.playSound("mixkit-electronic-retro-block-hit-2185.wav");
block.breakBlock();
// Change direction of the ball
horizontalSpeed = -horizontalSpeed;
}
}
}