123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="home">
    <div class="container">
<!--        <h1>Welcome!</h1>-->
<!--        <p>maybe you would like to play a little game of Arkanoid while you wait? :)</p>-->
        <a href="#" id="start">Insert new coin</a><br/>
        <canvas id="board" width="800" height="480" style="border:1px solid black"></canvas>
        <ol>
            <li>click on the board to start</li>
            <li>use mouse to move the paddle</li>
            <li>clear all the blocks by shooting at them with the ball</li>
            <li>loose a "life" when you "loose" the ball</li>
            <li>add a new "life" by clicking the "insert new coin" link; game will be faster each time</li>
            <li>some physics is off, this is on purpose to make it extra hard (*kuch*)</li>
            <li>be sure to ping me when you managed to clear the board!</a></strong> :)</li>
        </ol>
<br/>
        <p><em><small>Disclaimer: Game is made by me - I typed every single line of code; but I used diff. resources on the net. The goal is to refresh some physic principals, to have fun and to learn some coffeescripting, html5 and game development on the way. Since it's a learning thing, the game is not perfect and kinda buggy :)
        <br/>
        <br/>
        &copy; DaktaDeo 2013 - All rights reserved</small></em></p>
    </div>
</div>
            
          
!
            
              #$ ?= require 'jquery' # For Node.js compatibility
class Board
  constructor:(@x,@y,@width,@height)->
  showmessage:(msg,$context) ->
    clear()
    $context.textAlign="center"
    $context.font ="bold 20px sans-serif"
    $context.fillStyle="black"
    $context.fillText(msg,$board.width/2,$board.height/2,$board.height)

class Block
  constructor:(@x,@y,@width,@height,@row,@color,@hit) ->
    @hit=0
  draw: ($context)->
    if @hit ==0 || @hit == undefined
      $context.fillStyle=@color
      $context.beginPath()
      $context.rect(@x+1,@y+1,@width-1,@height-1)
      $context.closePath()
      $context.fill()
  explode:()->
    @hit=1
    console.log "bwam"
  collides:($ball)->
    if collision($ball.x,$ball.y,$ball.diameter,@x+1,@y+1,@width,@height) && @hit==0
      $ball.dx = -$ball.dx
      $ball.dy = -$ball.dy
      this.explode()

class Ball
  constructor:(@x,@y,@diameter,@dx,@dy,@color) ->
  draw:($context) ->
    $context.beginPath()
    $context.fillStyle=@color
    # Draws a circle of radius 20 at the coordinates 100,100 on the canvas
    $context.arc(@x,@y,@diameter,0,Math.PI*2,true)
    $context.closePath()
    $context.fill()

class Paddle
  constructor:(@x,@y,@width,@height,@row,@color,@hits) ->
    @hits=0
  draw: ($context)->
    #if @hit ==0 || @hit == undefined
    $context.fillStyle=@color
    $context.beginPath()
    $context.rect(@x+1,@y+1,@width-1,@height-1)
    $context.closePath()
    $context.fill()
  explode:()->
    @hit+=1
    #console.log "ping"
    #$board.showSound("ping",$context)
  collides:($ball)->
    if collision($ball.x,$ball.y,$ball.diameter,@x+1,@y+1,@width,@height)
      newY = ($paddle.y + ($paddle.height/2)) - $ball.y
      normalisedY = newY/($paddle.height/2)
      angle = normalisedY * 75 # 75 = maxbounceangle

      $ball.dx = 5 * Math.cos(angle) #10 = ballspeed
      $ball.dy = 5 * -Math.sin(angle)

      if $ball.x < @width/2 + @x+1
        $ball.dx = -$ball.dx

      #$ball.dy = -$ball.dy
      this.explode()
#vars
coin = 1
$game = null
$context=null
$board = null
$paddle = null
$ball  = new Ball(150,150,10,5,5,"#c0392b")
$blocks=[]

$colors = [
  ["#2f484b", "#2f484b","#2f484b", "#2f484b", "#2f484b", "#7fa6aa", "#7fa6aa", "#7fa6aa", "#fbb03b"],
  ["#990033", "#B2003C", "#CB0045", "#E4004E", "#FE0057", "#FF1967", "#FF3378"],
  ["#FFDF81", "#FFD868", "#FFD24E", "#FFCB34", "#FFC51A", "#FFBF00", "#E6AC00", "#CC9900"],
  ["#990000", "#B20000", "#CB0000", "#E40000", "#FE0000", "#FF1919", "#FF3333", "#FF4D4D", "#FF6767"],
  ["#003300", "#004C00", "#006500", "#007E00", "#009700", "#00B000", "#00C900", "#00E200", "#00FB00", "#16FF15"],
  ["#003366", "#004080", "#004D99", "#005AB2", "#0067CB", "#0074E4", "#0081FE", "#198EFF", "#339BFF", "#4DA7FF", "#67B4FF"]
]
$level1=[
  [1,1,1,1,1,0,0,0],
  [1,1,1,1,1,0,0,0],
  [1,1,1,1,1,0,0,0],
  [1,1,1,1,1,0,0,0],
  [1,1,1,1,1,0,0,0],
  [0,0,0,0,0,1,1,1],
  [0,0,0,0,0,1,1,1],
  [0,0,0,0,0,1,1,1],
  [0,0,0,0,0,0,0,1]
]

$level2=[
  [0,1,1,1,1,1,1,0],
  [1,1,1,1,1,1,1,1],
  [1,1,1,0,0,1,1,1],
  [0,1,1,1,1,1,1,0]
]


$(document).ready ->
  #init
  $resetcount=0
  $context = $("#board")[0].getContext("2d")
  $board = new Board(0,0,$("#board").width(),$("#board").height())
  $paddle =new Paddle(210,$board.height-10,150,10,0,"#000")

  $("#board").on("click", (ev) ->
   ev.preventDefault()
   if coin == 1
    coin=0
    startgame()
  )

  $("#start").on("click",(ev) ->
    ev.preventDefault()
    console.log(coin)
    coin =1
    $resetcount += 1
    if $resetcount == 5
      $board.showmessage("I'm BROKE :(",$context)
    else
      $ball  = new Ball(150,150,10,2,2,"#c0392b")
      $paddle = new Paddle(210,$board.height-10,150-($resetcount*30),10,0,"#000")
      drawgame()
  )

  #mouse events
  $("#board").on("mousemove",(ev)->
    $paddle.x = mouse(ev).x #- $(this).offsetLeft + 300 - ($board.width/2)
  )

  #generate all the bricks
  createRow i, row for row,i in $level1

  #start game
  drawgame()
  #$game = setInterval(drawgame,10)

drawgame=()->
  clear()

  block.draw($context) for block in $blocks

  $ball.draw($context)
  $paddle.draw($context)

  #collision detection: playground
  if $ball.x<0 || $ball.x >$board.width
    $ball.dx = -$ball.dx

  if $ball.y<0 #|| $ball.y>$board.height
    $ball.dy = -$ball.dy
  else if $ball.y>$board.height
    #you just died
    $board.showmessage("GAME OVER",$context)
    clearInterval($game)

  $ball.x += $ball.dx
  $ball.y += $ball.dy

  #collision detection: paddle
  $paddle.collides($ball)

  #collision detection: blocks
  block1.collides($ball) for block1 in $blocks

collision = (circleX, circleY, radius, squareX, squareY, width, height) ->
  distance = 0
  if (circleX < squareX)
    distance += Math.pow(circleX - squareX, 2)
  else if (circleX > squareX + width)
    distance += Math.pow(circleX - squareX - width, 2)

  if (circleY < squareY)
    distance += Math.pow(circleY - squareY, 2)
  else if (circleY > squareY + height)
    distance += Math.pow(circleY - squareY - height, 2)

  return distance <= Math.pow(radius, 2)


clear=()->
  $context.clearRect($board.x,$board.y, $board.width,$board.height)

createRow=(index,$row) ->
  createBlock i, block,index for block,i in $row

createBlock=(index,$block,row) ->
  if $block ==1
    $tile = new Block(100*index,20*row,100,20,row,$colors[0][row])
    $blocks.push($tile)

mouse=(ev)->
  rect = $("#board")[0].getBoundingClientRect()
  return {
    x: ev.clientX - rect.left,
    y: ev.clientY - rect.top
  }

startgame=()->
  $game = setInterval(drawgame,5)
window.startgame=startgame
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console