Googly Eyes looking around screen
#1
Small demo of Googly eyes drifting around the screen.  The eyes look toward the direction they are moving.  Just a small demo that may help some learn to use _PUTIMAGE and move images around the screen in different ways.  I tried to comment more than usual.

- Dav

Code: (Select All)
'==============
'GOOGLYEYES.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


'=== 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, 32 'left pupil
ball 150, 50, 50, 255, 255, 255 'right eye
ball 130, 50, 20, 0, 0, 32 'right pupil

'=== 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, 32
ball 150, 50, 50, 255, 255, 255
ball 170, 50, 20, 0, 0, 32

'=== 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, 32
ball 150, 50, 50, 255, 255, 255
ball 150, 30, 20, 0, 0, 32

'=== 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, 32
ball 150, 50, 50, 255, 255, 255
ball 150, 70, 20, 0, 0, 32


'=== 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 = 100 '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

'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
NEXT


DO

    CLS , _RGB(0, 0, 32) 'clear screen to dark blue

    '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 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

        'get new random direction change
        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
        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: 6 Guest(s)