当前位置:网站首页>Canvas game lower level 100
Canvas game lower level 100
2022-06-13 00:42:00 【KatyLight】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
background-color: black;
padding: 0;
margin: 0;
}
</style>
<script>
let c, ctx, ctx2
var hero = {
p: {
x: 500,
y: 10
}
}
var land = []
var enemy = []
let getEn
var game
let las = false
var lasT = null
var score=0;
var scoreSpan
window.onload = () => {
c = document.getElementById('c')
ctx = c.getContext('2d')
ctx.strokeStyle = '#00FFFF'
scoreSpan=document.getElementById('score');
ctx.lineWidth=3
let ccc=getLands();
game = setInterval(() => {
ctx.clearRect(0, 0, c.width, c.height);
drawHero();
ccc();
drawLand();
}, 1)
}
function getLands() {
let j = 0
let k = Math.floor(Math.random() * 100 + 100)
return function() {
j++
if (j === k) {
j = 0
k = Math.floor(Math.random() * 100 + 50)
randomLand();
}
}
}
function scoreAdd(){
score++;
scoreSpan.innerText=score;
}
function randomLand(){
land.push({
p:{
x:Math.floor(Math.random()*700),
y:900
},
d:Math.random()*3<1
})
}
function drawHero() {
if(hero.p.y<-30){
clearInterval(game);
alert(' You were killed by the ceiling ')
return;
}
let down=true;
for(let i=0;i<land.length;i++){
if(Math.abs(land[i].p.y-hero.p.y)<10&&(hero.p.x>land[i].p.x?hero.p.x-land[i].p.x<200:land[i].p.x-hero.p.x<20)){
hero.p.y=land[i].p.y;
if(land[i].d){
clearInterval(game);
alert(' You were stabbed to death ')
}
down=false;
break;
}
}
if(down){
hero.p.y+=1;
if(hero.p.y>950){
clearInterval(game);
alert(' You fell dead ')
}
}
ctx.beginPath();
ctx.arc(hero.p.x, hero.p.y, 20, 0, Math.PI * 2);
ctx.stroke();
ctx.closePath();
}
function drawLand(){
for(let i=0;i<land.length;i++){
if(land[i].d){
ctx.fillStyle = '#FF0000';
}else{
ctx.fillStyle = '#00FFFF';
}
ctx.beginPath();
ctx.rect(land[i].p.x,land[i].p.y,200,10);
ctx.fill();
ctx.closePath();
land[i].p.y-=1;
if(land[i].p.y<-40){
land.splice(i,1);
scoreAdd();
}
}
}
let upTimer = null
let leftTimer = null
let rightTimer = null
let downTimer = null
let attackTimer = null
window.onkeydown = function(e) {
switch (e.keyCode) {
/* case 38:
if (upTimer !== null) {
return
}
if (hero.p.y > -10) {
hero.p.y -= 10
}
upTimer = setInterval(() => {
if (hero.p.y > -10) {
hero.p.y -= 10
}
}, 10)
break*/
case 39:
if (rightTimer !== null) {
return
}
if (hero.p.x < 910) {
hero.p.x += 10
}
rightTimer = setInterval(() => {
if (hero.p.x < 910) {
hero.p.x += 10
}
}, 10)
break
/* case 40:
if (downTimer !== null) {
return
}
if (hero.p.y < 910) {
hero.p.y += 10
}
downTimer = setInterval(() => {
if (hero.p.y < 910) {
hero.p.y += 10
}
}, 10)
break*/
case 37:
if (leftTimer !== null) {
return
}
if (hero.p.x > -10) {
hero.p.x -= 10
}
leftTimer = setInterval(() => {
if (hero.p.x > -10) {
hero.p.x -= 10
}
}, 10)
break
}
}
window.onkeyup = function(e) {
switch (e.keyCode) {
/* case 38:
clearInterval(upTimer)
upTimer = null
break*/
case 39:
clearInterval(rightTimer)
rightTimer = null
break
/* case 40:
clearInterval(downTimer)
downTimer = null
break*/
case 37:
clearInterval(leftTimer)
leftTimer = null
break
}
}
</script>
</head>
<body>
<div style="position: relative;margin: 20px auto;width: 1000px;">
<div style="position: absolute;right: 10px;top: 100px;font-size: 40px;color: cyan">
<span></span><span id="score">0</span>
</div>
<canvas id="c" width="900" height="900" style="border: 1px solid white;"></canvas>
</div>
</body>
</html>
Code
边栏推荐
- 【SCA-CNN 解读】空间与通道注意力:Spatial and Channel-wise Attention
- Arduino controls tb6600 driver +42 stepper motor
- What are the levels of safety accidents
- Buuctf babyupload[gxyctf2019]
- 6.824 Lab 3A: Fault-tolerant Key/Value Service
- Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis
- 为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)
- [LeetCode]21. Merge two ordered linked lists twenty-nine
- Transaction verification of btcd transaction process (2)
- String类中split()方法的使用
猜你喜欢
Aunt learning code sequel: ability to sling a large number of programmers
Installation of IK word breaker
[MRCTF2020]Ez_ bypass --BUUCTF
What are the levels of safety accidents
Development notes of Mongoose
Using com0com/com2tcp to realize TCP to serial port (win10)
Kali system -- dnsmap for DNS collection and analysis
磁盘分区方式对比(MBR与GPT)
DNS attack surface analysis
市值破万亿,连续三个月销量破10万,比亚迪会成为最强国产品牌?
随机推荐
Stm32f4 development of DMA transmission to GPIO port
Kali system -- fierce of DNS collection and analysis
Summary of openstack installation problems
[Error] invalid use of incomplete type 使用了未定义的类型
Can branches sign labor contracts with employees
[ciscn2019 North China Day2 web1]hack world --buuctf
Daily buckle exercise - conclusion
从ADK的WinPE自己手动构建自己的PE
[LeetCode]28. Implement strstr()
Leetcode weekly -- April to May
Androi天气
Three kinds of thinking make me reborn
[LeetCode]13. Roman numerals to integers thirty
63. different paths II
Mongodb array operation
今日在家休息
Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis
Influence of higher order poles on waveform
高阶极点对于波形的影响
Delphi2009 connecting Oracle11g