Googly Eyes looking around screen
#9
Eyes now blink (hopefully they will look more like eyes Phil, lol).   Gave the eyes something to look at too. 

Last version probably, my eyes are getting tired of looking at these.  Was a fun diversion for a while.

- Dav

Code: (Select All)
'===============
'GOOGLYEYES3.BAS
'===============
'Eyes drift around space, looking in direction they go.
'Shows how to create images off screen and use _PUTIMAGE.
'Demo also shows how to move images in interesting ways.
'Coded by Dav, NOV/2022

'V3 - Gave eyes something to look at. Made Eyes Blink

'=== First, create 4 eye images to use....

'=== Create image of eyes looking left
eyeleft& = _NEWIMAGE(230, 200, 32)
_DEST eyeleft& 'point to above image so we can draw to it
ball 50, 50, 50, 255, 255, 255 'left eye
ball 30, 50, 20, 0, 0, 128 'left pupil
ball 150, 50, 50, 255, 255, 255 'right eye
ball 130, 50, 20, 0, 0, 128 'right pupil
CIRCLE (50, 50), 50, _RGB(0, 0, 0)
CIRCLE (150, 50), 50, _RGB(0, 0, 0)
CIRCLE (30, 50), 20, _RGB(0, 0, 0)
CIRCLE (130, 50), 20, _RGB(0, 0, 0)

'=== Create image of eyes looking right
eyeright& = _NEWIMAGE(230, 200, 32)
_DEST eyeright& 'point to above image so we can draw to it
ball 50, 50, 50, 255, 255, 255
ball 70, 50, 20, 0, 0, 128
ball 150, 50, 50, 255, 255, 255
ball 170, 50, 20, 0, 0, 128
CIRCLE (50, 50), 50, _RGB(0, 0, 0)
CIRCLE (150, 50), 50, _RGB(0, 0, 0)
CIRCLE (70, 50), 20, _RGB(0, 0, 0)
CIRCLE (170, 50), 20, _RGB(0, 0, 0)

'=== Create an image of eyes looking up
eyeup& = _NEWIMAGE(230, 200, 32)
_DEST eyeup& 'point to above image so we can draw to it
ball 50, 50, 50, 255, 255, 255
ball 50, 30, 20, 0, 0, 128
ball 150, 50, 50, 255, 255, 255
ball 150, 30, 20, 0, 0, 128
CIRCLE (50, 50), 50, _RGB(0, 0, 0)
CIRCLE (150, 50), 50, _RGB(0, 0, 0)
CIRCLE (50, 30), 20, _RGB(0, 0, 0)
CIRCLE (150, 30), 20, _RGB(0, 0, 0)

'=== Create an image of eyes looking down
eyedown& = _NEWIMAGE(230, 200, 32)
_DEST eyedown& 'point to above image so we can draw to it
ball 50, 50, 50, 255, 255, 255
ball 50, 70, 20, 0, 0, 128
ball 150, 50, 50, 255, 255, 255
ball 150, 70, 20, 0, 0, 128
CIRCLE (50, 50), 50, _RGB(0, 0, 0)
CIRCLE (150, 50), 50, _RGB(0, 0, 0)
CIRCLE (50, 70), 20, _RGB(0, 0, 0)
CIRCLE (150, 70), 20, _RGB(0, 0, 0)

'=== Create an image of eyes blinking
eyeblink& = _NEWIMAGE(200, 800, 32)
_DEST eyeblink& 'point to above image so we can draw to it
ball 50, 150, 50, 196, 196, 196
ball 150, 150, 50, 196, 196, 196
ball 50, 150, 20, 64, 64, 128
ball 150, 150, 29, 64, 64, 128

CIRCLE (50, 150), 50, _RGB(0, 0, 0)
CIRCLE (150, 150), 50, _RGB(0, 0, 0)

'=== Now we point to main screen

_DEST 0 'set destination to draw to main screen
SCREEN _NEWIMAGE(800, 600, 32) 'main screen size

RANDOMIZE TIMER 'do this so the RND call is different everytime

Eyes = 75 'the number of eyes on screen
EyeSizeMax = 225 'largest size eyes can be

DIM EyeX(Eyes), EyeY(Eyes) 'x/y position of the eye
DIM EyeSize(Eyes) ' size of eye
DIM EyeGrowth(Eyes) 'eye growing or shrinking on screen
DIM EyeDrift(Eyes) 'direction eye drifts across screen
DIM EyeDriftSpeed(Eyes) 'speed for the drift
DIM EyeBlinkFlag(Eyes) 'eyes blinking flag
DIM EyeBlinkCount(Eyes)

'generate eye values
FOR d = 1 TO Eyes
    EyeX(d) = RND * _WIDTH 'make random x position
    EyeY(d) = RND * _HEIGHT 'make random y position
    EyeSize(d) = (RND * EyeSizeMax) 'random eye size, up to EyeSizeMax
    EyeGrowth(d) = INT(RND * 2) 'make way eye size is changing, 0=shrinking, 1=growing
    EyeDrift(d) = INT(RND * 4) 'make random direction a eye can drift (4 different ways)
    EyeDriftSpeed(d) = INT(RND * 3) + 2 'speed eyes will be drifting
    EyeBlinkFlag(d) = 0 'if eye is blinking or not
    EyeBlinkCount(d) = 0
NEXT


DO

    'Bubble sort through eyesize, putting smallest size first so..
    '..they will be _PUTIMAGE'd first, putting them in the background.
    FOR b = 1 TO Eyes
        FOR b2 = 1 TO Eyes
            IF EyeSize(b2) > EyeSize(b) THEN
                SWAP EyeX(b), EyeX(b2)
                SWAP EyeY(b), EyeY(b2)
                SWAP EyeSize(b), EyeSize(b2)
                SWAP EyeGrowth(b), EyeGrowth(b2)
                SWAP EyeDrift(b), EyeDrift(b2)
                SWAP EyeDriftSpeed(b), EyeDriftSpeed(b2)
                SWAP EyeBlinkFlag(b), EyeBlinkFlag(b2)
                SWAP EyeBlinkCount(b), EyeBlinkCount(b2)
            END IF
        NEXT
    NEXT

    'scroll secret built-in qb64 icon image...
    qx = qx + 1: IF qx > _WIDTH THEN qx = 0
    _PUTIMAGE (qx, 0)-(qx + _WIDTH, _HEIGHT), -11
    _PUTIMAGE (-_WIDTH + qx, 0)-(qx, _HEIGHT), -11

    'step through each eye
    FOR d = 1 TO Eyes

        'if eye is shrinking, subtract eyesize, else add to it
        IF EyeGrowth(d) = 0 THEN
            EyeSize(d) = EyeSize(d) - 1
        ELSE
            EyeSize(d) = EyeSize(d) + 1
        END IF

        'if eyesize reaches max size, switch growth to 0 start shrinking instead
        IF EyeSize(d) >= EyeSizeMax THEN EyeGrowth(d) = 0

        'if if reaches smallest eyesize, switch growth to 1 to start growing now
        IF EyeSize(d) <= 20 THEN EyeGrowth(d) = 1

        'drift eye in  1 of 4 directions we generated, and do +x,-x,+y,-y to it.
        IF EyeDrift(d) = 0 THEN EyeX(d) = EyeX(d) + EyeDriftSpeed(d) 'drift right
        IF EyeDrift(d) = 1 THEN EyeX(d) = EyeX(d) - EyeDriftSpeed(d) 'drift left
        IF EyeDrift(d) = 2 THEN EyeY(d) = EyeY(d) + EyeDriftSpeed(d) 'drift down
        IF EyeDrift(d) = 3 THEN EyeY(d) = EyeY(d) - EyeDriftSpeed(d) 'drift up

        'this creates the shakiness. randomly adjust x/y positions by +/-2 each step
        IF INT(RND * 2) = 0 THEN EyeX(d) = EyeX(d) + 2 ELSE EyeX(d) = EyeX(d) - 2
        IF INT(RND * 2) = 0 THEN EyeY(d) = EyeY(d) + 2 ELSE EyeY(d) = EyeY(d) - 2

        'below handles if eye goes off screen, let it dissapear completely
        IF EyeX(d) > _WIDTH + EyeSize(d) THEN EyeX(d) = -EyeSize(d)
        IF EyeX(d) < -EyeSize(d) THEN EyeX(d) = _WIDTH + EyeSize(d)
        IF EyeY(d) > _HEIGHT + EyeSize(d) THEN EyeY(d) = -EyeSize(d)
        IF EyeY(d) < -EyeSize(d) THEN EyeY(d) = _HEIGHT + EyeSize(d)

        'drift eye in  1 of 4 directions we generated, and +x,-x,+y,-y to it.

        'If blinking flag on...
        IF EyeBlinkFlag(d) = 1 THEN

            SELECT CASE EyeBlinkCount(d)
                CASE 0 TO 3
                    _PUTIMAGE (EyeX(d), EyeY(d))-(EyeX(d) + EyeSize(d), EyeY(d) + EyeSize(d)), eyeblink&
                CASE 4 TO 8
                    LINE (EyeX(d), EyeY(d) + (EyeSize(d) / 6))-(EyeX(d) + EyeSize(d), EyeY(d) + (EyeSize(d) / 6) + 3), _RGB(64, 64, 64), BF
                CASE 9 TO 12
                    _PUTIMAGE (EyeX(d), EyeY(d))-(EyeX(d) + EyeSize(d), EyeY(d) + EyeSize(d)), eyeblink&
            END SELECT

            EyeBlinkCount(d) = EyeBlinkCount(d) + 1
            IF EyeBlinkCount(d) > 12 THEN
                EyeBlinkCount(d) = 0
                EyeBlinkFlag(d) = 0
            END IF

        ELSE
            'showing normal eyes
            IF EyeDrift(d) = 0 THEN _PUTIMAGE (EyeX(d), EyeY(d))-(EyeX(d) + EyeSize(d), EyeY(d) + EyeSize(d)), eyeright& 'drift right
            IF EyeDrift(d) = 1 THEN _PUTIMAGE (EyeX(d), EyeY(d))-(EyeX(d) + EyeSize(d), EyeY(d) + EyeSize(d)), eyeleft& 'drift left
            IF EyeDrift(d) = 2 THEN _PUTIMAGE (EyeX(d), EyeY(d))-(EyeX(d) + EyeSize(d), EyeY(d) + EyeSize(d)), eyedown& 'drift down
            IF EyeDrift(d) = 3 THEN _PUTIMAGE (EyeX(d), EyeY(d))-(EyeX(d) + EyeSize(d), EyeY(d) + EyeSize(d)), eyeup& 'drift up
        END IF

        'get random direction change,growth and blinking once in a while
        SELECT CASE INT(RND * 300)
            CASE 1: EyeDrift(d) = 0: EyeDriftSpeed(d) = INT(RND * 3) + 2
            CASE 2: EyeDrift(d) = 1: EyeDriftSpeed(d) = INT(RND * 3) + 2
            CASE 3: EyeDrift(d) = 2: EyeDriftSpeed(d) = INT(RND * 3) + 2
            CASE 4: EyeDrift(d) = 3: EyeDriftSpeed(d) = INT(RND * 3) + 2
            CASE 5: EyeGrowth(d) = 0
            CASE 6: EyeGrowth(d) = 1
            CASE 7: IF EyeBlinkFlag(d) = 0 THEN EyeBlinkFlag(d) = 1
        END SELECT

    NEXT

    _DISPLAY
    _LIMIT 30

LOOP

SUB ball (x, y, size, r&, g&, b&)
    'small sub that draws a filled ball with given color.
    FOR s = 1 TO size STEP .4
        CIRCLE (x, y), s, _RGB(r&, g&, b&)
        r& = r& - 1: g& = g& - 1: b& = b& - 1
    NEXT
END SUB

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
Googly Eyes looking around screen - by Dav - 11-09-2022, 01:52 PM
RE: Googly Eyes looking around screen - by bplus - 11-09-2022, 04:02 PM
RE: Googly Eyes looking around screen - by Dav - 11-09-2022, 04:44 PM
RE: Googly Eyes looking around screen - by Pete - 11-09-2022, 06:15 PM
RE: Googly Eyes looking around screen - by Pete - 11-09-2022, 11:49 PM
RE: Googly Eyes looking around screen - by Dav - 11-10-2022, 04:40 PM
RE: Googly Eyes looking around screen - by Pete - 11-10-2022, 04:46 PM
RE: Googly Eyes looking around screen - by bplus - 11-10-2022, 05:08 PM
RE: Googly Eyes looking around screen - by bplus - 11-10-2022, 05:43 PM
RE: Googly Eyes looking around screen - by Pete - 11-10-2022, 06:30 PM



Users browsing this thread: 3 Guest(s)