Skip to content

Instantly share code, notes, and snippets.

@rkmathi
Created June 6, 2013 13:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkmathi/5721344 to your computer and use it in GitHub Desktop.
Save rkmathi/5721344 to your computer and use it in GitHub Desktop.
WIN_SIZE = [320, 320]
CTK_IMG = "ctk.png"
CTK_SIZE = [80, 80]
CTK_MOVE = 10
ITF_IMG = "itf.png"
ITF_SIZE = [90, 60]
ITF_MOVE = 10
enchant()
class CtkGame extends Game
constructor : ->
super WIN_SIZE[0], WIN_SIZE[1]
@fps = 30
CtkGame.game = @
@preload CTK_IMG
@preload ITF_IMG
@onload = ->
@rootScene.backgroundColor = 'black'
player = new Ctk(WIN_SIZE[0]/2-CTK_SIZE[0]/2, WIN_SIZE[1]-CTK_SIZE[1])
@rootScene.addChild(player)
enemy = new Itf(0, 0)
@rootScene.addChild(enemy)
@rootScene.addEventListener 'enterframe', ->
if (player.x < enemy.x+ITF_SIZE[0]-30 && player.x+CTK_SIZE[0] > enemy.x+30)
if (player.y < enemy.y+ITF_SIZE[1]-30 && player.y+CTK_SIZE[1] > enemy.y+30)
@stop()
@start()
class Ctk extends Sprite
constructor: (x, y) ->
super CTK_SIZE[0], CTK_SIZE[1]
@x = x
@y = y
game = CtkGame.game
@image = game.assets[CTK_IMG]
@addEventListener 'enterframe', ->
if game.input.up && @y > 0
@y -= CTK_MOVE
else if game.input.down && @y < WIN_SIZE[1]-CTK_SIZE[1]
@y += CTK_MOVE
if game.input.left && @x > 0
@x -= CTK_MOVE
@scaleX *= -1 if @scaleX < 0
else if game.input.right && @x < WIN_SIZE[0]-CTK_SIZE[0]
@x += CTK_MOVE
@scaleX *= -1 if @scaleX > 0
class Itf extends Sprite
constructor: (x, y) ->
super ITF_SIZE[0], ITF_SIZE[1]
@x = x
@y = y
game = CtkGame.game
@image = game.assets[ITF_IMG]
xx = Math.floor(Math.random()*(WIN_SIZE[0]-ITF_SIZE[0]))
yy = Math.floor(Math.random()*(WIN_SIZE[1]-ITF_SIZE[1]))
to = [xx, yy]
console.log to
@addEventListener 'enterframe', ->
if @x <= to[0]
@x += ITF_MOVE
else if @x >= to[0]
@x -= ITF_MOVE
if @y <= to[1]
@y += ITF_MOVE
else if @y >= to[1]
@y -= ITF_MOVE
if to[0]-5 <= @x <= to[0]+5
to[0] = Math.floor(Math.random()*(WIN_SIZE[0]-ITF_SIZE[0]))
if to[1]-5 <= @y <= to[1]+5
to[1] = Math.floor(Math.random()*(WIN_SIZE[1]-ITF_SIZE[1]))
window.onload = ->
new CtkGame()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment