Pardon the mess, Play My Code is in beta!

READY TO PLAY?
CLICK TO LOG IN!

sign up - lost password

Space Dodge »

You do not own this project, so changes will not be saved

$y = 190
$x = 10
$score = 0
$ammo = 50
$speed = 2.5

$asteroids = []
$shots = []

$run = true

$bg = new Image('bg.png')
$ship = new Image('ship.png')
$asteroid = new Image('asteroid.png')

$die = new Sound('die.wav')
$level = new Sound('level.wav')
$points = new Sound('points.wav')
$shot = new Sound('shot.wav')
$music = new Sound('loop1.mp3')

$music.setRepeating(true)
$music.play()

c = getControls()

timer = new Timer(1500 / $speed)

setFont('courier', 20, 'bold')

class Asteroid
    def new(startx)
        @x = startx
        @y = rand(0, 360)
    end
    def update(self, delta)
        @x = @x - $speed * delta
        if @x < -26
            $asteroids.delete(self)
        end
        drawImage($asteroid, @x, @y, false)
        if isRectOverlap(@x, @y, 26, 26, $x, $y, 34, 20, false)
            $run = false
            $music.stop()
            $die.play()
        end
    end
    def getX()
        return @x
    end
    def getY()
        return @y
    end
end

class Shot
    def new(startx, starty)
        @x = startx
        @y = starty
        setColor(:white)
        fillRect(@x, @y, 6, 2, false)
    end
    def update(self, delta)
        @x = @x + ($speed * delta)
        fillRect(@x, @y, 6, 2, false)
        $asteroids.each() do |asteroid|
            if isRectOverlap(@x, @y, 6, 2, asteroid.getX(), asteroid.getY(), 26, 26, false)
                $asteroids.delete(asteroid)
                $shots.delete(self)
                $points.play()
                $ammo = $ammo + 1
                $score = $score + 10
            end
        end
    end
end

def shipUpdate()
    if $y < 0
        $y = 380
    end
    if $y > 380
        $y = 0
    end
    if $x < 0
        $x = 0
    end
    if $x > 166
        $x = 166
    end
    drawImage($ship, $x, $y, false)
end

def textUpdate()
    $score = $score + 1
    fillText("Score: " + $score, 5, 5)
    fillText("Ammo: " + $ammo, 5, 390, :left, :bottom)
end

def speedUpdate()
    if $score > 2500 && $speed < 7
        $speed = 7
        $level.play()
    elseif $score > 2000 && $speed < 6
        $speed = 6
        $level.play()
    elseif $score > 1500 && $speed < 5
        $speed = 5
        $level.play()
    elseif $score > 1000 && $speed < 4
        $speed = 4
        $level.play()
    elseif $score > 500 && $speed < 3
        $speed = 3
        $level.play()
    end
end

onEachFrame() do |delta|
    if $run == true
        fill( :black )
        drawImage($bg, 0, 0, false)
        
        if c.isKeyDown(:up)
            $y = $y - ($speed * delta)
        end
        if c.isKeyDown(:down)
            $y = $y + ($speed * delta)
        end
        if c.isKeyDown(:left)
            $x = $x - ($speed * delta)
        end
        if c.isKeyDown(:right)
            $x = $x + ($speed * delta)
        end
        if c.isKeyPressed(:space)
            if $ammo > 0
                $shots.add(new Shot($x + 34, $Y + 8))
                $ammo = $ammo - 1
                $shot.play()
            end
        end
        
        shipUpdate()
        
        textUpdate()
        
        speedUpdate()

        if timer.isExpired()
            $asteroids.add(new Asteroid(600))
            timer = new Timer(1500 / $speed * delta)
        end
        $shots.each() do |shot|
            shot.update(shot, delta)
        end
        $asteroids.each() do |asteroid|
            asteroid.update(asteroid, delta)
        end
        if $run == false
            fillText("Press space to restart", 310, 220, true)
            setFont('courier', 32, 'bold')
            fillText("gameover", 310, 180, true)
        end
    else
        if c.isKeyPressed(:space)
            $y = 190
            $x = 10
            $score = 0
            $ammo = 50
            $speed = 2.5
            $asteroids = []
            $shots = []
            $run = true
            timer = new Timer(1500 / $speed * delta)
            setFont('courier', 20, 'bold')
            $music.play()
        end
    end
end

ERRORS

YOUR BROWSER DOES NOT SUPPORT HTML5!

Please use one of these instead

Our games cannot run in your browser