- HTML5 Game Development by Example:Beginner's Guide(Second Edition)
- Makzan
- 177字
- 2021-07-16 14:09:58
Time for action – auto moving the left paddle
Perform the following set of actions for automoving our paddle:
- Let's continue with our
pingpong.js
JavaScript file. We create a function that follows the ball's y position.function autoMovePaddleA() { var speed = 4; var direction = 1; var paddleY = pingpong.paddleA.y + pingpong.paddleA.height/2; if (paddleY > pingpong.ball.y) { direction = -1; } pingpong.paddleA.y += speed * direction; }
- Then, inside the game loop function, we call our
autoMovePaddleA
function.autoMovePaddleA();
What just happened?
We created a logic that moves the left paddle based on the ball's y position. You may try the game with its current progress at http://makzan.net/html5-games/pingpong-wip-step6/.
Since we have already implemented the view rendering in the renderPaddles
function, in this section, we only need to update the paddle's data and the view will get updated automatically.
We make the paddle speed slower than the ball's speed. Otherwise, the player can never win against the computer, because the computer-controlled paddle can always catch the ball and bounce it back if they have the same speed.
推薦閱讀
- Git Version Control Cookbook
- Vue.js入門與商城開發實戰
- Mastering Articulate Storyline
- Scratch 3.0少兒編程與邏輯思維訓練
- R的極客理想:工具篇
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- 精通Python自動化編程
- NetBeans IDE 8 Cookbook
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- Visual C#.NET Web應用程序設計
- Python程序設計與算法基礎教程(第2版)(微課版)
- Hands-On Kubernetes on Windows
- Spring技術內幕:深入解析Spring架構與設計原理(第2版)
- App Inventor少兒趣味編程動手做
- Hacking Android