Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flappy youwen(?) #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions 1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<title>早上好!</title>
<style>
</style>
<link href="1.css">
</head>
<header>
</header>
<body>
<div style="width: 1600px;height: 840px;background-color:gray;position: fixed;"></div>
<div id="1" style="left: 500px;width: 5000px;height: 840px;position: fixed;">

</div>
<img src="youwen.jpg" id="0" style="position: relative;top: 0px;left:0px;width: 40px;height: 40px;background-color: red;">
</body>
<script src="1.js"></script>
</html>
85 changes: 85 additions & 0 deletions 1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
playerX = 200;
playerY = 100;

playerV = 0;

i = 0;

mapX = 500;
timer = setInterval(timer1, 10);
document.addEventListener("keydown", OnKeyDown);

function timer1()
{
if (playerY < 0 || playerY > 800) {
clearInterval(timer);
window.alert("游戏结束!");
}
i++;
console.log("tick:" + i);
playerV -= 0.1;
playerY -= playerV;
mapX--;
document.getElementById("0").style.top = playerY + "px";
document.getElementById("0").style.left = playerX + "px";
document.getElementById("1").style.left = mapX + "px";
if (i % 500 == 0) {
MakeABlock();
}

// 碰撞检测
checkCollision();
}

function OnKeyDown(event)
{
if (event.keyCode == 87) {
playerV = 5;
}
}

function MakeABlock()
{
var block = document.createElement("div");
block.id = "block_" + i;
block.className="block";
block.style.cssText = "left:" + (i + 1000) + "px;top: 0px;background-color: green;width: 50px;height: 840px;position:absolute;";
var space = document.createElement("div");
space.style.cssText = "top:" + Math.random() * 600 + "px;background-color: gray;width: 90px;height: 240px;position: absolute";
space.className="space";
block.appendChild(space);
document.getElementById("1").appendChild(block);
}

var crashed=false;
function checkCollision()
{
var player = document.getElementById("0");
var playerRect = player.getBoundingClientRect();

var blocks = document.querySelectorAll(".block");
blocks.forEach(function (block)
{
var blockRect = block.getBoundingClientRect();

// 检查玩家是否与障碍物发生碰撞
if ( playerRect.left < blockRect.right && playerRect.right > blockRect.left && playerRect.top < blockRect.bottom && playerRect.bottom > blockRect.top )
{
crashed=true;
var spaces=document.querySelectorAll(".space");
spaces.forEach(function(space)
{
var spaceRect=space.getBoundingClientRect();
if( playerRect.left < spaceRect.right && playerRect.right > spaceRect.left && playerRect.top < spaceRect.bottom && playerRect.bottom > spaceRect.top )
{
crashed=false;
}
})
}
if(crashed)
{
clearInterval(timer);
window.alert("游戏结束!");
}
});
}
Binary file added youwen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.