- HTML5 Game Development by Example:Beginner's Guide(Second Edition)
- Makzan
- 480字
- 2021-07-16 14:09:59
Time for action – Showing the score of both players
We are going to create a text-based scoreboard and update the scores when either player scores a goal:
- We are making improvements on our existing game so that we can use the last example as the starting point.
- Open the
index.html
in the text editor. We are going to add the scoreboard's DOM elements. - Add the
#scoreboard
HTML structure to our#game
DIV inside index.html. The#game
DIV becomes the following:<p id="game"> <p id="playground"> <p class="paddle-hand right"></p> <p class="paddle-hand left"></p> <p id="paddleA" class="paddle"></p> <p id="paddleB" class="paddle"></p> <p id="ball"></p> </p> <p id="scoreboard"> <p class="score"> A : <span id="score-a">0</span></p> <p class="score"> B : <span id="score-b">0</span></p> </p> </p>
- Now, let's move onto the JavaScript part. Open the
js/pingpong.js
file. - We need two more variables to store the players' scores. Add their score variables inside the existing
pingpong
data object:var pingpong = { scoreA : 0, // score for player A scoreB : 0, // score for player B // existing pingpong data goes here. }
- Find the
playerAWin
function. We increment player A's score there and update the scoreboard with the following code:// player B lost. pingpong.scoreA += 1; $("#score-a").text(pingpong.scoreA);
- We can add a code similar to that in the previous step to update player B's score when player A is lost in the
playerBWin
function:// player A lost. pingpong.scoreB += 1; $("#score-b").text(pingpong.scoreB);
- Let's move onto the
css/pingpong.css
file. Put the following styles in the file to make the score board look nicer:/* Score board */ #scoreboard { position: absolute; bottom: 0; left: 0; width: 100%; padding: 5px; color: lightgrey; }
- It is time to test our latest code. Open the
index.html
in a web browser. Try playing by controlling both paddles and lose some points. The scoreboard should be counting the scores correctly.
What just happened?
We just used another common jQuery function: text()
to alter the content of the game on the fly.
The text()
function gets or updates the text content of the selected element. Here is a general definition of the text
() function:
.text() .text(string)
When we use the text()
function without an argument, it returns the text content of the match elements. When we use it with an argument, it sets the text content to all the matched elements with the given string.
For example, provide the following HTML structure:
<p>My name is <span id="myname" class="name">Makzan</span>.</p> <p>My pet's name is <span id="pet" class="name"> Co-co</span>.</p>
The following jQuery calls return Makzan:
$("#myname").text(); // returns Makzan
However, in the following jQuery call, it sets all matched elements to the given HTML content:
$(".name").text("Mr. Mystery")
Executing the jQuery command gives the following HTML result:
<p>My name is <span id="myname" class="name">Mr. Mystery</span></p> <p>My pet's name is <span id="pet" class="name">Mr. Mystery</span></p>
Have a go hero – winning the game
Imagine that the game is an advertisement. We set the entire game playground with pointer cursor so that the user knows the game is clickable and links to some other place. Try to use jQuery's click
event and handle the advertisement that's linked to the handleMouseInputs
function.
- Learning Real-time Processing with Spark Streaming
- Android Jetpack開發:原理解析與應用實戰
- 劍指JVM:虛擬機實踐與性能調優
- Python自動化運維快速入門
- MongoDB for Java Developers
- 編程珠璣(續)
- Servlet/JSP深入詳解
- 云計算通俗講義(第3版)
- Nginx實戰:基于Lua語言的配置、開發與架構詳解
- INSTANT Sinatra Starter
- Unity 2018 Augmented Reality Projects
- Machine Learning With Go
- Arduino可穿戴設備開發
- RESTful Web Clients:基于超媒體的可復用客戶端
- Google Adsense優化實戰