In this assessment, you are expected to use the bouncing balls demo from the previous article as a starting point, and add some new and interesting features to it.
| Prerequisites: | Before attempting this assessment you should have already worked through all the articles in this module. |
|---|---|
| Objective: | To test comprehension of JavaScript objects and object-oriented constructs |
To get this assessment started, make a local copy of index-finished.html , style.css ,和 main-finished.js from our last article in a new directory in your local computer.
Alternatively, you could use a site like
JSBin
or
Glitch
to do your assessment. You could paste the HTML, CSS and JavaScript into one of these online editors. If the online editor you are using doesn't have separate JavaScript/CSS panels, feel free to put them inline
<script>
/
<style>
elements inside the HTML page.
注意: If you get stuck, then ask us for help — see the Assessment or further help section at the bottom of this page.
A couple of pointers before you get started.
Our bouncy ball demo is fun, but now we want to make it a little bit more interactive by adding a user-controlled evil circle, which will eat the balls if it catches them. We also want to test your object-building skills by creating a generic
Shape()
object that our balls and evil circle can inherit from. Finally, we want to add a score counter to track the number of balls left to capture.
The following screenshot gives you an idea of what the finished program should look like:
To give you more of an idea, have a look at the finished example (no peeking at the source code!)
The following sections describe what you need to do.
First of all, create a new
形状
class. This has only a constructor. The
形状
constructor should define the
x
,
y
,
velX
,和
velY
properties in the same way as the
Ball()
constructor did originally, but not the
color
and
size
特性。
Ball
class should be made to derive from
形状
使用
extends
. The constructor for
Ball
should:
x
,
y
,
velX
,
velY
,
size
,和
color
形状
constructor using
super()
, passing in the
x
,
y
,
velX
,和
velY
arguments
color
and
size
properties from the parameters it is given.
Ball
constructor should define a new property called
exists
, which is used to track whether the balls exist in the program (have not been eaten by the evil circle). This should be a boolean (
true
/
false
), initialized to
true
in the constructor.
collisionDetect()
方法在
Ball
class needs a small update. A ball needs to be considered for collision detection only if the
exists
特性为
true
. So, replace the existing
collisionDetect()
code with the following code:
collisionDetect() {
for (const ball of balls) {
if (!(this === ball) && ball.exists) {
const dx = this.x - ball.x;
const dy = this.y - ball.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.size + ball.size) {
ball.color = this.color = randomRGB();
}
}
}
}
As discussed above, the only addition is to check if the ball exists — by using
ball.exists
在
if
conditional.
The ball
draw()
and
update()
method definitions should be able to stay exactly the same as they were before.
At this point, try reloading the code — it should work just the same as it did before, with our redesigned objects.
Now it's time to meet the bad guy — the
EvilCircle()
! Our game is only going to involve one evil circle, but we are still going to define it using a constructor that inherits from
Shape()
, to give you some practice. You might want to add another circle to the app later on that can be controlled by another player, or have several computer-controlled evil circles. You're probably not going to take over the world with a single evil circle, but it will do for this assessment.
Create a definition for an
EvilCircle
class. It should inherit from
形状
使用
extends
.
The constructor for
EvilCircle
should:
x
,
y
arguments
x
,
y
arguments up to the
形状
superclass along with values for
velX
and
velY
hardcoded to 20. You should do this with code like
super(x, y, 20, 20);
color
to
white
and
size
to
10
.
Finally, the constructor should set up the code enabling the user to move the evil circle around the screen:
window.addEventListener('keydown', (e) => {
switch(e.key) {
case 'a':
this.x -= this.velX;
break;
case 'd':
this.x += this.velX;
break;
case 'w':
this.y -= this.velY;
break;
case 's':
this.y += this.velY;
break;
}
});
This adds a
keydown
event listener to the
window
object so that when a key is pressed, the event object's
key
property is consulted to see which key is pressed. If it is one of the four specified keys, then the evil circle will move left/right/up/down.
EvilCircle
class should have three methods, as described below.
This method has the same purpose as the
draw()
方法对于
Ball
: it draws the object instance on the canvas. The
draw()
方法对于
EvilCircle
will work in a very similar way, so you can start by copying the
draw()
方法对于
Ball
. You should then make the following changes:
fillStyle
and
fill()
to
strokeStyle
and
stroke()
.
lineWidth
somewhere after the
beginPath()
call (3 will do).
This method will do the same thing as the first part of the
update()
方法对于
Ball
— look to see whether the evil circle is going to go off the edge of the screen, and stop it from doing so. Again, you can mostly just copy the
update()
方法对于
Ball
, but there are a few changes you should make:
if()
statements, if the tests return true we don't want to update
velX
/
velY
; we want to instead change the value of
x
/
y
so the evil circle is bounced back onto the screen slightly. Adding or subtracting (as appropriate) the evil circle's
size
property would make sense.
This method will act in a very similar way to the
collisionDetect()
方法对于
Ball
method, so you can use a copy of that as the basis of this new method. But there are a couple of differences:
if
statement, you no longer need to check whether the current ball in the iteration is the same as the ball that is doing the checking — because it is no longer a ball, it is the evil circle! Instead, you need to do a test to see if the ball being checked exists (with which property could you do this with?). If it doesn't exist, it has already been eaten by the evil circle, so there is no need to check it again.
if
statement, you no longer want to make the objects change color when a collision is detected — instead, you want to set any balls that collide with the evil circle to not exist any more (again, how do you think you'd do that?).
Now we've defined the evil circle, we need to actually make it appear in our scene. To do this, you need to make some changes to the
loop()
函数。
draw()
,
update()
,和
collisionDetect()
functions for each one, make it so that these functions are only called if the current ball exists.
draw()
,
checkBounds()
,和
collisionDetect()
methods on every iteration of the loop.
To implement the score counter, follow the following steps:
<p>
element just below the
<h1>
element containing the text "Ball count: ".
p {
position: absolute;
margin: 0;
top: 35px;
right: 5px;
color: #aaa;
}
If you would like your work assessed, or are stuck and want to ask for help:
最后修改: , 由 MDN 贡献者