Pardon the mess, Play My Code is in beta!

READY TO PLAY?
CLICK TO LOG IN!

sign up - lost password

Space Hunter »

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

/**
 * Hunter - a bloody duck hunt clone.
 */

setFont( 'Arial', 32, 'italic' )

$GUN_SOUND = new Sound('gun.mp3')

$SCORE = 0

back = new Image("background.png")
showCursor( false )

shipImg = new Image('ship')
$BLOOD_IMAGE = new Image('blood.png')
$DUCK_IMAGE = {
        :right => shipImg,
         :left => shipImg
}

$birds  = []
$bloods = []

$cursor = new Cursor()

$back = new Image( getScreenWidth(), getScreenHeight() )

onEachFrame() do |delta|
    fill( :black )
    setAlpha( 1 )
    setBlend( :alpha )
    drawImage( back, 0, 0 )
    
    $back.setBlend( :clear ) do
        $back.fill( :black, 0.01 )
    end
    drawImage( $back, 0, 0 )
    
    if randBoolean(250)
        $birds.add( new Bird() )
    end
    
    $bloods.each() do |b|
        b.update(delta)
        if b.isOutside()
            $bloods.delete( b )
        end
    end
    
    $birds.each() do |b|
        b.update(delta)
        if b.isOutside()
            $birds.delete( b )
        end
    end
    
    $cursor.update( delta )
    
    $birds.each() { |b| b.draw() }
    $bloods.each() { |b| b.draw() }
    
    $cursor.draw()
    
    fillText( $SCORE + " killed", getScreenWidth()-10, 4, :right, :top )
end

class Cursor < Sprite
    def new()
        super( 0, 0, new Image('cursor.png') )
        
        @scale = 0.05
        
        @shotTime = null
    end
    
    def update( delta )
        c = getControls()
        
        x( c.getMouseX() )
        y( c.getMouseY() )
        
        diffX = (getScreenWidth()/2 - c.getMouseX()) * 2
        diffY = (getScreenHeight() - c.getMouseY()) * 9
        
        drawAngle( diffY.atan2(diffX) )
        
        if @shotTime
            if @shotTime.isExpired()
                @shotTime = null
            end
        else if c.isLeftClick()
            @shotTime = new Timer( 500 )
            $GUN_SOUND.stop().play()
            
            x = x()
            y = y()
            $birds.each() do |bird|
                if not bird.isShot()
                    shotX = x - (bird.x()-bird.image().getWidth()/2)
                    shotY = y - (bird.y()-bird.image().getHeight()/2)
                    
                    if bird.image().getPixelAlpha(shotX, shotY) > 0
                        bird.shoot()
                    end
                end
            end
        end
    end
    
    def draw()
        translate( x(), y() )
        
        if @shotTime
            delta = (@shotTime.getPercent()*180.toRadians()).sin()
            
            rotate( delta*30.toRadians() )
            
            setAlpha( 1 - 0.9*delta )
            
            shotScale = delta * 0.3
            scale = @scale + shotScale
        else
            scale = @scale
        end
        
        scale( scale )
        
        drawRotatedImage( image(), drawAngle(), 0, 0, true )
        
        if @shotTime
            undoTransform()
            setAlpha( 1 )
        end
        
        undoTransform()
        undoTransform()
    end
end

class Sprite
    attr( x, y, image, drawAngle )
    
    def new( x, y, image )
        @x = x
        @y = y
        @image = image
        @drawAngle = 0
    end
    
    def update( delta )
        
    end
    
    def isOutside()
        w2 = @image.getWidth()/2
        h2 = @image.getHeight()/2
        
        return @x < -w2 or @y < -h2 or @x > getScreenWidth()+w2 or @y > getScreenHeight()+h2
    end
    
    def draw()
        drawRotatedImage( @image, @drawAngle, @x, @y, true )
    end
end

class Blood < Sprite
    def new( x, y )
        super( x, y, $BLOOD_IMAGE )
        angle = rand( -190, 10 ).toRadians()
        
        diffX = x - getScreenWidth()/2
        diffY = y - getScreenHeight()*0.7
        angle = diffY.atan2( diffX ) + rand( -30, 30 ).toRadians()
        
        speed = rand( 3, 9 )
        @deltaX = speed*angle.cos()
        @deltaY = speed*angle.sin()
        
        @timer = new Timer( rand(600, 1200) )

        @spin = rand( -15, 15 ).toRadians()
        
        drawAngle( rand( 360 ).toRadians() )
    end
    
    def isOutside()
        return @timer.isExpired()
    end

    def update( delta )
        x( x() + @deltaX*delta )
        y( y() + @deltaY*delta )
        
        @deltaY = @deltaY + 0.1*delta
        
        drawAngle( drawAngle() + @spin*delta )
    end

    def draw()
        translate( x(), y() )
        scale = (@timer.getPercent()*90).toRadians().sin()
        scale( scale )
        
        alpha = @timer.getPercentLeft()
        setAlpha( alpha ) do
            drawRotatedImage( image(), drawAngle(), 0, 0, true )
        end
        
        undoTransform()
        undoTransform()
        
        $back.translate( x(), y() ).scale( scale ).setAlpha( alpha/3 ) do
            $back.drawRotatedImage( image(), drawAngle(), 0, 0, true )
        end
        $back.undoTransform()
        $back.undoTransform()
        
    end
end

class Bird < Sprite
    def new()
        if randBoolean()
            image = $DUCK_IMAGE[:left]
            x = getScreenWidth() + image.getWidth()
            deltaX = -1
        else
            image = $DUCK_IMAGE[:right]
            x = - image.getWidth()
            deltaX = 1
        end
        
        y = rand( 80, 280 )
        angle = rand( -5, 5 ).toRadians()
        speed = rand( 2, 5 )
        
        @deltaX = deltaX * speed*angle.cos()
        @deltaY = speed*angle.sin()
        
        super( x, y, image )
        
        @outTimer = null
    end

    def isShot()
        return ( @outTimer != null )
    end
    
    def isOutside()
        if @outTimer and @outTimer.isExpired()
            return true
        else if @deltaX < 0
            return this.x()+this.image().getWidth() < 0
        else
            return this.x()-this.image().getWidth() > getScreenWidth()
        end
    end
    
    def shoot()
        if @outTimer == null
            @outTimer = new Timer( 250 )
            @bloodTimer = new Timer( 30 )
            
            $SCORE = $SCORE+1
        end
    end
    
    def update( delta )
        if @outTimer
            @deltaY = @deltaY + 0.01
            
            if @bloodTimer.isExpired()
                @bloodTimer.reset()
                $bloods.add( new Blood(x(), y()) )
            end
        end
        
        x( x() + @deltaX*delta )
        y( y() + @deltaY*delta )
    end
    
    def draw()
        translate( x(), y() )
        
        if @outTimer
            scale( @outTimer.getPercentLeft() )
        end
        
        drawRotatedImage( image(), drawAngle(), 0, 0, true )
        
        if @outTimer
            undoTransform()
        end
        
        undoTransform()
    end
end

ERRORS

YOUR BROWSER DOES NOT SUPPORT HTML5!

Please use one of these instead

Our games cannot run in your browser