HTML小游戏

Wesley13
• 阅读 803

开发工具:Visual Studio Code

游戏介绍:已知有3个方块,前面的2个方块涂有相近的两种颜色,你需要根据这两种颜色序列的递减情况猜测第三个方块的颜色是什么,并从下面的方块中选择你觉得最接近的颜色。

程序设计步骤: 1.编写html代码

2.编写js代码

html代码编写 设置css文件

import url('https://fonts.googleapis.com/css?family=Pacifico');

html, body { background: #9cf; margin: 0; padding: 0; } h1, h2 { text-align: center; color: white; font-size: 5vmin; text-shadow: 0 1px 3px rgba(0,0,0,0.25); font-family: Pacifico, arial, serif; font-weight: normal; } h2 { font-size: 3.5vmin; margin-top: 5vmin; } #points { font-family: Pacifico, Verdana, sans-serif; color: white; font-size: 5vmin; text-shadow: 0 1px 3px rgba(0,0,0,0.25); position: absolute; top: 1vmin; right: 2vmin; } .center { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .boxes { margin: auto auto; text-align: center; white-space: nowrap; } .color-box { display: inline-block; background: red; box-sizing: border-box; border: 1.25vmin solid white; border-radius: 2px; width: 20vmin; height: 20vmin; margin-right: 5vmin; box-shadow: 0 1rem 0.75rem -0.75rem rgba(0,0,0,0.25); position: relative; } .boxes.mini .color-box { width: 15vmin; height: 15vmin; margin-right: 3vmin; cursor: pointer; } .color-box.right { border-color: green; }

.color-box.wrong { border-color: #e81222; } #box-3 { margin-right: 0; background: #ccc; overflow: hidden; } #color-3 { margin-right: 0; } #box-3::before { content: "?"; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-family: Pacifico, Verdana, sans-serif; font-size: 6vmin; color: rgba(0,0,0,0.5); } #scrim { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0); display: none; } #scrim.correct, #scrim.incorrect { display: block; } #scrim > div { padding: 3vmin; border-radius: 3px; background: white; box-shadow: 0 0.5rem 1.5rem -0rem rgba(0,0,0,0.25); } #scrim h2 { color: #444; margin-top: 0; display: none; } #scrim.correct #correct, #scrim.incorrect #incorrect { display: block; } #scrim button { width: 100%; text-align: center; font-size: 2vmin; padding: 1.5vmin; border-radius: 3px; border: 0; background: #396; color: white; box-shadow: 0 1rem 0.75rem -0.75rem rgba(0,0,0,0.25); cursor: pointer; } #correct-color, #picked-color { position: absolute; width: 100%; height: 60%; z-index: 2; } #picked-color { top: 50%; } html界面

游戏:根据渐变序列猜颜色

js编写 const game = { color: { red: 0, green: 0, blue: 0 }, variation: { red: 0, green: 0, blue: 0 }, right: 0, total: 0, possibilities: [ [0,0,16], [0,16,0], [0,16,16], [16,0,0], [16,0,16], [16,16,0], [16,16,16], [0,0,-16], [0,-16,0], [0,-16,-16], [-16,0,0], [-16,0,-16], [-16,-16,0], [-16,-16,-16], [0,16,-16], [0,-16,16], [16,0,-16], [-16,0,16], [16,-16,0], [-16,16,0], [16,16,-16], [16,-16,16], [16,-16,-16], [-16,16,16], [-16,16,-16], [-16,-16,16] ], min: 50, correct: 0, initialize: function() { // associate the event handlers to the buttons const boxes = document.querySelectorAll(".boxes.mini .color-box"); for (let x = 0; x < boxes.length; x++) { boxes[x].addEventListener("click", function() { if (this.dataset.value == game.correct) { document.querySelector("#scrim").classList.add("correct"); this.classList.add("right"); game.right++; } else { document.querySelector("#scrim").classList.add("incorrect"); this.classList.add("wrong"); document.querySelector([data-value='${game.correct}']).classList.add("right"); } game.total++; document.querySelector("#total").textContent = game.total; document.querySelector("#guessed").textContent = game.right; document.querySelector("#correct-color").style.backgroundColor = document.querySelector([data-value='${game.correct}']).style.backgroundColor; document.querySelector("#picked-color").style.backgroundColor = this.style.backgroundColor; }); }

// associate event to the button
document.querySelector("#scrim button").addEventListener("click", function() {
  const scrim = document.querySelector("#scrim");
  scrim.classList.remove("correct");
  scrim.classList.remove("incorrect");
  game.generateGame();
});

// generate a new round
this.generateGame();

}, generateGame: function() { // remove rights and wrongs var dright = document.querySelector(".right"); if (dright) dright.classList.remove("right"); var dwrong = document.querySelector(".wrong"); if (dwrong) dwrong.classList.remove("wrong"); document.querySelector("#correct-color").style.backgroundColor = "rgba(0,0,0,0)"; document.querySelector("#picked-color").style.backgroundColor = "rgba(0,0,0,0)";

// generate the sequence
this.color.red   = this.min + Math.floor(Math.random() * (255 - this.min * 2));
this.color.green = this.min + Math.floor(Math.random() * (255 - this.min * 2));
this.color.blue  = this.min + Math.floor(Math.random() * (255 - this.min * 2));
this.variation.red   = Math.floor((Math.random() * this.min) / 2);
this.variation.green = Math.floor((Math.random() * this.min) / 2);
this.variation.blue  = Math.floor((Math.random() * this.min) / 2);
document.querySelector("#box-1").style.backgroundColor = `rgb(${this.color.red},${this.color.green},${this.color.blue})`;
document.querySelector("#box-2").style.backgroundColor = `rgb(${this.color.red+this.variation.red},${this.color.green+this.variation.green},${this.color.blue+this.variation.blue})`;

// set up the correct one
this.correct = Math.floor(Math.random()*4);
document.querySelector("#color-" + this.correct).style.backgroundColor = `rgb(${this.color.red+this.variation.red*2},${this.color.green+this.variation.green*2},${this.color.blue+this.variation.blue*2})`;

// generate the incorrect colors
for (let x = 0; x < 4; x++) {
  if (x != this.correct) {
    var change = Math.floor(Math.random() * this.possibilities.length);
    document.querySelector("#color-" + x).style.backgroundColor = `rgb(${this.color.red+this.variation.red+this.possibilities[change][0]},${this.color.green+this.variation.green+this.possibilities[change][1]},${this.color.blue+this.variation.blue+this.possibilities[change][2]})`;
  }
}

} }

game.initialize() 运行截图

HTML小游戏 HTML小游戏 HTML小游戏

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Java修道之路,问鼎巅峰,我辈代码修仙法力齐天
<center<fontcolor00FF7Fsize5face"黑体"代码尽头谁为峰,一见秃头道成空。</font<center<fontcolor00FF00size5face"黑体"编程修真路破折,一步一劫渡飞升。</font众所周知,编程修真有八大境界:1.Javase练气筑基2.数据库结丹3.web前端元婴4.Jav
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这