|
樓主 |
發表於 2023-7-24 18:26:27
|
顯示全部樓層
- <style>
- body{
- background-color: #c3c3c3;
- }
- h1{
- color: #4c6ebb;
- }
- #canvas {
- width: 100%;
- background-color: black;
- }
- </style>
- <h1 align="center">my game</h1>
- <p align="center">test</p>
- <canvas id="canvas" width="600" height="400">
- You need new browser.
- </canvas>
- <script>
- window.onload = function () {
- var t = Date.now();
- let speed = 25;
- let count = 0;
- var canvas = document.getElementById("canvas");
- var context =canvas.getContext("2d");
- var x = 300;
- var y = 350;
- document.onkeydown = function () {
- count += 1;
- y -= 25;
- function draw(){
- var timePassed = (Date.now() - t) / 1000;
- t = Date.now();
- if(y <= 350) {
- speed += 50 * timePassed;
- y += speed*timePassed;
- }
- context.clearRect(0,0,600,400);
- context.beginPath();
- context.arc(x, y, 50, 0, 2*Math.PI);
- context.fillStyle="red";
- context.fill();
-
- context.font='25px Arial';
- context.fillStyle='white';
- context.fillText(count,20,30);
- console.log(count);
- window.requestAnimationFrame(draw);
- }
- draw();
- speed=25;
- if(y > 350) {
- count = 0
- }
- }
-
- document.ontouchstart = function () {
- count += 1;
- y -= 25;
- function draw(){
- var timePassed = (Date.now() - t) / 1000;
- t = Date.now();
- if(y <= 350) {
- speed += 50 * timePassed;
- y += speed*timePassed;
- }
- context.clearRect(0,0,600,400);
- context.beginPath();
- context.arc(x, y, 50, 0, 2*Math.PI);
- context.fillStyle="red";
- context.fill();
-
- context.font='25px Arial';
- context.fillStyle='white';
- context.fillText(count,20,30);
- console.log(count);
- window.requestAnimationFrame(draw);
- }
- draw();
- speed=25;
- if(y > 350) {
- count = 0
- }
- }
- }
- </script>
複製代碼 一個小互動遊戲
|
|