Skip to content

Commit

Permalink
Draw ball. Move ball by cursor keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenzan100 committed Dec 28, 2020
1 parent d13f560 commit 5baa3f8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
<html>
<head>
<meta charset="utf-8" />
<title>Your. First. Game. Ever.</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; display: block; margin: 0 auto; }
</style>
</head>
<body>
Hi.
<canvas id="myCanvas" width="480" height="320"></canvas>
<script src="main.js"></script>
</body>
</html>
45 changes: 45 additions & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
console.log('Hello');

const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

let x = 10;
let y = 10;
let dx = 0;
let dy = 0;

function draw_ball(x, y) {
ctx.beginPath();
ctx.arc(x, y, 20, 0, Math.PI*2, false);
ctx.fillStyle = "green";
ctx.fill();
ctx.closePath();
}

const inputs = {
keyDownActions: {
'Left': () => { dx = -2; dy = 0; },
'Right': () => { dx = 2; dy = 0; },
'Up': () => { dy = -2; dx = 0; },
'Down': () => { dy = 2; dx = 0; },
},

keyDownHandler(e) {
let val = e.key.replace('Arrow', '');
const fn = this.keyDownActions[val];
if (typeof fn == 'function') {
fn();
};
},
};

const renderer = {
render() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
x += dx;
y += dy;
draw_ball(x, y);
},
};

document.addEventListener("keydown", inputs.keyDownHandler.bind(inputs), false);
setInterval(renderer.render, 1000/60);

1 comment on commit 5baa3f8

@kenzan100
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. see the circle appears on the canvas.
  2. see your cursor key can move the circle.

361a77072f71cf0eb0c425bf0b4e955b

Please sign in to comment.