Coffeescript Game Tutorial the Mouse

마우스 이벤트 처리 코드를 추가하였다.

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
init_mouse = ->
canvasMinX = 0
canvasMaxX = canvasMinX + WIDTH
document.onmousemove = (evt) ->
if evt.pageX > canvasMinX and evt.pageX < canvasMaxX
paddlex = evt.pageX - canvasMinX
draw = ->
clear()
circle(x, y, 10)
# move the paddle if left or right is currently pressed
paddlex += 5 if rightDown
paddlex -= 5 if leftDown
rect(paddlex, HEIGHT-paddleh, paddlew, paddleh)
dx = -dx if x + dx > WIDTH or x + dx < 0
if y + dy < 0 then dy = -dy
else if y + dy > HEIGHT
if x > paddlex and x < paddlex + paddlew
dy = -dy
else
# game over, so stop the animation
clearInterval(intervalId)
x += dx;
y += dy;
window.onload = ->
init()
init_mouse()

Reference

Share Comments