前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何用html+css+js做一个跳跃逃离坍塌房子

如何用html+css+js做一个跳跃逃离坍塌房子

作者头像
海曈
发布2022-11-14 16:25:28
7670
发布2022-11-14 16:25:28
举报
文章被收录于专栏:HTML+CSS前端HTML+CSS前端

本章教你如何用html+css+js做一个跳跃逃离坍塌房子。

这次我们做动画特效,我们来整个小游戏! 我先把跳跃逃离坍塌房子运行结果放在下面

在这里插入图片描述
在这里插入图片描述

我们可以把html+css+js放在html文件里面,也就是把css和JavaScript放在index.html里面。

废话不多说我们先来讲解html的代码。

我使用下面的 HTML 和 CSS 代码在网页上创建了游戏界面。html代码就是你所看到的六个div和class选择器组成的。

代码语言:javascript
复制
<div class="game">
<div class="player idle"></div>
<div class="buildings"></div>
<div class="road"></div>
<div class="score">分数: 0</div>
<div class="msg">点击开始...</div>

当我们写好html代码后,我们使用下面的CSS后,我们就成功的在网页上搭建了游戏界面。

代码语言:javascript
复制
@import url("https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@1,800&display=swap");
*, *::before, *::after {
  padding: 0;
  margin: 0 auto;
  box-sizing: border-box;
}

body {
  font-family: "Open Sans", sans-serif;
  background-color: #111;
  color: #fff;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.game {
  position: relative;
  width: 960px;
  height: 340px;
  overflow: hidden;
  background-image: linear-gradient(#001, #200);
  box-shadow: 0 0 1em #fff1;
}

.score {
  position: absolute;
  top: 0.5em;
  left: 0.5em;
}

.msg {
  position: absolute;
  top: 2em;
  left: 50%;
  font-size: 1.2em;
  transform: translateX(-50%);
  text-align: center;
  transition: opacity 0.25s;
}
.msg.off {
  opacity: 0;
}

.player {
  position: absolute;
  top: var(--player-y, 320px);
  left: var(--player-x, 480px);
  transform: translate(-100%, -100%);
  width: 24px;
  height: 24px;
  background-size: contain;
}
.player.idle {
  background-image: url("https://repo.bfw.wiki/bfwrepo/images/game/pao/player-idle.gif");
}
.player.run {
  background-image: url("https://repo.bfw.wiki/bfwrepo/images/game/pao/player-run.gif");
}
.player.jump {
  background-image: url("https://repo.bfw.wiki/bfwrepo/images/game/pao/player-jump.gif");
}
.player.dead {
  background-image: url("https://repo.bfw.wiki/bfwrepo/images/game/pao/player-dead.gif");
}

.building {
  position: absolute;
  bottom: 20px;
  left: var(--building-x, 960px);
  border: solid #0007;
  border-width: 0 1px;
}
.building_fragment {
  position: absolute;
  width: 25%;
  height: 33.334%;
  background-image: url("https://repo.bfw.wiki/bfwrepo/images/game/pao/building02.png");
  background-size: 1600% 300%;
  filter: hue-rotate(var(--hue));
}
.building_fragment:nth-child(4n+1) {
  left: 0%;
  background-position-x: calc(var(--buildingImageX, 0%) + 0%);
}
.building_fragment:nth-child(4n+2) {
  left: 25%;
  background-position-x: calc(var(--buildingImageX, 0%) + 6.25%);
}
.building_fragment:nth-child(4n+3) {
  left: 50%;
  background-position-x: calc(var(--buildingImageX, 0%) + 12.5%);
}
.building_fragment:nth-child(4n+4) {
  left: 75%;
  background-position-x: calc(var(--buildingImageX, 0%) + 18.75%);
}
.building_fragment:nth-child(n+1):nth-child(-n+4) {
  top: 0%;
  background-position-y: 0%;
}
.building_fragment:nth-child(n+5):nth-child(-n+8) {
  top: 33.334%;
  background-position-y: 50%;
}
.building_fragment:nth-child(n+9):nth-child(-n+12) {
  top: 66.668%;
  background-position-y: 100%;
}
.building.destroy .building_fragment {
  z-index: 100;
  -webkit-animation: destroy 1s ease-out forwards;
          animation: destroy 1s ease-out forwards;
}
@-webkit-keyframes destroy {
  to {
    transform: translateX(var(--tx)) translateY(var(--ty)) rotateX(var(--rx)) rotateY(var(--ry)) rotateZ(var(--rz));
    opacity: 0;
  }
}
@keyframes destroy {
  to {
    transform: translateX(var(--tx)) translateY(var(--ty)) rotateX(var(--rx)) rotateY(var(--ry)) rotateZ(var(--rz));
    opacity: 0;
  }
}
.building.destroy::before {
  content: "";
  position: absolute;
  top: calc(50% - 5px);
  right: 90%;
  width: 320px;
  height: 10px;
  border-radius: 5px;
  background-image: linear-gradient(#f000, #f00, #f000);
  -webkit-animation: laserOff 0.5s ease-out forwards;
          animation: laserOff 0.5s ease-out forwards;
}
@-webkit-keyframes laserOff {
  to {
    opacity: 0;
  }
}
@keyframes laserOff {
  to {
    opacity: 0;
  }
}

.road {
  position: absolute;
  bottom: 0;
  width: calc(100% + 10px);
  height: 20px;
  background-image: linear-gradient(#555, #333);
}
.road::after {
  content: "";
  position: absolute;
  top: calc(50% - 1px);
  left: 0;
  width: 100%;
  height: 2px;
  background-image: repeating-linear-gradient(90deg, #aaa 0px 5px, #aaa0 5px 10px);
}

html+css的代码演示如下

由于代码不能放多了,所以JavaScript代码我就不放在这个上面了,我就简单的讲一下js在游戏里面的作用。 JavaScript在这个小游戏里面非常重要的作用,用另一种说就是游戏的核心,JavaScript在游戏里划分游戏人物跳跃和计算人物所跳跃的数量,还规划了游戏的场景。所以JavaScript在游戏里起到了重要作用!

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=5t95bj3gl3hu

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-11-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本章教你如何用html+css+js做一个跳跃逃离坍塌房子。
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档