Coffeescript Game Tutorial Bounce

공 튀기는 코드이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
### BEGIN LIBRARY CODE ###
x = 150
y = 150
dx = 2
dy = 4
ctx = null
WIDTH = 0
HEIGHT = 0
init = ->
canvas = document.getElementById("myCanvas")
ctx = canvas.getContext("2d")
WIDTH = canvas.width
HEIGHT = canvas.height
setInterval(draw, 10)
circle = (x, y, r) ->
ctx.beginPath()
ctx.arc(x, y, r, 0, Math.PI*2, true)
ctx.closePath()
ctx.fill()
rect = (x, y, w, h) ->
ctx.beginPath()
ctx.rect(x, y, w, h)
ctx.closePath()
ctx.fill()
clear = ->
ctx.clearRect(0, 0, WIDTH, HEIGHT)
### END LIBRARY CODE ###
draw = ->
clear()
circle(x, y, 10)
dx = -dx if x + dx > WIDTH or x + dx < 0
dy = -dy if y + dy > HEIGHT or y + dy < 0
x += dx;
y += dy;
window.onload = ->
init()

Reference

Share Comments