JavaScript小游戏,通过颜色选择正确的字

前言

又是一个用JavaScript实现的小游戏,游戏规则为:根据上面的字的颜色从下面选择正确的字,选择正确自动开始

正文

主要思路:

如何改变字体和颜色 :将颜色和字块,放进数组,通过将数组进行重新排序,这样可以将颜色随机赋给随机的一个字体。

如何进行判断:通过获取提示信息的颜色值和当前点击的对象的innerHTML进行比较,如果符合条件,则进入下一轮,例如:

if(char_inform == '红' && remind.style.color == 'red') {

random();

count++;

}

分数设置:当第一次点击正确时,游戏开始,并且开始计时,每点击正确一次,分数加1;在这儿,通过两层判断,用来防止计时器重复启动。例如:

if(count == 1) {

a++;

if(a == 1) {

cut_down();//计时器函数

}

}

}

这样,当下次点击正确时,a=2;满足条件,计时器函数无法再次调用。

重置游戏:当计时结束时,即time=0时,游戏结束。点击重置按钮,可以重新开始游戏,这儿用到history.go('0')用来刷新界面,以此来达到重置游戏的效果

效果图:




源码如下:




	
		
   
   
		
		
	

	
		
   
   
根据上面的字的颜色从下面选择正确的字,选择正确自动开始
<script type="text/javascript"> //定义颜色和字符数组 var color = ['red', 'blue', 'pink', 'purple', 'orange']; var character = ['红', '蓝', '橙', '粉', '紫']; var color1 = ['red', 'blue', 'pink', 'purple', 'orange']; var character1 = ['红', '蓝', '橙', '粉', '紫']; //创建信息框 var remind = document.getElementById('remind'); var inform_ = []; var count = 0; var time = 20; var a = 0; for(i = 0; i < character.length; i++) { var information = document.getElementById('information'); var inform = document.createElement('div'); inform.style.border = '1px solid yellowgreen'; inform.style.width = '18%'; inform.style.margin = '2px'; inform.style.marginTop = '10px'; inform.style.lineHeight = '60px'; inform.style.height = '60px'; inform.style.textAlign = 'center'; inform.style.fontSize = '2rem'; inform.style.display = 'inline-block'; inform.onclick = click; inform_.push(inform); information.appendChild(inform); } //功能实现 function random() { color = color.sort(function() { return Math.random() - 0.5; }); color1 = color1.sort(function() { return Math.random() - 0.5; }); character = character.sort(function() { return Math.random() - 0.5; }); character1 = character1.sort(function() { return Math.random() - 0.5; }); remind.style.color = color[2]; remind.innerHTML = character[4]; for(i = 0; i < color.length; i++) { inform_[i].innerHTML = character1[i]; inform_[i].style.color = color1[i]; } } random(); var count_ = document.getElementById('count_'); coun = count_.innerHTML; function click() { var char_inform = this.innerHTML; if(time != 0) { if(char_inform == '红' && remind.style.color == 'red') { random(); count++; } else if(char_inform == '蓝' && remind.style.color == 'blue') { random(); count++; } else if(char_inform == '粉' && remind.style.color == 'pink') { random(); count++; } else if(char_inform == '紫' && remind.style.color == 'purple') { random(); count++; } else if(char_inform == '橙' && remind.style.color == 'orange') { random(); count++; } count_.innerHTML = coun + count + '分'; if(count == 1) { a++; if(a == 1) { cut_down(); } } } console.log(count) } var timer = null; var title = document.getElementById('title'); titl = title.innerHTML; title.innerHTML = titl + time + 's'; function cut_down() { if(time >= 0) { timer = setInterval(function() { time--; title.innerHTML = titl + time + 's'; console.log(time); if(time == 0) { alert('游戏结束'); clearInterval(timer) // history.go('0'); } }, 1000) } } //重置游戏 var button0=document.getElementById("button0") button0.οnclick=function(){ history.go('0'); } </script>
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值