p5实现一个人机对战打乒乓球小游戏代码

代码语言:html

所属分类:游戏

代码描述:p5实现一个人机对战打乒乓球小游戏代码

代码标签: p5 乒乓球 游戏

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
<style>
    html {  text-align: center;padding:100px;}
</style>
</head>

<body>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/p5.js"></script>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/p5.dom.min.js"></script>
    <script>
        var player;
  var computer;
  var ball;
  var playerServe;
  var goalWaitPeriod = false;
  var sparks = [];
  var lightningForge;
  var isPlayerForcePush = false;
  var isCompForcePush = false;
  var xoff = 0;

  function setup() {
      var c = window.innerHeight <= 542 ? window.innerHeight - 20 : 522;
      var d = window.innerWidth <= 957 ? window.innerWidth - 20 : 937;
      createCanvas(d, c);
      player = new Player();
      computer = new Computer();
      ball = new Ball(width / 2, height / 2);
      scoreboard = new Scoreboard();
      playerServe = true;
      lightningForge = new LightningForge();
      textSize(32);
      textFont("Futura")
  }

  function draw() {
      if (goalWaitPeriod) {
          translate(random(-13, 13), random(-13, 13))
      }
      background(25);
      stroke(255);
      line(width / 2, 0, width / 2, height);
      player.update();
      if (keyIsDown(UP_ARROW)) {
          player.move(0, -7)
      } else {
          if (keyIsDown(DOWN_ARROW)) {
              player.move(0, 7)
          }
      }
      player.show();
      if (isPlayerForcePush) {
          player.paddle.forceUpdate("player")
      }
      computer.update();
      if (isCompForcePush) {
          computer.paddle.forceUpdate("computer")
      }
      computer.show();
      text(scoreboard.playerScore + " / 7", width / 2 - 140, 60);
      text(scoreboard.computerScore + " / 7", width / 2 + 60, 60);
      for (var c = sparks.length - 1; c >= 0; c--) {
          sparks[c].update();
          sparks[c].show();
          if (sparks[c].done()) {
              sparks.splice(c, 1)
          }
      }
      if (scoreboard.gameOver()) {
          fill(25, 123);
          noStroke();
          rect(0, 0, width, height);
          fill(255);
          var d = scoreboard.playerScore > scoreboard.computerScore ? "You win! " : "You lose! ";
          text(d + scoreboard.playerScore + " to " + scoreboard.computerScore + ".", width / 2 - 130, height / 2 - 40);
          text("Press spacebar to play again.", width / 2 - 220, height / 2)
      } else {
          if (!goalWaitPeriod) {
              if (lightningForge.forgeIsFormed()) {
                  ball.update();
                  ball.show()
              } else {
                  lightningForge.update();
                  lightningForge.show()
              }
          }
      }
  }

  function keyPressed() {
      if (scoreboard.gameOver && keyCode === 32) {
          scoreboard.resetScore()
      }
  }

  function Paddle(g, h, f, e) {
      this.x = g;
      this.y = h;
      this.width = f;
      this.height = e;
      this.xspeed = 0;
      this.yspeed = 0;
      this.forcePushTime = 0;
      this.show = function() {
          if (isPlayerForcePush) {
              fill(255)
          } else {
              fill(240)
          }
          rect(this.x, this.y, this.width, this.height, 5)
      };
      this.forceUpdate = function(a) {
          if (this.forcePushTime < 6) {
              this.x -= 1;
              this.width += 2;
              this.y -= 1;
              this.height += 2;
              this.forcePushTime += 1
          } else {
              if (this.forcePushTime < 12) {
                  this.x += 1;
                  this.width -= 2;
                  this.y += 1;
                  this.height -= 2;
                  this.forcePushTime += 1
              } else {
                  if (a === "player") {
                      isPlayerForcePush = false
                  } else {
                      isCompForcePush = false
                  }
                  this.forcePushTime = 0
              }
          }
      }
  }

  function Scoreboard() {
      this.playerScore = 0;
      this.computerScore = 0;
      this.gameOver = function() {
          return this.playerScore === 7 || this.computerScore === 7
      };
      this.resetScore = function() {
          this.playerScore = 0;
          this.computerScore = 0
      };
      this.playerScored = function() {
          this.playerScore += 1
      };
      this.computerScored = function() {
          this.computerScore += 1
      }
  }

  function Player() {
      this.paddle = new Paddle(30, (height / 2) - 25, 20, 70);
      this.update = function() {
          this.paddle.xspeed = 0;
          this.paddle.yspeed = 0
      };
      this.move = function(c, d) {
          this.paddle.x += c;
          this.paddle.y += d;
          this.paddle.xspeed = c;
          this.paddle.yspeed = d;
          if (this.paddle.y < 0) {
              this.paddle.y = 0;
              this.paddle.yspeed = 0
          } else {
              if (this.paddle.y + this.paddle.height > height) {
                  this.paddle.y = height - this.paddle.height;
                  this.paddle.yspeed = 0
              }
          }
      };
      this.show = function() {
          this.paddle.show()
      }
  }

  function Computer() {
      this.paddle = new Paddle(width - 50, (height / 2) - 25, 20, 70);
      this.move = function(c, d) {
          this.paddle.x += c;
          this.paddle.y += d;
          this.paddle.xspeed = c;
          this.paddle.yspeed = d;
          if (this.paddle.y < 0) {
              this.paddle.y = 0;
              this.paddle.yspeed = 0
          } else {
              if (this.paddle.y + this.paddle.height > height) {
                  this.paddle.y = height - this.paddle.height;
                  this.paddle.yspeed = 0
              }
          }
      };
      this.update = function() {
          var d = ball.y;
          var c = -((this.paddle.y + (this.paddle.height / 2)) - d);
          if (c < 0 && c < -4) {
              c = -8
          } else {
           .........完整代码请登录后点击上方下载按钮下载查看

网友评论0