Screen Scrolling

New to FreeBASIC? Post your questions here.
Clay Dragon
Posts: 15
Joined: Jul 30, 2016 2:05
Location: Canada

Screen Scrolling

Postby Clay Dragon » Jul 30, 2016 2:11

Hello!

I have a small problem I can't figure out! You see, I've found that nice scrolling pixel by pixel freebasic demo from lachie's website http://lachie.phatcode.net/fbscroll.php It does a really nice job full screen but what I really want is it to do is scrolling only a part of the screen.

I'm almost done with the screen part but it doesn't scroll properly. Here's the link to the file of my work http://claydragon.ca/tmp/scrolling_zone.zip
I would be really happy for getting help with this problem!

Thank you!
BasicCoder2
Posts: 3397
Joined: Jan 01, 2009 7:03

Re: Screen Scrolling

Postby BasicCoder2 » Jul 30, 2016 9:36

Your source code is too complex for me to untangle so all I can show is how I would do it.
I make heavy use of bitmaps. So in your example I would draw my tiles onto a bitmap which I have called display in the example below and then copy a window on that display to what ever position you want on the screen. This window is scrolled across the display map.

Code: Select all

screenres 640,480,32

const DISPLAYW = 1280
const DISPLAYH = 960
dim shared as integer WINW   = 320  'width of viewport window
dim shared as integer WINH   = 240  'height of viewport window
dim shared as integer WINX   = 0    'top/left position of viewport in DISPLAY bitmap
dim shared as integer WINY   = 0    'changed by scrolling
dim shared as integer POSX   = 160  'top/left position of viewport on display
dim shared as integer POSY   = 120

dim shared as any ptr display
display = imagecreate(DISPLAYW,DISPLAYH) 'twice size of display screen

'create background image
line DISPLAY,(0,0)-(DISPLAYW-1,DISPLAYH-1),rgb(100,244,100),bf  'green grass
'fill with objects to make scroll visible
dim as integer x,y
for i as integer = 0 to 200
    x = int(rnd(1)*DISPLAYW)
    y = int(rnd(1)*DISPLAYH)
    circle DISPLAY,(x,y),15,rgb(int(rnd(1)*256),int(rnd(1)*256),int(rnd(1)*256)),,,,f
next i

sub update()
    screenlock
    cls
    print:print " USE CURSOR KEYS TO SCROLL DISPLAY"
    print:print " WINX = ";WINX;"  WINY = ";WINY
    put (POSX,POSY),DISPLAY,(WINX,WINY)-(WINX+WINW-1,WINY+WINH-1),trans
    screenunlock
end sub


do
   
        if multikey(&H4D) then
            WINX = WINX + 1
            if WINX >= DISPLAYW-WINW then WINX = DISPLAYW-WINW
        end if
       
        if multikey(&H4B) then
            WINX = WINX - 1
            if WINX < 0 then WINX = 0
        end if
       

        if multikey(&H50) then
            WINY = WINY + 1
            if WINY >= DISPLAYH-WINH then WINY = DISPLAYH-WINH
        end if
       
        if multikey(&H48) then
            WINY = WINY - 1
            if WINY < 0 then WINY = 0
        end if
       
        update
       
        sleep 2

loop until multikey(&H01)

sleep
Last edited by BasicCoder2 on Jul 30, 2016 18:18, edited 1 time in total.
Lothar Schirm
Posts: 333
Joined: Sep 28, 2013 15:08
Location: Bavaria, Germany

Re: Screen Scrolling

Postby Lothar Schirm » Jul 30, 2016 17:51

Cool! And quite simple!
BasicCoder2
Posts: 3397
Joined: Jan 01, 2009 7:03

Re: Screen Scrolling

Postby BasicCoder2 » Jul 30, 2016 19:09

Of course the position of the window in a game is controlled by the position of the character.
The display bitmap is now just the drawing area. All sorts of stuff and bitmap layers can be painted there before it is copied to the screen.

Code: Select all

screenres 640,480,32

type SPRITE
    as integer x
    as integer y
    as integer w
    as integer h
    as any ptr img
end type
dim shared as SPRITE hero

const MAPW = 1280   'size of map in pixels
const MAPH = 960
dim shared as integer WINW   = 320  'width of viewport window
dim shared as integer WINH   = 240  'height of viewport window
dim shared as integer WINX   = 0    'top/left position of viewport in background bitmap
dim shared as integer WINY   = 0    'changed by scrolling
dim shared as integer POSX   = 160  'top/left position of viewport on display
dim shared as integer POSY   = 120

dim shared as any ptr display
display = imagecreate(MAPW,MAPH)    'twice size of window display screen
dim shared as any ptr background
background = imagecreate(MAPW,MAPH) '

hero.img = imagecreate(16,32,rgb(255,0,255))
line hero.img,(0,0)-(15,31),rgb(0,0,50),bf   'blue block
line hero.img,(0,0)-(15,31),rgb(0,0,0),b
hero.x = 30
hero.y = 30
hero.w = 16
hero.h = 32

'create background image
line background,(0,0)-(MAPW-1,MAPH-1),rgb(100,244,100),bf  'green grass
'fill with objects to make scroll visible
dim as integer x,y
for i as integer = 0 to 200
    x = int(rnd(1)*MAPW)
    y = int(rnd(1)*MAPH)
    circle background,(x,y),15,rgb(int(rnd(1)*256),int(rnd(1)*256),int(rnd(1)*256)),,,,f
next i

sub update()
    screenlock
    cls
    'copy background bitmap to display bitmap to erase any sprites
    put display,(0,0),background,trans
    'draw sprite onto display bitmap
    put display,(hero.x,hero.y),hero.img,trans
    'copy window on display area to screen
    put (POSX,POSY),display,(WINX,WINY)-(WINX+WINW-1,WINY+WINH-1),trans
    print:print " USE CURSOR KEYS TO SCROLL background"
    print:print " WINX = ";WINX;"  WINY = ";WINY
    screenunlock
end sub

do
   
        if multikey(&H4D) then
            hero.x = hero.x + 1
            if hero.x >= MAPW-hero.w then hero.x = MAPW-hero.w
        end if
       
        if multikey(&H4B) then
            hero.x = hero.x - 1
            if hero.x < 0 then hero.x = 0
        end if
       

        if multikey(&H50) then
            hero.y = hero.y + 1
            if hero.y >= MAPH-hero.h then hero.y = MAPH-hero.h
        end if
       
        if multikey(&H48) then
            hero.y = hero.y - 1
            if hero.y < 0 then hero.y = 0
        end if
       
        'adjust position of window on background
        WINX = hero.x - (WINW\2)
        if WINX < 0 then WINX = 0
        if WINX >= MAPW-WINW then WINX = MAPW-WINW
        WINY = hero.y - (WINH\2)
        if WINY < 0 then WINY = 0
        if WINY >= MAPH-WINH then WINY = MAPH-WINH
       
        update
       
        sleep 2

loop until multikey(&H01)

sleep

Clay Dragon
Posts: 15
Joined: Jul 30, 2016 2:05
Location: Canada

Re: Screen Scrolling

Postby Clay Dragon » Jul 31, 2016 3:59

Thank you so much for your help, BasicCoder2!

Can't wait to try it tomorrow!

Cheers!
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: Screen Scrolling

Postby leopardpm » Jul 31, 2016 23:49

BasicCoder2 is awesome!

(from anonymous fan)
BasicCoder2
Posts: 3397
Joined: Jan 01, 2009 7:03

Re: Screen Scrolling

Postby BasicCoder2 » Aug 01, 2016 0:22

@leopardpm, I thought you had vanished from the scene?

I couldn't resist trying to copy some more of the demo.

First you need the sprites and tiles for the demo,

This will generate the player sprite block,

Code: Select all

'The the images generated are those used by Richard Eric Lope's pixel by pixel scrolling engine.
'http://lachie.phatcode.net/fbscroll.php
screenres 40,80,32
cls
dim as ulong colors(13)
for i as integer = 0 to 13
    read colors(i)
next i
dim as ulong v

dim as string datum,c
for j as integer = 0 to 79
    read datum
    for i as integer = 0 to 39
        c = mid(datum,i+1,1)
        pset (i,j),colors(asc(c)-65)
    next i
next j
bsave "spriteBlock2.bmp",0


sleep

data &HFFFF00FF
data &HFF94704C
data &HFFD8B800
data &HFFFCFCFC
data &HFF006CD8
data &HFFC89400
data &HFF005CB8
data &HFF0064C8
data &HFF543800
data &HFF808080
data &HFFA0A0A0
data &HFF383838
data &HFF785400
data &HFF745838

DATA "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
DATA "AAAAAAAABBBBAAAAAAAAAAAAAAAABBBBAAAAAAAA"
DATA "AAAAAAABBBBBBAAAAAAAAAAAAAABBBBBBAAAAAAA"
DATA "AAAAAABBBCBBBBAAAAAAAAAAAABBBCBBBBAAAAAA"
DATA "AAAAAABBCCCCBBAAAAAAAAAAAABBCCCCBBAAAAAA"
DATA "AAAAAABDECCEDAAAAAAAAAAAAABDECCEDAAAAAAA"
DATA "AAAAAAADECCEDAAAAAAAAAAAAAADECCEDAAAAAAA"
DATA "AAAAAAAAFCCFAAAAAAAAAAAAAAAAFCCFAAAAAAAA"
DATA "AAAAAAGGHFFHGIAAAAAAAAAAAAGGHFFHGIAAAAAA"
DATA "AAAAAGGGGHHGIGGAAAAAAAAAAGGGGHHGIGGAAAAA"
DATA "AAAAAGGGGGGIGGGAAAAAAAAAAGGGGGGIGGGAAAAA"
DATA "AAAAAGGCGGIGCGGAAAAAAAAAAGGCGGIGCGGAAAAA"
DATA "AAAAAACFGIGGFCAAAAAAAAAAAACFGIGGFCAAAAAA"
DATA "AAAAAAI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A"
DATA "AAAAAAAALLLLLLAAAAAAAAAAAAAALLLLLLAAAAAA"
DATA "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
DATA "AAAAAAAABBBBAAAAAAAAAAAAAAAABBBBAAAAAAAA"
DATA "AAAAAAABBBBBBAAAAAAAAAAAAAABBBBBBAAAAAAA"
DATA "AAAAAABBBBBBBBAAAAAAAAAAAABBBBBBBBAAAAAA"
DATA "AAAAAABBBBBBBBAAAAAAAAAAAABBBBBBBBAAAAAA"
DATA "AAAAAABBBBBBBAAAAAAAAAAAAABBBBBBBAAAAAAA"
DATA "AAAAAAABBBBBBAAAAAAAAAAAAAABBBBBBAAAAAAA"
DATA "AAAAAAAAFCBFAAAAAAAAAAAAAAAAFCBFAAAAAAAA"
DATA "AAAAAAGIHFFHGGAAAAAAAAAAAAGIHFFHGGAAAAAA"
DATA "AAAAAGGGIHHGGGGAAAAAAAAAAGGGIHHGGGGAAAAA"
DATA "AAAAAGGGGIGGGGGAAAAAAAAAAGGGGIGGGGGAAAAA"
DATA "AAAAAGHCGGIGCHGAAAAAAAAAAGHCGGIGCHGAAAAA"
DATA "AAAAAAFFGGGIFFAAAAAAAAAAAAFFGGGIFFAAAAAA"
DATA "AAAAAAAGGGGGIAAAAAAAAAAAAAAGGGGGIIAAAAAA"
DATA "AAAAAAAIIIIIIIAAAAAAAAAAAAAIIIIIIIIAAAAA"
DATA "AAAAAAAGGGGGGIIAAAAAAAAAAAAGGGGGGIIAAAAA"
DATA "AAAAAAAGGGGGGIIAAAAAAAAAAAAGGGGGGAAAAAAA"
DATA "AAAAAAACCCGGGAAAAAAAAAAAAAAGGGCCCAAAAAAA"
DATA "AAAAAALMMMCCCLAAAAAAAAAAAALCCCMMMLAAAAAA"
DATA "AAAAALLLLLMMMLLAAAAAAAAAALLMMMLLLLLAAAAA"


Then some sprites for the others characters,

Code: Select all

'The the images generated are those used by Richard Eric Lope's pixel by pixel scrolling engine.
'http://lachie.phatcode.net/fbscroll.php
screenres 40,80,32
cls
dim as ulong colors(11)
for i as integer = 0 to 11
    read colors(i)
next i
dim as ulong v

dim as string datum,c
for j as integer = 0 to 79
    read datum
    for i as integer = 0 to 39
        c = mid(datum,i+1,1)
        pset (i,j),colors(asc(c)-65)
    next i
next j
bsave "spriteBlock3.bmp",0


sleep
data &HFFFF00FF
data &HFFA87400
data &HFFC89400
data &HFFD8B800
data &HFFFCFCFC
data &HFF006CD8
data &HFF6C4C00
data &HFF006400
data &HFF008400
data &HFF444444
data &HFF9C6C00
data &HFF383838

DATA "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
DATA "AAAAAAAABBBBAAAAAAAAAAAAAAAABBBBAAAAAAAA"
DATA "AAAAAAABBBBBBAAAAAAAAAAAAAABBBBBBAAAAAAA"
DATA "AAAAAABBBCBBBBAAAAAAAAAAAABBBCBBBBAAAAAA"
DATA "AAAAAABBCDDCBBAAAAAAAAAAAABBCDDCBBAAAAAA"
DATA "AAAAAABEFDDFEBAAAAAAAAAAAABEFDDFEBAAAAAA"
DATA "AAAAAAABBBBBBAAAAAAAAAAAAAABBBBBBAAAAAAA"
DATA "AAAAAGAABDDBAAAAAAAAAAAAAGAABDDBAAAAAAAA"
DATA "AAAAAGGHBBBBIIAAAAAAAAAAAGGHBBBBIIAAAAAA"
DATA "AAAAAGGHHBBHHHIAAAAAAAAAAGGHHBBHHHIAAAAA"
DATA "AAAAAIGGHHHHHHIAAAAAAAAAAIGGHHHHHHIAAAAA"
DATA "AAAAAHGDHHHHDIIAAAAAAAAAAHGDHHHHDIIAAAAA"
DATA "AAAAAADDHHHHDDAAAAAAAAAAAADDHHHHDDAAAAAA"
DATA "AAAAAAGGHHHIIAAAAAAAAAAAAAGGHHHIIAAAAAAA"
DATA "AAAAAAAGHHIIIAAAAAAAAAAAAAAGHHIIIAAAAAAA"
DATA "AAAAAAAGGHHIIAAAAAAAAAAAAAAGGHHIIAAAAAAA"
DATA "AAAAAAAGGHHHIAAAAAA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
DATA "AAAAAAABBBBBBAAAAAAAAAAAAAABBBBBBAAAAAAA"
DATA "AAAAAABBBBBBBBAAAAAAAAAAAABBBBBBBBAAAAAA"
DATA "AAAAAABBBBBBBBAAAAAAAAAAAABBBBBBBBAAAAAA"
DATA "AAAAAABBBBBBBBAAAAAAAAAAAABBBBBBBBAAAAAA"
DATA "AAAAAGABBBBBBAAAAAAAAAAAAGABBBBBBAAAAAAA"
DATA "AAAAAGGABBBBAAAAAAAAAAAAAGGABBBBAAAAAAAA"
DATA "AAAAAGHHBHHBIIAAAAAAAAAAAGHHBHHBIIAAAAAA"
DATA "AAAAAHHHHHHHHHIAAAAAAAAAAHHHHHHHHHIAAAAA"
DATA "AAAAAHHHHHHHHHIAAAAAAAAAAHHHHHHHHHIAAAAA"
DATA "AAAAAHHHHHHHHIIAAAAAAAAAAHHHHHHHHIIAAAAA"
DATA "AAAAAADHHHHHHDAAAAAAAAAAAADHHHHHHDAAAAAA"
DATA "AAAAAAGHHHHHIAAAAAAAAAAAAAGHHHHHIAAAAAAA"
DATA "AAAAAAGHHHHHHAAAAAAAAAAAAAGHHHHHHAAAAAAA"
DATA "AAAAAAAHHHHHIAAAAAAAAAAAAAAHHHHHIAAAAAAA"
DATA "AAAAAAAHHHHHIAAAAAAAAAAAAAAHHHHHIAAAAAAA"
DATA "AAAAAAADDDHHHAAAAAAAAAAAAAAHHHDDDAAAAAAA"
DATA "AAAAAAJKKKDDDJAAAAAAAAAAAAJDDDKKKJAAAAAA"
DATA "AAAAAJJJJJKKKJJAAAAAAAAAAJJKKKJJJJJAAAAA"


And a reduced set of tiles where tile 0 and tile 1 are walkable,

Code: Select all

screenres 320,20,32
cls
dim as ulong colors(40)
for i as integer = 0 to 40
    read colors(i)
next i
dim as ulong v

dim as string datum,c
for i as integer = 0 to 319
    read datum
    for j as integer = 0 to 19
        c = mid(datum,j+1,1)
        pset (i,j),colors(asc(c)-65)
    next j
next i
bsave "tileBlock2.bmp",0


sleep

data &HFF00C800
data &HFF00B800
data &HFF00D800
data &HFF007400
data &HFF00A800
data &HFF808080
data &HFF606060
data &HFF707070
data &HFF444444
data &HFFD8D800
data &HFF505050
data &HFF006CD8
data &HFF0074E8
data &HFFAE8557
data &HFFA0A0A0
data &HFF2C2C2C
data &HFF202020
data &HFFE80000
data &HFF005CB8
data &HFF0064C8
data &HFF008400
data &HFFB89B5C
data &HFFDB926D
data &HFF141414
data &HFFC80000
data &HFFB80000
data &HFFD80000
data &HFF0080FC
data &HFFDCB06E
data &HFF383838
data &HFF926D49
data &HFF940000
data &HFF005400
data &HFF004000
data &HFF643000
data &HFF944C00
data &HFFA85400
data &HFFC86400
data &HFF4F964B
data &HFFD86C00
data &HFF909090

DATA "ABAAAABCAAABAAAABBAB"
DATA "BBABABBAABBBBAABABAA"
DATA "AAABABABAABABABACABB"
DATA "BAAAAABAAAAABBACAAAA"
DATA "AABBBCAAABAABACBBBBA"
DATA "BABBAAAABABBAAAABABA"
DATA "AABABAAABAAAABBBAAAB"
DATA "BBBABBAAAABBABABABBA"
DATA "AABAAABABBABABABABBA"
DATA "BBBBBBABBBABBABBAAAA"
DATA "AABAABABAABABBAAABBB"
DATA "BBAABABABABAAAAAABAA"
DATA "AAABABBABAABABBBAAAA"
DATA "CBAAAAAABBABBAAAAABA"
DATA "ABBBABBAABBAABABABBA"
DATA "BBAABBAABBABBBAABCBB"
DATA "BBBBAABBAABABAAABABA"
DATA "ABBAABABBABABBBBABBA"
DATA "BBABBAABABBBABAABAAB"
DATA "ABBAACCAAABABBBCAABA"
DATA "ABAAAABEEDEEEAAABBAB"
DATA "BBABEDEDEEEEUEEEABAA"
DATA "AAAEEEEEEDEEEEEEEABB"
DATA "BAEDEEDDEDDEEDEEEEAA"
DATA "AEEEEEEDEEDEEUEEEEEA"
DATA "BEEUEDEEEEUEDDDDEEDA"
DATA "DEEEEUEEDUEUUEUEDEEB"
DATA "EEEDDEEEEDEEUEEDDEEE"
DATA "EEEUEEEEEEEDEEEUEEDD"
DATA "EEEUEEEEUEDEEEEUEEDE"
DATA "DEDEDUDUDEEEEEEEDDEE"
DATA "EEDUDEDEEEEDDDEDEEEE"
DATA "EDUEUEDDEUEDEDEEDEEE"
DATA "CDEEEEEEDEEEEDDDEEEA"
DATA "AEEDEEEUUUEDEUDEEDEA"
DATA "BEEEEEEEEEUEEEEEEDEB"
DATA "BBEDEEEEEEDEEDDEDEBA"
DATA "ABBDEEEEDDDDEEEEEBBA"
DATA "BBABEEDEEEDEEEEEBAAB"
DATA "ABBAACCEEEEEEBBCAABA"
DATA "ABBAAAACAABBACAAABBB"
DATA "ABAAAABCAAABAAAABBAB"
DATA "BBABAaabbbBBBAABABAA"
DATA "AAABDabbbbbABABACABB"
DATA "BAADDDaabbbbBBACAAAA"
DATA "AADDDaaaabbbbACBBBBA"
DATA "BAEEEDaabbbbbAAABABA"
DATA "AEEEEEDDaabbbbBBAAAd"
DATA "BEEEEEDDDabbbbcccddd"
DATA "ABBEEEEDaDabbbccddef"
DATA "BBEEEEEEDaaaabdddeeh"
DATA "ABBBEBEEDDaaaaddffhh"
DATA "BBBBBEEEEDDaaaeefhhh"
DATA "ABBEBEEEEEDaaaBdfAAd"
DATA "CBBBBEEEEEDDaAdfAABA"
DATA "ABBBBBBEEEDEDBABABBA"
DATA "BBABBBEEEEDEBBAABCBB"
DATA "BBBBBBBEEEEABAAABABA"
DATA "ABBAABBBBEBABBBBABBA"
DATA "BBABBAABAABBABAABAAB"
DATA "ABBAACCAAABABBBCAABA"
DATA "ABBAAAACAABBAAAABBBB"
DATA "BBNN_N__ABABBAABBBAB"
DATA "AANVVN__AABBBABBCBAB"
DATA "BNNVVVN_AAABBBACAgBA"
DATA "ANVVVVNNABBBBBCBABBA"
DATA "BNVVNNNNBABBABAABgBA"
DATA "ANVVVVVNBBABABBBBgBB"
DATA "BNVVNVVNAABNNNNNNggA"
DATA "BNVV]NVNNNNNVVVVVggA"
DATA "BNV]]]VNVVV]NNNNNgAA"
DATA "ANV]NN]NNNNNNBBANgBB"
DATA "BNW]]N]NBBBBABAAgBAA"
DATA "ANN]]]]NBAABBBBAgAAA"
DATA "BBN]]N]NBBABBBAAAABA"
DATA "ABN]]]]NBBBABBBgABBA"
DATA "BBN]W]WNBBABBBBBBCBB"
DATA "BBNW]]WNABBBBABABABA"
DATA "ABBNWWNBBBABBBBBABBA"
DATA "BBBANWNBAAABABAABAAB"
DATA "ABBAANCAABBACABCAABA"
DATA "LMMMMMM\MMMMMMMMMMMM"
DATA "LMMMMMM\MMMMMMMMMMMM"
DATA "LMMMMMM\MMMMMMM\MMMM"
DATA "LMMMMMM\MMMMMMMMMMMM"
DATA "LMMMMMM\MMMMMMM\MMMM"
DATA "LMMMMMM\MMMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMMMMMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMM\MMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "LMMMMMMM\MMMMMM\MMMM"
DATA "LMM\MMMM\MMMMMM\MMMM"
DATA "LMMMMMMMMMMMMMM\MMMM"
DATA "BBABABBAABBBBAABABAA"
DATA "AOHHABABAABABABAFHBB"
DATA "FHHGIEBAAAAABBAOFIGA"
DATA "FHGIXEAAAOFGHACFHIXE"
DATA "GIPPQEAAFFHGGPAHGPXE"
DATA "APXQEAAAFHHIPXEIPXQE"
DATA "BBEEBBAAHGIIPXEBQQEA"
DATA "AABAAABAGGIXXQEBEEBA"
DATA "BBBBBBABBPXQQEBBAAAA"
DATA "HGEAABABAABEEBAAABBB"
DATA "HPEABABABABAAAAAABAA"
DATA "IQEBOOFHHAABABBBAAAA"
DATA "CEAOOFFGGPABBAOFHGBA"
DATA "ABBFHGHIIIEAAOFHGGIA"
DATA "BBAFHGGPPXEBBOHHIPXB"
DATA "BBBHGGIIXXEABHHGPPQA"
DATA "ABBHIIPPQEBABGIPPXQE"
DATA "BBABIPXQQEBBAEIXXQEE"
DATA "ABBAAEEEEABABBEEEEEA"
DATA "ABAAAABCAAABAAJJJBAB"
DATA "BBABABBAABBBBJJJJJAA"
DATA "AAABRRRBAABABJJYJJEE"
DATA "BAARRRRRAAEABJJJJJAE"
DATA "AABRRYRREEAEEAJJJBBA"
DATA "BABRRRRRBEBBAAAABABA"
DATA "AABARRRABAAARRRBAAAB"
DATA "BBBABBAAAABRRRRRABEA"
DATA "AJJJAABABBARRYRREEBE"
DATA "JJJJJBAEBBARRRRRAEAA"
DATA "JJYJJEEBEEBARRRAABBB"
DATA "JJJJJAEABABAAAAAABAA"
DATA "AJJJABBABRRRABBBAAAA"
DATA "CBAAAAAARRRRRAAEAABA"
DATA "ABBBABBARRYRREEBEEBA"
DATA "BBRRRBAARRRRRBEABCBB"
DATA "BRRRRRBBARRRBAAABABA"
DATA "ARRYRREEAEEABBBBABBA"
DATA "BRRRRRAEAABBABAABAAB"
DATA "ABRRRCCAAABABBBCAABA"
DATA "ABAAAABCAAABAAAABBAB"
DATA "BBABAFDAABBBBAABABAA"
DATA "AAABAFDBAABABABAIABB"
DATA "BAAAFGDDADAABBECGBAA"
DATA "AABFGGDDDDAABACBBBBA"
DATA "BAGGGIDDDABBAAAABBBB"
DATA "AABIIIDDBAAAABBBBBDB"
DATA "BBBABIDAAABBABFDBDDB"
DATA "AABAAIDABBABABFDDDDA"
DATA "BBBBBIDBBBABBAFDDDAA"
DATA "AABAABABAFFFFFGDDDBB"
DATA "BBAABABABAIGGGIDDBAA"
DATA "AAABGBBABAAIGIIDDAAA"
DATA "CBAG^AAABBABIIIDDABA"
DATA "ABB^BBBAABBAABIDDBBA"
DATA "BBAABBAA^BABBBIDDCBB"
DATA "BBBEAAB^HBBABAIDBABA"
DATA "ABBABBABBABABBIDABBA"
DATA "BBABBAABAEBBABAABAAB"
DATA "ABBAACCABABABBBCAABA"
DATA "ABAAAABCAAABAAAABBAB"
DATA "BBABAaabbbBBBAABABAA"
DATA "AAABDabbbbbABABACABB"
DATA "BAADDD`abbbbBBACAAAA"
DATA "AADD`aaaa`bbbACBBBBA"
DATA "BAEEEDaabbbbbAAABABA"
DATA "AEEEZE`DaabbbbBBAAAd"
DATA "BEZEEEDD`abbbbcccddd"
DATA "ABBEEZEDaDab`bccddef"
DATA "BBEEEEEEDaaaabdddeeh"
DATA "ABBBEBEEDDaaaaddffhh"
DATA "BB[BBZEEED`aaaeefhhh"
DATA "ABBEBEE[EEDaaaBdfAAd"
DATA "CBBBBEEEEEDDaAdfAABA"
DATA "ABBB[BBEEEDEDBABABBA"
DATA "BBABBB[EZEDEBBAABCBB"
DATA "BBBBBBBEEEEABAAABABA"
DATA "ABBAA[BBBEBABBBBABBA"
DATA "BBABBAABAABBABAABAAB"
DATA "ABBAACCAAABABBBCAABA"
DATA "KKKKKKKKKKKKKKKKKKKU"
DATA "KGGKGGGHFKGGKGGKGGGU"
DATA "KGGKGGGGHKGGKGGKGGUU"
DATA "KGGGGGGGGKGFUUGGGUUU"
DATA "KGGGGGGGGGGUUUBGBBBU"
DATA "KGGGHGGGGKUUKGBBBGGB"
DATA "KGGGGGKGGKGGGGGKGGGB"
DATA "KGGGGGKGGKGGKGGKGGGB"
DATA "KGFGGGKGGGGGKGGUGGGB"
DATA "KGGKGGGGFGGGKGGKUGBB"
DATA "KGFGGGGGGGGGKGGKUUBB"
DATA "KGHFGGBGGHGGKGGKGGGB"
DATA "KGGGHGKGGGGGBGGGGGGB"
DATA "KGGGGGKGGKGGBBGKGGGU"
DATA "KGGKGGKGGKGGUBBBBGGU"
DATA "KGGKGGKGGGGUUBBKBGGU"
DATA "KGGGGFKGFKGUKGGKBBBB"
DATA "KGGGGGKGGGGGKGGKGBBB"
DATA "KGGGGGKGGGFGKGG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MMMMMMMMMMMLTSEBA"
DATA "LLLLLLLLLLLLLLTSEBAB"
DATA "EEEEEEEEEEEEEEEEAABA"


And the demo. I have not implemented the sprite to sprite collision although the collision routine is there.

Code: Select all

'The the images used are those by Richard Eric Lope's pixel by pixel scrolling engine.
'Although in this example the tile images have been reduced to 16 and renumbered.
'Tiles 0 and 1 are considered walkable.
'This engine was ported to FreeBASIC.
'http://lachie.phatcode.net/fbscroll.php
'

screenres 640,480,32

type SPRITE
    as integer x
    as integer y
    as integer dx
    as integer dy
    as integer w
    as integer h
    as integer frame     'current frame
    as integer direction 'current direction
end type

dim shared as SPRITE agent(3)  'four sprites

function testCollision(s1 as SPRITE,s2 as SPRITE) as boolean
    return (s1.y+s1.h) > (s2.y) and (s1.y) < (s2.y+s2.h) and (s1.x) < (s2.x+s2.w) and (s1.x + s1.w) > (s2.x)
end function

const TILEW = 20
const TILEH = 20
dim shared as integer TMAPW     'TILE MAP
dim shared as integer TMAPH
dim shared as integer TileID        'tile number in tile block
dim shared as integer MAPW          'size of map in pixels
dim shared as integer MAPH
dim shared as integer WINW   = 320  'width of viewport window
dim shared as integer WINH   = 240  'height of viewport window
dim shared as integer WINX   = 0    'top/left position of viewport in background bitmap
dim shared as integer WINY   = 0    'changed by scrolling
dim shared as integer POSX   = 160  'top/left position of viewport on display
dim shared as integer POSY   = 120

read TMAPW,tMAPH
dim shared as integer TMAP(TMAPW,TMAPH)
for j as integer = 0 to TMAPH-1
    for i as integer = 0 to TMAPW-1
        read TMAP(i,j)
    next i
next j

MAPW  = TMAPW*TILEW   'size of map in pixels
MAPH  = TMAPH*TILEH

dim shared as any ptr spriteBlock2
spriteBlock2 = imagecreate(40,80)
bload "spriteBlock2.bmp",spriteBlock2

dim shared as any ptr spriteBlock3
spriteBlock3 = imagecreate(40,80)
bload "spriteBlock3.bmp",spriteBlock3

dim shared as any ptr tileBlock2
tileBlock2 = imagecreate(320,40)
bload "tileBlock2.bmp",tileBlock2

dim shared as any ptr display       'working bitmap to draw display
display = imagecreate(MAPW,MAPH)    'twice size of window display screen

dim shared as any ptr background    'background created from tile map
background = imagecreate(MAPW,MAPH) '


agent(0).x = 3*TILEW  'measured in pixels not tiles
agent(0).y = 3*TILEH
agent(0).w = TILEW
agent(0).h = TILEH

for i as integer = 1 to 3
    agent(i).x = i*40+40
    agent(i).y = 32*TILEH
    agent(i).w = TILEW
    agent(i).h = TILEH
    agent(i).dx = int(rnd(1)*3)-1
    agent(i).dy = int(rnd(1)*3)-1
    while (agent(i).dx<>0 or agent(i).dy=0) and (agent(i).dx=0 or agent(i).dy<>0)
        agent(i).dx = int(rnd(1)*3)-1
        agent(i).dy = int(rnd(1)*3)-1 
    wend
next i



sub update()
    dim as integer x,y
    screenlock
    cls
    for j as integer = 0 to TMAPH-1
        for i as integer = 0 to TMAPW-1
            tileID = TMAP(i,j)
            y = int(tileID/16)
            x = tileID - (y*16)
            put display,(i*TILEW,j*TILEH),tileBlock2,(x*TILEW,y*TILEH)-(x*TILEW+TILEW-1,y*TILEH+TILEH-1),trans
        next i
    next j
   
    'put display,(agent(0).x,agent(0).y),agent(0).img,trans
   
   
    put display,(agent(0).x,agent(0).y),spriteBlock2,(agent(0).frame*20,agent(0).direction*20)-(agent(0).frame*20+19,agent(0).direction*20+19),trans
    for i as integer = 1 to 3
        put display,(agent(i).x,agent(i).y),spriteBlock3,(agent(i).frame*20,agent(i).direction*20)-(agent(i).frame*20+19,agent(i).direction*20+19),trans       
    next i
   
    'copy window on display area to screen
    put (POSX,POSY),display,(WINX,WINY)-(WINX+WINW-1,WINY+WINH-1),trans
   
    print:print " USE CURSOR KEYS TO SCROLL background"
    print:print " WINX = ";WINX;"  WINY = ";WINY
    print:print " agent(0).x =";agent(0).x;"  agent(0).y =";agent(0).y
    screenunlock
end sub

function moveAgent(ag as SPRITE) as integer
   
        dim as integer hit
        dim as integer TILEX,TILEY

       
        hit = 0
        ag.x = ag.x + ag.dx
        ag.y = ag.y + ag.dy
       
        'out of bounds
        if ag.x < 0 or ag.x > MAPW-ag.w or ag.y < 0 or ag.y > MAPH-ag.h then hit = 1

        'test overlap sprite with a tile
        TILEX = int((ag.x+4)/TILEW)
        TILEY = int((ag.y+15)/TILEH)
        if TMAP(TILEX,TILEY)>1 then hit = 1

        TILEX = int((ag.x+TILEW-5)/TILEW)
        TILEY = int((ag.y+15)/TILEH)
        if TMAP(TILEX,TILEY)>1 then hit = 1

        TILEX = int((ag.x+4)/TILEW)
        TILEY = int((ag.y+TILEH-1)/TILEH)
        if TMAP(TILEX,TILEY)>1 then hit = 1

        TILEX = int((ag.x+TILEW-5)/TILEW)
        TILEY = int((ag.y+TILEH-1)/TILEH)
        if TMAP(TILEX,TILEY)>1 then hit = 1

        if hit = 1 then
            ag.x = ag.x - ag.dx 'undo move
            ag.y = ag.y - ag.dy
        end if

        ag.frame = ag.frame + 1
        if ag.frame = 2 then ag.frame = 0
       
        return hit
       
end function


dim as integer hit
dim as single time1 = timer
dim as single time2 = timer

do
   
    if timer > time1 + 0.01 then
        time1 = timer  'reset time

        'move agent with cursor keys
        agent(0).dx = 0
        agent(0).dy = 0
        if multikey(&H4D) then
            agent(0).dx =  1
            agent(0).direction = 1
        end if
       
        if multikey(&H4B) then
            agent(0).dx = -1
            agent(0).direction = 2
        end if
       
        if multikey(&H50) then
            agent(0).dy =  1
            agent(0).direction = 0
        end if
       
        if multikey(&H48) then
            agent(0).dy = -1
            agent(0).direction = 3
        end if
       
        hit = moveAgent(agent(0))  'no need to do anything with hit
       
        'now move other agents not under cursor key control
        'if they hit an object then change their direction
        if timer > time2 + 0.01 then
            time2 = timer
            for i as integer = 1 to 3
                hit = moveAgent(agent(i))
                if hit = 1 or int(rnd(1)*60)=0 then  'change direction if hit or random decision
                    agent(i).dx = int(rnd(1)*3)-1
                    agent(i).dy = int(rnd(1)*3)-1
                    'do not allow diagonal movements or no movement at all
                    while (agent(i).dx<>0 or agent(i).dy=0) and (agent(i).dx=0 or agent(i).dy<>0)
                        agent(i).dx = int(rnd(1)*3)-1
                        agent(i).dy = int(rnd(1)*3)-1 
                    wend
                end if
            next i
        end if
       
        'adjust position of window on background
        WINX = agent(0).x - (WINW\2)
        if WINX < 0 then WINX = 0
        if WINX >= MAPW-WINW then WINX = MAPW-WINW
        WINY = agent(0).y - (WINH\2)
        if WINY < 0 then WINY = 0
        if WINY >= MAPH-WINH then WINY = MAPH-WINH
       
        update
    end if
   
    sleep 2

loop until multikey(&H01)

sleep
data 36,39  'size of TMAP
data 06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,10,10,10,11,10,10,10,10,11,06,06,06,06,06,06
data 06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,11,10,10,10,11,11,11,10,11,06,06,06,06,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,11,09,10,11,10,11,09,09,10,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,01,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,12,13,00,00,06,06
data 06,00,00,00,01,01,01,00,00,00,00,00,00,00,01,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,14,15,00,00,06,06
data 06,00,00,00,01,01,01,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,01,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,12,04,13,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,04,04,04,00,02,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,07,00,00,00,00,01,02,00,00,00,00,00,00,00,00,00,04,04,04,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,07,00,00,00,00,00,00,00,00,00,00,00,00,00,00,14,04,15,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,07,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,07,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,07,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,02,00,00,06,06
data 06,00,00,00,07,00,00,00,00,00,00,00,00,02,08,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,02,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,01,00,00,08,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,07,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,03,00,00,00,08,00,02,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,06,06
data 06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06
data 06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06,06
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: Screen Scrolling

Postby leopardpm » Aug 01, 2016 0:31

BasicCoder2 wrote:@leopardpm, I thought you had vanished from the scene?

I got a bit overloaded and stressed at work... just needed some 'hermit' time is all. I am still thinking about our SimWorld thing, but right now I am obsessed with pathfinding (due to our exploration of A-STAR)... just posted today: http://www.freebasic.net/forum/viewtopic.php?f=7&t=24902&p=222691#p222691

also, you might REALLY like this lecture I found: http://www.gdcvault.com/play/1021848/Building-a-Better-Centaur-AI

good to talk to you again!
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: Screen Scrolling

Postby leopardpm » Aug 01, 2016 0:42

LOL! I forgot how much I missed all your little 'generation' programs to get around hosting image files (I am now using PostImage.org, works fine, no need to login either) - You do it so much that I am VERY surprised you havent yet streamlined the entire process to doing it automatically for you, maybe you have..


I LOVE YOUR DEMO! works smoothly and even has some other agents wandering around in there! LOL! Can't wait to look into your code

PS: luv the tiny graphics too!!! Are these from another project you are working on... perhaps to do with AI or simulation?!
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: Screen Scrolling

Postby leopardpm » Aug 01, 2016 0:49

argh! I wanted to stress test it with lots of little agents, but you have it set up for each agent having its own sprite... drats....
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: Screen Scrolling

Postby leopardpm » Aug 01, 2016 0:56

leopardpm wrote:argh! I wanted to stress test it with lots of little agents, but you have it set up for each agent having its own sprite... drats....

I was wrong, I had just missed another place where you referred to having 3 agents only... now is linked to 'numOfagents' variable so can easily test different number of agents... doesn't feel so lonely now...
BasicCoder2
Posts: 3397
Joined: Jan 01, 2009 7:03

Re: Screen Scrolling

Postby BasicCoder2 » Aug 01, 2016 1:10

@leopardpm,
The graphics are not mine they are those used by Richard Eric Lope,
http://lachie.phatcode.net/fbscroll.php

The generation of code to turn images into data statements is semi automatic.

At the moment I am also learning to use JavaScript to enhance a web page as an interest in common with another family member.
https://jsfiddle.net/
.
Last edited by BasicCoder2 on Aug 01, 2016 16:10, edited 1 time in total.
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: Screen Scrolling

Postby leopardpm » Aug 01, 2016 1:21

excellent! I know next to nothing about JavaScript, except it seems related to C in ways... good luck with your project, I hope to see you around some more soon - will post some stuff on Pathfinding I have been researching.... http://www.freebasic.net/forum/viewtopic.php?f=7&t=24902&p=222706#p222706
Clay Dragon
Posts: 15
Joined: Jul 30, 2016 2:05
Location: Canada

Re: Screen Scrolling

Postby Clay Dragon » Aug 01, 2016 6:06

BasicCoder2,

Your coding skills are amazing! I really couldn't ask for anything better!

I'll give you credit for that if you want to!

Thank you so much again!
BasicCoder2
Posts: 3397
Joined: Jan 01, 2009 7:03

Re: Screen Scrolling

Postby BasicCoder2 » Aug 02, 2016 0:17

@Clay Dragon,
All programmers use the work of others that came before them. There is nothing novel or new in any of my code. The forum is for sharing code or showing off code. If my code enabled you to understand how to scroll images that is great although I didn't also explain it with words. Also I didn't implement sprite to sprite collisions. All the time we are really dealing with rectangles. The code below is an example of using the sprite to sprite collision function. Unlike the sprite to tile collisions in the game demo where the sprite collision rectangle is smaller than the sprite rectangle itself I noticed the sprites in the demo did not overlap when one sprite walked in front of another sprite. Here the sprites collision rectangles are the same size as the sprite rectangle.

Code: Select all

const SCRW = 640
const SCRH = 480

screenres SCRW,SCRH,32
color rgb(0,0,0),rgb(255,255,255):cls   'black ink, white paper

type RECTANGLE
    as integer x
    as integer y
    as integer w
    as integer h
    as integer dx
    as integer dy
    as ulong c
end type

function testCollision(s1 as RECTANGLE,s2 as RECTANGLE) as boolean
    return (s1.y+s1.h) > (s2.y) and (s1.y) < (s2.y+s2.h) and (s1.x) < (s2.x+s2.w) and (s1.x + s1.w) > (s2.x)
end function

dim shared as RECTANGLE rect(1 to 4)

sub drawRectangles()
    screenlock
    cls
    for i as integer = 1 to 4
        line (rect(i).x,rect(i).y)-(rect(i).x+rect(i).w,rect(i).y+rect(i).h),rect(i).c,bf
        draw string (rect(i).x+4,rect(i).y+4),str(i)
    next i
    screenunlock
end sub

sub moveRectangles()
    dim as integer hit
    hit = 0
    for i as integer = 1 to 4                'for each rectangle
       
        rect(i).x = rect(i).x + rect(i).dx   'make move
        rect(i).y = rect(i).y + rect(i).dy
       
        'test boundary
        if rect(i).x < 0 or rect(i).x >= SCRW - rect(i).w or rect(i).y < 0 or rect(i).y >= SCRH - rect(i).h then
            rect(i).x = rect(i).x - rect(i).dx
            rect(i).y = rect(i).y - rect(i).dy
            rect(i).dx = int(rnd(1)*3)-1
            rect(i).dy = int(rnd(1)*3)-1
            while rect(i).dx=0 and rect(i).dy=0
                rect(i).dx = int(rnd(1)*3)-1
                rect(i).dy = int(rnd(1)*3)-1 
            wend
        end if
       
        'test for collision with another rectangle
        for j as integer = 1 to 4   'for each other rectangle
            if i<>j then            'which isn't itself
                if  testCollision(rect(i),rect(j)) = TRUE then   'has collided then
                    rect(i).x = rect(i).x - rect(i).dx
                    rect(i).y = rect(i).y - rect(i).dy
                    rect(i).dx = int(rnd(1)*3)-1
                    rect(i).dy = int(rnd(1)*3)-1
                    while rect(i).dx=0 and rect(i).dy=0
                        rect(i).dx = int(rnd(1)*3)-1
                        rect(i).dy = int(rnd(1)*3)-1 
                    wend 
                end if
            end if
        next j
       
    next i
end sub
   
rect(1).x = 95
rect(1).y = 58
rect(1).w = 55
rect(1).h = 123
rect(1).c = rgb(200,50,50)

rect(2).x = 323
rect(2).y = 119
rect(2).w = 130
rect(2).h = 86
rect(2).c = rgb(50,200,50)

rect(3).x = 169
rect(3).y = 274
rect(3).w = 129
rect(3).h = 124
rect(3).c = rgb(50,50,200)

rect(4).x = 493
rect(4).y = 314
rect(4).w = 73
rect(4).h = 33
rect(4).c = rgb(50,200,100)

for i as integer = 1 to 4
    rect(i).dx = int(rnd(1)*3)-1
    rect(i).dy = int(rnd(1)*3)-1
    while rect(i).dx=0 and rect(i).dy=0
        rect(i).dx = int(rnd(1)*3)-1
        rect(i).dy = int(rnd(1)*3)-1 
    wend
next i

dim as single time1
time1 = timer
drawRectangles()

do
    if timer > time1 + 0.01 then
        time1 = timer  'reset time counter
        moveRectangles()
        drawRectangles()
    end if
   
    sleep 2
loop until multikey(&H01)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 42 guests