Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
NewHandin
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
james.green2
NewHandin
Commits
4e3cc1f5
Commit
4e3cc1f5
authored
Oct 27, 2017
by
james.green2
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
final push
parents
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
176 additions
and
0 deletions
+176
-0
PongWallBounce5.pde
PongWallBounce5.pde
+176
-0
No files found.
PongWallBounce5.pde
0 → 100644
View file @
4e3cc1f5
import
processing
.
core
.
PApplet
;
import
java
.
util
.
Random
;
PFont
f
;
//Declaring the variables for the first players paddle
float
Player1xpos
=
320
;
float
Player1ypos
=
45
;
float
Player1xspeed
=
1
*
3
;
int
Player1Points
=
0
;
//Declaring the variables for the second players paddle
float
Player2xpos
=
320
;
float
Player2ypos
=
440
;
float
Player2xspeed
=
1
*
3
;
int
Player2Points
=
0
;
//Display method draws the paddle in each new position depending
// on whether the paddle has moved left or right in previous methods
void
display
(
float
xpos
,
float
ypos
,
float
xpos2
,
float
ypos2
){
rectMode
(
CENTER
);
//fill(255);
rect
(
xpos
,
ypos
,
95
,
5
);
rect
(
xpos2
,
ypos2
,
95
,
5
);
}
//Changes the xposition of the paddle to make it move to the right
//first a check is carried out on the yposition to work
//out which paddle needs to be moved
void
driveR
(
float
xpos
,
float
xspeed
,
float
ypos
){
if
(
ypos
==
45
){
Player1xpos
=
xpos
+
xspeed
;
if
(
xpos
>
width
){
Player1xpos
=
0
;
}
}
if
(
ypos
==
440
){
Player2xpos
=
xpos
+
xspeed
;
if
(
xpos
<
0
){
Player2xpos
=
200
;
}
}
}
void
driveL
(
float
xpos
,
float
xspeed
,
float
ypos
){
if
(
ypos
==
45
){
Player1xpos
=
xpos
-
xspeed
;
if
(
xpos
<
0
){
Player1xpos
=
200
;
}
}
if
(
ypos
==
440
){
Player2xpos
=
xpos
-
xspeed
;
if
(
xpos
<
0
){
Player2xpos
=
200
;
}
}
}
// x position of the ball
private
int
ballX
;
// y position of the ball
private
int
ballY
;
// Ball speed in the x direction
private
int
ballSpeedX
;
// Ball speed in the y direction
private
int
ballSpeedY
;
// Width of our window
final
private
int
width
=
640
;
// height of our window
final
private
int
height
=
480
;
public
void
settings
()
{
// Set our window size
size
(
width
,
height
);
}
public
void
setup
()
{
// Create a random initial position and speed for the ball
f
=
createFont
(
"Arial"
,
16
,
true
);
// STEP 2 Create Font
Random
r
=
new
Random
();
ballX
=
r
.
nextInt
(
width
);
ballY
=
r
.
nextInt
(
height
);
ballSpeedX
=
(
r
.
nextInt
(
width
/
150
)
+
1
);
ballSpeedY
=
(
r
.
nextInt
(
height
/
150
)
+
1
);
ellipseMode
(
CENTER
);
}
public
void
keyPressed
(){
//listener if statement that runs every time a key is pressed
// the various key's are how the player controls the paddles
if
(
keyPressed
){
if
(
key
==
'0'
)
{
//Move the paddle right
driveR
(
Player1xpos
,
Player1xspeed
,
Player1ypos
);
}
if
(
key
==
'9'
){
//Move the paddle left
driveL
(
Player1xpos
,
Player1xspeed
,
Player1ypos
);
}
if
(
keyPressed
){
if
(
key
==
'1'
)
{
//Move the paddle Left
driveL
(
Player2xpos
,
Player2xspeed
,
Player2ypos
);
}
if
(
key
==
'2'
){
//Move the paddle right
driveR
(
Player2xpos
,
Player2xspeed
,
Player2ypos
);
}
}
}
}
public
void
draw
()
{
// Clear the background of the window
background
(
255
,
255
,
255
);
textFont
(
f
,
16
);
// STEP 3 Specify font to be used
fill
(
0
);
// STEP 4 Specify font color
text
(
"Player1: "
+
Player1Points
,
10
,
100
);
text
(
"Player2: "
+
Player2Points
,
10
,
140
);
// Draw the ball
ellipse
(
ballX
,
ballY
,
32
,
32
);
// Move the ball
ballX
+=
ballSpeedX
;
ballY
+=
ballSpeedY
;
//Checks the ball is still within the screen
//If not it reverses the balls direction and
//'bounces' the ball off the screen edge
if
(
ballX
>
(
width
-
16
)
||
ballX
<
0
){
ballSpeedX
=
ballSpeedX
-
(
ballSpeedX
*
2
);
}
if
(
ballY
>
(
height
-
16
)
||
ballY
<
0
){
ballSpeedY
=
ballSpeedY
-
(
ballSpeedY
*
2
);
}
//If the ball is is in the same x and y position as a players
//paddle then it bounces off the paddle
if
(
ballY
<=
Player1ypos
+
16
){
if
((
ballX
>
Player1xpos
-
58.5
&&
ballX
<
Player1xpos
+
58.5
)){
ballSpeedY
=
ballSpeedY
-
(
ballSpeedY
*
2
);
}
}
if
(
ballY
>=
Player2ypos
-
16
){
if
((
ballX
>
Player2xpos
-
58.5
&&
ballX
<
Player2xpos
+
58.5
)){
ballSpeedY
=
ballSpeedY
-
(
ballSpeedY
*
2
);
}
}
if
(
ballY
<
16
){
if
((
ballX
>
0
&&
ballX
<
600
)){
setup
();
ellipse
(
ballX
,
ballY
,
32
,
32
);
Player2Points
=
Player2Points
+
1
;
}
}
if
(
ballY
>
464
){
if
((
ballX
>
0
&&
ballX
<
640
)){
setup
();
ellipse
(
ballX
,
ballY
,
32
,
32
);
Player1Points
=
Player1Points
+
1
;
}
}
//Move the paddles
keyPressed
();
//Refresh the board
display
(
Player1xpos
,
Player1ypos
,
Player2xpos
,
Player2ypos
);
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment