Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
DecisionDay270120
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
a.guest
DecisionDay270120
Commits
4890f982
Commit
4890f982
authored
Jan 21, 2020
by
a.guest
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
255 additions
and
0 deletions
+255
-0
processing.pde
processing.pde
+255
-0
No files found.
processing.pde
0 → 100644
View file @
4890f982
int
window_width
=
600
;
int
window_height
=
480
;
int
target_size
=
50
;
int
score
=
0
;
Target
aTarget
=
null
;
ArrayList
<
Explosion
>
explosions
=
new
ArrayList
<
Explosion
>
();
void
setup
()
{
size
(
600
,
480
);
}
void
input
()
{
if
(
mousePressed
==
true
)
{
float
d
=
dist
(
mouseX
,
mouseY
,
aTarget
.
x_position
,
aTarget
.
y_position
);
if
(
d
<
target_size
)
{
// Target hit
explosions
.
add
(
new
Explosion
(
aTarget
.
x_position
,
aTarget
.
y_position
));
aTarget
=
null
;
// deletes target that has been hit
score
++
;
}
}
}
void
update
()
{
if
(
aTarget
==
null
)
{
aTarget
=
new
Target
();
}
if
(
!
aTarget
.
update
())
// update reveals target has gone off screen and should be deleted
{
aTarget
=
null
;
// deletes off screen target
}
for
(
int
i
=
0
;
i
<
explosions
.
size
();
i
++
)
// update each explosion
{
Explosion
exp
=
explosions
.
get
(
i
);
if
(
!
exp
.
update
())
{
explosions
.
remove
(
i
);
}
}
}
void
render
()
{
background
(
119
,
148
,
108
);
// set background colour (red, green, blue) each in 0-255 range
fill
(
255
,
255
,
255
);
// Set the colour to white for the score text
textSize
(
32
);
String
scoreText
=
"Score : "
+
score
;
text
(
scoreText
,
20
,
40
);
// show the score at the top left of the screen
fill
(
255
,
0
,
0
);
// set colour to red for the target
if
(
!
(
aTarget
==
null
))
// checks to see the target exists
{
aTarget
.
render
();
// draw the target
}
for
(
int
i
=
0
;
i
<
explosions
.
size
();
i
++
)
// draws each explosion in turn
{
Explosion
exp
=
explosions
.
get
(
i
);
exp
.
render
();
}
}
void
draw
()
{
input
();
update
();
render
();
}
class
Target
{
float
x_position
;
float
y_position
;
float
x_velocity
;
float
y_velocity
;
Target
()
{
int
randomNumber
=
int
(
random
(
4
));
if
(
randomNumber
==
0
)
// Start on Left Hand Side
{
x_position
=
-
target_size
;
// Starts off screen
y_position
=
random
(
400
)
+
40
;
x_velocity
=
random
(
10
);
y_velocity
=
random
(
20
)
-
10
;
}
else
if
(
randomNumber
==
1
)
// Start on Right Hand Side
{
x_position
=
600
+
target_size
;
// Starts off screen
y_position
=
random
(
400
)
+
40
;
x_velocity
=
-
random
(
10
);
y_velocity
=
random
(
20
)
-
10
;
}
else
if
(
randomNumber
==
2
)
// Start at top of screen
{
x_position
=
random
(
520
)
+
40
;
y_position
=
-
target_size
;
// Starts off screen
x_velocity
=
random
(
20
)
-
10
;
y_velocity
=
random
(
10
);
}
else
// Start at bottom of screen
{
x_position
=
random
(
520
)
+
40
;
y_position
=
480
+
target_size
;
// Starts off screen
x_velocity
=
random
(
20
)
-
10
;
y_velocity
=
-
random
(
10
);
}
}
boolean
update
()
{
x_position
+=
x_velocity
;
// change the target's position by adding its velocity to its current position
y_position
+=
y_velocity
;
// check to see if the target has gone off screen
if
(
x_position
<-
target_size
||
x_position
>
600
+
target_size
||
y_position
<-
target_size
||
y_position
>
480
+
target_size
)
{
return
false
;
}
else
{
return
true
;
}
}
void
render
()
{
// draws the target
ellipse
(
x_position
,
y_position
,
target_size
,
target_size
);
}
}
class
Explosion
{
float
x_position
;
float
y_position
;
ExplosionPart
[]
bits
;
int
timer
;
Explosion
(
float
x
,
float
y
)
{
// creates a new explosion at the specified location
x_position
=
x
;
y_position
=
y
;
timer
=
20
;
// Create 20 "explosion parts"
bits
=
new
ExplosionPart
[
20
];
for
(
int
i
=
0
;
i
<
20
;
i
++
)
{
bits
[
i
]
=
new
ExplosionPart
(
x_position
,
y_position
);
}
}
boolean
update
()
{
// timer is the lifespan of the explosion
// It starts at 20 and counts down by one each time the explosion is updated
// at timer = 0 the explosion ends
timer
--
;
if
(
timer
>
0
)
{
// if the explosion is still going on then update each of the explosion parts
for
(
int
i
=
0
;
i
<
20
;
i
++
)
{
bits
[
i
].
update
();
}
return
true
;
}
else
{
for
(
int
i
=
0
;
i
<
20
;
i
++
)
{
bits
[
i
]
=
null
;
}
return
false
;
}
}
void
render
()
{
// To draw the explosion we draw each of the explosion parts in turn
for
(
int
i
=
0
;
i
<
20
;
i
++
)
{
if
(
bits
[
i
]
!=
null
)
{
bits
[
i
].
render
();
}
}
}
}
class
ExplosionPart
{
float
x_position
;
float
y_position
;
float
x_velocity
;
float
y_velocity
;
int
colour
;
int
part_size
;
ExplosionPart
(
float
x
,
float
y
)
{
// Set up an explosion part at the target location
x_position
=
x
;
y_position
=
y
;
// give it a random velocity so each part goes in a different direction
x_velocity
=
random
(
20
)
-
10
;
y_velocity
=
random
(
20
)
-
10
;
// give the part a random shade of red as a starting colour
// each update the part will get slightly darker
colour
=
int
(
random
(
200
))
+
50
;
// give the part a random size
// each update the part will get slightly smaller
part_size
=
15
+
int
(
random
(
10
));
}
void
update
()
{
// make the part's colour a little darker
colour
=
colour
-
int
(
random
(
20
));
if
(
colour
<
10
)
{
colour
=
10
;
}
// make the part a little smaller
part_size
--
;
if
(
part_size
<
1
)
{
part_size
=
1
;
}
// update the part's position
x_position
+=
x_velocity
;
y_position
+=
y_velocity
;
}
void
render
()
{
fill
(
colour
,
0
,
0
);
ellipse
(
x_position
,
y_position
,
part_size
,
part_size
);
}
}
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