canvas实现万圣节的猫和蝙蝠跳跃小游戏代码

代码语言:html

所属分类:游戏

代码描述:canvas实现万圣节的猫和蝙蝠跳跃小游戏代码,按鼠标左键或空格键进行跳跃避开红色的物体。

代码标签: canvas 万圣节 蝙蝠 跳跃 小游戏 代码

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

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

<head>
  <meta charset="UTF-8">
  

  
  
  
<style>
@import url("https://fonts.googleapis.com/css2?family=Oxygen:wght@700&family=Work+Sans:wght@300;500&display=swap");

body {
  margin: 0;
  overflow: hidden;
  font-family: "Work Sans", sans-serif;
  background: radial-gradient(
		circle,
		rgba(7, 73, 71, 1) 2%,
		rgb(14, 44, 42) 90%
	);
}

canvas {
  display: block;
  background-color: #f0f0f0;
  max-height: calc(100vh - 100px);
  margin: 50px auto;
}

.footer {
  position: fixed;
  bottom: 10px;
  right: 10px;
  color: white;
  font-size: 12px;
  text-align: right;
  font-family: monospace;
}

.footer a {
  color: white;
  text-decoration: none;
}
</style>

  
</head>

<body >
  <canvas id="gameCanvas"></canvas>

<div class="footer">
	Made with ❤️ by Marcus | Visit <a href="" target="_blank">Shopblocks</a>
</div>
  
      <script id="rendered-js" >
let debugMode = false;
let currentTime = Date.now();
let score = 0; // Initialize the score
let highScore = 0; // Initialize the high score

const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");

// Set canvas size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const spriteSheet = new Image();
spriteSheet.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAACACAYAAADktbcKAAAAAXNSR0IArs4c6QAACPdJREFUeJzt3T13ozgUgGF5z3T0Sk1Plyr/v0iVzn3q0KfWFmMx10ISsvUF+H3O8fGOvfEVIAkhPq5SAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKMc473gtT23//3oX4IR6LL/RWt+99yiD8/5qei6/GYbh7j31D0t1AFTAv3Ef3gAl4mqtlX3N8yzL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U4LwbsxlfKOClot96us/yo4BEAK49zWvWIPBSo/Fg6FcRagohNdDHMRE2vRV6UJuDOsw12iA0Cq1EZdvPHv/FLoQ6MDAF4YHUAeLoRpIPXR53jcmTqA2vejG88ryD4JGVXRAf/zVP0/y1mA2vkI706BcSFMN8mV+4lH0R/ZUu9vZ2uS63/JDqDXzRhGXLNe40q86PlvtBHrgH2d7Qt1wFn1v1QH8HQPVIovH2GtzMTf39/LlXChnHwirwEXoqC6Z+t/iTmA3T4RptRpIJv1R3InpsZxXF7OOXPkucQuQLKZkLCWUv+LDJE7X4pZ+4EUd8+7k9wEINyJVtVqHmYn9yL0llX/q54FaHAhRrdZYNvQScfdzN263tkI7KEzRCXj5tb/lA4gtkCtG2CX03D2d0J7ebtnYrKwLdkhXK9XmSq+6Bmgre+11so+QOWBv0uJu/UKSq3/W5OARswuPrRAhU/DcD86erCJakOTaUubiEzCqcDfbsYNJectWf+jyUFllt5nz4MXuDXUexqu8TGgrACtnsiDNePsVC7yM/HkoqIjgNiOzNfQPr5+lh1gRv2PZqcuVf83U3WL/6fHJMzdyt/p/ehoq0cHfDcCVarbJGTwmpRn6//DQ5PSBYjFEiOQVSckh0Q8Dw6NtKz/0fg96r8ZhmF5KTEZYf+ttS49A+r+loyxXPts44tyAaVt1n/nuyplKF3//wdF5eHgh8td5QAAAABJRU5ErkJggg==";

const gravity = 0.25; // Gravity force (adjust as needed)
const airResistance = 0.14; // Air resistance to slow down the cat in mid-air
const scale = 3; // Scale factor for the cat
const catWidth = 32; // Width of each frame
const catHeight = 32; // Height of each frame
const initialObstacleSpeed = 2;

let currentObstacleSpeed = initialObstacleSpeed; // Variable to track the current obstacle speed
let isGameOver = false;

const cat = {
  x: 16 * scale, // Initial x position (just off the left-hand side)
  y: canvas.height - canvas.height * 0.1 - catHeight * scale, // Initial y position (10% from the bottom)
  width: catWidth, // Width of each frame
  height: catHeight, // Height of each frame
  frameX: 0, // Current frame X coordinate
  frameY: 0, // Current frame Y coordinate
  speed: 5, // Cat movement speed
  jumpPower: -9, // Jump power (initial upward velocity)
  isJumping: false, // Is the cat currently jumping
  canDoubleJump: true, // Flag to indicate if the cat can double jump
  velocityY: 0, // Vertical velocity for jump
  jumpCount: 0, // Number of jumps in the air
  maxJumpCount: 2, // Maximum number of jumps allowed in the air (2 for double jump)
  onGround: true // Flag to indicate whether the cat is on the ground
};

let animationId;
let lastFrameUpdateTime = 0;
const frameDuration = 100; // Duration in milliseconds for each frame (moved to global scope)

const jumpCooldown = 300; // Milliseconds
let lastJumpTime = 0;
let lastCatX = cat.x;

ctx.imageSmoothingEnabled = false;

const obstacles = [];
let obstacleSpeed = 2; // Adjust the speed as needed
let gameStarted = false; // Flag to indicate if the game has started

function generateObstacle() {
  const minObstacleHeight = 32; // Minimum obstacle height
  const maxObstacleHeight = 128; // Maximum obstacle height
  const minDistanceBetweenObstacles = 100; // Minimum distance between obstacles
  const maxDistanceBetweenObstacles = 300; // Maximum distance between obstacles

  const obstacleWidth = 32; // Adjust the width as needed
  const obstacleHeight = Math.floor(
  Math.random() * (maxObstacleHeight - minObstacleHeight + 1) + minObstacleHeight);


  const obstacleX = canvas.width + Math.floor(
  Math.random() * (maxDistanceBetweenObstacles - minDistanceBetweenObstacles + 1) + minDistanceBetweenObstacles);


  obstacles.push({
    x: obstacleX,
    y: canvas.height - obstacleHeight, // Position on the floor
    width: obstacleWidth,
    height: obstacleHeight });

}

function drawScore() {
  ctx.fillStyle = "white";
  ctx.font = "20px monospace";

  // Display the score in the top right corner
  ctx.textAlign = "right";
  ctx.fillText("Score: " + score, canvas.width - 10, 30);

  // Display the high score if it's higher than 0
  if (highScore > 0) {
    ctx.textAlign = "le.........完整代码请登录后点击上方下载按钮下载查看

网友评论0