当前位置:网站首页>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
边栏推荐
- Programming training 1
- 为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)
- Browser cache execution process
- Go design concurrent web crawler
- Triangle wave and triangle wave convolution
- Using fastjson to solve the problem of returning an empty array from a null value of a field string object
- Browser console injection JS
- 高阶极点对于波形的影响
- After so long use, CSDN has finally opened a blog
- Delphi Chinese digit to Arabic digit
猜你喜欢

硬(磁)盘(一)

Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis

String类中split()方法的使用

Opencv face recognition of ros2 foxy~galactic~humble

高阶极点对于波形的影响

Successfully installed opencv under delphixe

(01).NET MAUI实战 建项目

磁盘分区方式对比(MBR与GPT)

Static analysis of malicious code
![Buuctf babyupload[gxyctf2019]](/img/e8/202298b64d8764355fad348b50fee6.png)
Buuctf babyupload[gxyctf2019]
随机推荐
Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis
Stm32f4 development of DMA transmission to GPIO port
Some basic design knowledge
MAYA建模六
[buglist] serial port programming does not read data
[error] invalid use of incomplete type uses an undefined type
【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
.net core 抛异常对性能影响的求证之路
[supersocket 2.0] supersocket 2.0 from the beginning to the end
Opencv face recognition of ros2 foxy~galactic~humble
Basic operations of FreeMarker
Target recognition gadget
Packaging and uplink of btcd transaction process (III)
Kali system -- dnsmap for DNS collection and analysis
Use of split() method in string class
In / out / inout details of MySQL stored procedures
[LeetCode]20. Valid parentheses thirty-six
Arduino uses esp8266+ lighting technology + Xiaoai audio to realize voice control switch
6.824 Lab 3B: Fault-tolerant Key/Value Service
[LeetCode]3. The longest substring without duplicate characters forty