QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Prithak on April 04, 2020, 04:30:12 am

Title: Blocking stuff D:
Post by: Prithak on April 04, 2020, 04:30:12 am
Hey, Long time no see. Anyways,

I was developing a game where I run on a problem IMMEDIATELY xD. And I can't quite figure out what that problem is. Would you look at the code and find it out?
Code: [Select]
screen _newimage(1280,720,32)

_fullscreen

'Declaring Variables
type vec
    x as integer
    y as integer
    speed as integer
end type

dim shared player as vec
dim shared map as string

dim shared oldx, oldy as integer

player.speed = 3
map = "map1.txt"

player.x = _width / 2
player.y = _height / 2

'Main Loop
do
    cls

    makeplayer
    playerMovement
    'Test
    'makeObstacle 100,100,200,200,255,200,100

    makeMap

    'For Experiments: The co-ordinates
    _printstring (0,0), str$(player.x) + str$(player.y)

    _display
loop until _keyhit = 27
system
'Subs and Functions

sub makeplayer
    circle (player.x, player.y), 10, _rgb32(100,200,255)
    paint (player.x, player.y), _rgb32(100,200,255), _rgb32(100,200,255)
end sub

sub playerMovement
    if _keydown(18432) then player.y = player.y - player.speed
    if _keydown(20480) then player.y = player.y + player.speed
    if _keydown(19200) then player.x = player.x - player.speed
    if _keydown(19712) then player.x = player.x + player.speed
end sub

sub makeObstacle(x1,y1,x2,y2,r,g,b)

    line (x1,y1)-(x2,y2),_rgb32(r,g,b), BF

    if player.x + 10 >= x1 and player.x - 10 <= x2 and player.y + 10 >= y1 and player.y - 10 <= y2 then
        player.x = oldx
        player.y = oldy
    else
        oldx = player.x
        oldy = player.y
    end if

end sub

sub makeMap
    open map for input as #1
    while not eof(1)
        input #1, cm$,x1,y1,x2,y2,r,g,b
        if cm$ = "obs" then
            makeObstacle x1,y1,x2,y2,r,g,b
        end if
    wend
    close #1
end sub

Here's the file called "map1.txt"
Code: [Select]
obs, 0,0,700,50,242,242,242
obs, 800,50,1280,100,242,242,242

Here, the 1st obstacle is blocking the character perfectly, but the second one isn't. Why???

-Prithak
Title: Re: Blocking stuff D:
Post by: Unseen Machine on April 04, 2020, 05:56:44 am
Hi,

Its cause your only storing one set of data for the obstacles. You need to use an array to store more than one.

Id suggest you define a TYPE for the obstacles and then use an array of that type to handle them

i.e

type Obstacle
X1 AS integer
X2 as integer
Y1 as integer
Y2 as integer
Color as _unsigned long
End type

Dim Obstacle(9) as obstacle '// Creates an array of ten Obstacle type variables.

Have fun and happy coding!

Unseen
Title: Re: Blocking stuff D:
Post by: EricE on April 04, 2020, 06:00:53 am
Just to get the code to compile I had to make this change to explicitly specify that the map1 file is in the same directory as the exe:

Code: QB64: [Select]
  1. map = ".\map1.txt"
Title: Re: Blocking stuff D:
Post by: Prithak on April 04, 2020, 06:09:25 am
Hi,

Its cause your only storing one set of data for the obstacles. You need to use an array to store more than one.

Id suggest you define a TYPE for the obstacles and then use an array of that type to handle them

i.e

type Obstacle
X1 AS integer
X2 as integer
Y1 as integer
Y2 as integer
Color as _unsigned long
End type

Dim Obstacle(9) as obstacle '// Creates an array of ten Obstacle type variables.

Have fun and happy coding!

Unseen
Ya. I figured. But, Using Arrays would SIGNIFICANTLY slow down the computer. So, I figured this plan out. If this works, I won't have to worry about performance. Soo... If there is a way out, PLEASE TEACH MEH!!!

-Prithak
Title: Re: Blocking stuff D:
Post by: Prithak on April 04, 2020, 06:15:59 am
Just to get the code to compile I had to make this change to explicitly specify that the map1 file is in the same directory as the exe:

Code: QB64: [Select]
  1. map = ".\map1.txt"
Uhh... No you don't need to do that...
Title: Re: Blocking stuff D:
Post by: EricE on April 04, 2020, 06:25:26 am
Quote
Uhh... No you don't need to do that...

You are right!
The error does not pop up now.
Something else was causing the problem.
Probably Operator Error!
Title: Re: Blocking stuff D:
Post by: Unseen Machine on April 04, 2020, 07:02:34 am
Quote
But, Using Arrays would SIGNIFICANTLY slow down the computer

Are you sure? I easily get 25-35 fps with my current programs and they are far more CPU/Memory heavy than what you've posted. I cant see that you'd notice any difference in speed if you used arrays aposed to individual variables to store each value.

Good luck,

Unseen
Title: Re: Blocking stuff D:
Post by: Prithak on April 04, 2020, 07:37:23 am
Are you sure? I easily get 25-35 fps with my current programs and they are far more CPU/Memory heavy than what you've posted. I cant see that you'd notice any difference in speed if you used arrays aposed to individual variables to store each value.

Good luck,

Unseen
Ok. Would you mind sending me an example. Cuz, when I tried to do it. It just didn't work.

Sincerely,
Prithak
Title: Re: Blocking stuff D:
Post by: 40wattstudio on April 04, 2020, 07:38:26 am
But, Using Arrays would SIGNIFICANTLY slow down the computer.

Don't be afraid to use arrays, they allow for a lot of interesting new possibilities!
Your game will still be playable, even if you are using arrays to load graphics. For example, in my game Scrapship there is an array that cycles through 120 planet images to create a rotating planet effect. Each .png file is about 300kb.
Again, don't be afraid to use arrays. :)
Title: Re: Blocking stuff D:
Post by: Prithak on April 04, 2020, 07:46:51 am
I tried to Re-Do some stuff and Look what I've Found:
Code: [Select]
screen _newimage(1280,720,32)

type vec1
    x as integer
    y as integer
end type

type vec2
    x1 as integer
    y1 as integer
    x2 as integer
    y2 as integer
    r as integer
    g as integer
    b as integer
    btype as string
end type

dim shared player as vec1
dim shared speed as integer
dim shared map as string
dim shared obstacle(9) as vec2

dim shared i as integer

map = "map1.txt"

openMap

do
    cls
    j = j + 1



    if j > i then j = 0
    _display
loop

sub openMap
    open map for input as #1
    while not eof(1)
        input #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).x2, obstacle(i).y1, obstacle(i). y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
        i = i + 1
    wend
    close #1
end sub
"Input pas end of file!"
WHYYYY??

-Prithak
Title: Re: Blocking stuff D:
Post by: 40wattstudio on April 04, 2020, 08:16:03 am
"Input pas end of file!"
WHYYYY??

I wasn't able to duplicate your fault. When dealing with arrays you want to make sure they are within bounds and remember that they start at 0. You could also insert some print statements to help debug and see how far your program is getting along before it reaches the error. That's what I usually do anyways.
Title: Re: Blocking stuff D:
Post by: Prithak on April 04, 2020, 08:44:29 am
I FINALLY did things with Array. But, the same problem has arrived to my doorstep D:
Code: [Select]
screen _newimage(1280,720,32)

type vec1
    x as integer
    y as integer
    speed as integer
end type

type vec2
    x1 as integer
    y1 as integer
    x2 as integer
    y2 as integer
    r as integer
    g as integer
    b as integer
    btype as string
end type

dim shared player as vec1
dim shared speed as integer
dim shared map as string
dim shared obstacle(9) as vec2
dim shared oldx, oldy as integer

player.speed = 5

dim shared i as integer

map = "map1.txt"

openMap
j=1
do
    cls

    makeplayer
    playerMovement

    for j = 1 to i
        line (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2),_rgb32(obstacle(j).r,obstacle(j).g,obstacle(j).b),BF
        block obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2
    next j

    _display
    _limit 60
loop

sub openMap
    open map for input as #1
    while not eof(1)
        i = i + 1
        input #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i). y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
    wend
    close #1
end sub

sub makeplayer
    circle (player.x, player.y), 10, _rgb32(100,200,255)
    paint (player.x, player.y), _rgb32(100,200,255), _rgb32(100,200,255)
end sub

sub playerMovement
    if _keydown(18432) then player.y = player.y - player.speed
    if _keydown(20480) then player.y = player.y + player.speed
    if _keydown(19200) then player.x = player.x - player.speed
    if _keydown(19712) then player.x = player.x + player.speed
end sub

sub block(x1,y1,x2,y2)

    if player.x + 10 >= x1 and player.x - 10 <= x2 and player.y + 10 >= y1 and player.y - 10 <= y2 then
        player.x = oldx
        player.y = oldy
    else
        oldx = player.x
        oldy = player.y
    end if

end sub

Title: Re: Blocking stuff D:
Post by: Unseen Machine on April 04, 2020, 10:08:05 am
From a quick glance, i cant see what might be wrong but i can see your using 1 as your first array but declaring from 0.

if 1 is the first one you want then

DIM MyVar(1 to 10) as Integer

Unseeen
Title: Re: Blocking stuff D:
Post by: TempodiBasic on April 04, 2020, 11:14:57 am
Hi Prithak

your last post code is very incomplete!

So I add something to become it clearer than now....

but the glitch is logic and not about typing....

This your issue let me remember another in a graphic game engine in which the output of grafic doesn't recognize the position of objects of the scene  in respect with the main character... in that engine issue the engine has not option to distinguish the position of the character towards the object that are able to be pass back.

In your collision engine you haven't thought what it happens to the OldY and OldX variable at the second cycle of loop...
here my code can show you what it happens
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1280, 720, 32)
  2. _SCREENMOVE 1, 1 ' better show alla the window
  3.  
  4. TYPE vec1
  5.     x AS INTEGER
  6.     y AS INTEGER
  7.     speed AS INTEGER
  8.  
  9. TYPE vec2
  10.     x1 AS INTEGER
  11.     y1 AS INTEGER
  12.     x2 AS INTEGER
  13.     y2 AS INTEGER
  14.     r AS INTEGER
  15.     g AS INTEGER
  16.     b AS INTEGER
  17.     btype AS STRING
  18.  
  19. DIM SHARED player AS vec1
  20. DIM SHARED obstacle(9) AS vec2
  21. DIM SHARED oldx, oldy AS INTEGER
  22.  
  23. player.speed = 5
  24.  
  25. player.x = 100 ' character need a point of start X and Y
  26. player.y = 200
  27.  
  28.  
  29.  
  30. map = "map1.txt"
  31.  
  32. openMap
  33. j = 1
  34.     CLS , _RGB32(0, 255, 61) ' it is better to give a color to background
  35.     makeplayer
  36.     playerMovement
  37.  
  38.     FOR j = 1 TO i 'the loop checks for obstacles
  39.  
  40.         LOCATE 9: PRINT j
  41.         ' here graphic output
  42.         LINE (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2), _RGB32(obstacle(j).r, obstacle(j).g, obstacle(j).b), BF
  43.         ' here it tests for collision between character and obstacles
  44.         block obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2
  45.  
  46.     NEXT j
  47.  
  48.     _DISPLAY
  49.     _LIMIT 20
  50. LOOP UNTIL _KEYHIT = 32 ' you can exit from game with spacebar
  51.  
  52. SUB openMap
  53.     OPEN map FOR INPUT AS #1
  54.     WHILE NOT EOF(1)
  55.         i = i + 1
  56.         INPUT #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i).y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
  57.     WEND
  58.     CLOSE #1
  59.     PRINT i
  60.  
  61. SUB makeplayer
  62.     CIRCLE (player.x, player.y), 10, _RGB32(100, 200, 255)
  63.     PAINT (player.x, player.y), _RGB32(100, 200, 255), _RGB32(100, 200, 255)
  64.  
  65. SUB playerMovement
  66.     IF _KEYDOWN(18432) THEN player.y = player.y - player.speed
  67.     IF _KEYDOWN(20480) THEN player.y = player.y + player.speed
  68.     IF _KEYDOWN(19200) THEN player.x = player.x - player.speed
  69.     IF _KEYDOWN(19712) THEN player.x = player.x + player.speed
  70.  
  71. SUB block (x1, y1, x2, y2)
  72.  
  73.     IF (player.x + 10 >= x1 AND player.x - 10 <= x2) AND (player.y + 10 >= y1 AND player.y - 10 <= y2) THEN
  74.         ' if player X is in the X range of obstacle AND player Y is in the Y range of obstacle
  75.         ' I restore the old X and Y to player
  76.         player.x = oldx
  77.         player.y = oldy
  78.  
  79.         LOCATE 10
  80.         PRINT "X="; player.x; " Y="; player.y; " x1 "; x1; " x2 "; x2; " y1 "; y1; " y2 "; y2
  81.         PRINT "Oldx = "; oldx; " OldY= "; oldy
  82.  
  83.     ELSE
  84.         ' if player is out of obstacle
  85.         ' I store actual X and Y of player into OLD X and Y
  86.         oldx = player.x
  87.         oldy = player.y
  88.     END IF
  89.  
  90.  

as you can see when you go through the second obstacle using a FOR loop that tests before first obstacle and then the following second obstacle so it changes the value of OldX and OldY and then when it tests the second obstacle it restore the OldX and OldY calculated towards the first obstacle.

So you can imagine how to solve this issue... a STATIC flag into the collision engine? Or and array of OldY and OldY dor each obstacle to use...or whatelse?
Title: Re: Blocking stuff D:
Post by: Prithak on April 05, 2020, 04:27:29 am
Hi Prithak

your last post code is very incomplete!

So I add something to become it clearer than now....

but the glitch is logic and not about typing....

This your issue let me remember another in a graphic game engine in which the output of grafic doesn't recognize the position of objects of the scene  in respect with the main character... in that engine issue the engine has not option to distinguish the position of the character towards the object that are able to be pass back.

In your collision engine you haven't thought what it happens to the OldY and OldX variable at the second cycle of loop...
here my code can show you what it happens
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1280, 720, 32)
  2. _SCREENMOVE 1, 1 ' better show alla the window
  3.  
  4. TYPE vec1
  5.     x AS INTEGER
  6.     y AS INTEGER
  7.     speed AS INTEGER
  8.  
  9. TYPE vec2
  10.     x1 AS INTEGER
  11.     y1 AS INTEGER
  12.     x2 AS INTEGER
  13.     y2 AS INTEGER
  14.     r AS INTEGER
  15.     g AS INTEGER
  16.     b AS INTEGER
  17.     btype AS STRING
  18.  
  19. DIM SHARED player AS vec1
  20. DIM SHARED obstacle(9) AS vec2
  21. DIM SHARED oldx, oldy AS INTEGER
  22.  
  23. player.speed = 5
  24.  
  25. player.x = 100 ' character need a point of start X and Y
  26. player.y = 200
  27.  
  28.  
  29.  
  30. map = "map1.txt"
  31.  
  32. openMap
  33. j = 1
  34.     CLS , _RGB32(0, 255, 61) ' it is better to give a color to background
  35.     makeplayer
  36.     playerMovement
  37.  
  38.     FOR j = 1 TO i 'the loop checks for obstacles
  39.  
  40.         LOCATE 9: PRINT j
  41.         ' here graphic output
  42.         LINE (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2), _RGB32(obstacle(j).r, obstacle(j).g, obstacle(j).b), BF
  43.         ' here it tests for collision between character and obstacles
  44.         block obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2
  45.  
  46.     NEXT j
  47.  
  48.     _DISPLAY
  49.     _LIMIT 20
  50. LOOP UNTIL _KEYHIT = 32 ' you can exit from game with spacebar
  51.  
  52. SUB openMap
  53.     OPEN map FOR INPUT AS #1
  54.     WHILE NOT EOF(1)
  55.         i = i + 1
  56.         INPUT #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i).y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
  57.     WEND
  58.     CLOSE #1
  59.     PRINT i
  60.  
  61. SUB makeplayer
  62.     CIRCLE (player.x, player.y), 10, _RGB32(100, 200, 255)
  63.     PAINT (player.x, player.y), _RGB32(100, 200, 255), _RGB32(100, 200, 255)
  64.  
  65. SUB playerMovement
  66.     IF _KEYDOWN(18432) THEN player.y = player.y - player.speed
  67.     IF _KEYDOWN(20480) THEN player.y = player.y + player.speed
  68.     IF _KEYDOWN(19200) THEN player.x = player.x - player.speed
  69.     IF _KEYDOWN(19712) THEN player.x = player.x + player.speed
  70.  
  71. SUB block (x1, y1, x2, y2)
  72.  
  73.     IF (player.x + 10 >= x1 AND player.x - 10 <= x2) AND (player.y + 10 >= y1 AND player.y - 10 <= y2) THEN
  74.         ' if player X is in the X range of obstacle AND player Y is in the Y range of obstacle
  75.         ' I restore the old X and Y to player
  76.         player.x = oldx
  77.         player.y = oldy
  78.  
  79.         LOCATE 10
  80.         PRINT "X="; player.x; " Y="; player.y; " x1 "; x1; " x2 "; x2; " y1 "; y1; " y2 "; y2
  81.         PRINT "Oldx = "; oldx; " OldY= "; oldy
  82.  
  83.     ELSE
  84.         ' if player is out of obstacle
  85.         ' I store actual X and Y of player into OLD X and Y
  86.         oldx = player.x
  87.         oldy = player.y
  88.     END IF
  89.  
  90.  

as you can see when you go through the second obstacle using a FOR loop that tests before first obstacle and then the following second obstacle so it changes the value of OldX and OldY and then when it tests the second obstacle it restore the OldX and OldY calculated towards the first obstacle.

So you can imagine how to solve this issue... a STATIC flag into the collision engine? Or and array of OldY and OldY dor each obstacle to use...or whatelse?

THANK YOU! YOU ARE A LIFE SAVER. Like seriously, why didn't I think about that. Now, I am using an array for Oldx and Oldy. So, the blocking mechanism is working PERFECTLY. I had to make some changes tho. Here is the code:
Code: [Select]
screen _newimage(1280,720,32)

type vec1
    x as integer
    y as integer
    speed as integer
end type

type vec2
    x1 as integer
    y1 as integer
    x2 as integer
    y2 as integer
    r as integer
    g as integer
    b as integer
    btype as string
    oldx as integer
    oldy as integer
    wl as string
end type


dim shared player as vec1
dim shared map as string
dim shared obstacle(9) as vec2
player.speed = 5

dim shared i as integer

map = "map1.txt"

player.x = _width / 2
player.y = _height / 2

openMap
j=1
do
    cls

    makeplayer
    playerMovement

    for j = 1 to i
        if obstacle(j).btype = "obs" then
            line (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2),_rgb32(obstacle(j).r,obstacle(j).g,obstacle(j).b),BF
            if player.x + 10 >= obstacle(j).x1 and player.x - 10 <= obstacle(j).x2 and player.y + 10 >= obstacle(j).y1 and player.y - 10 <= obstacle(j).y2 then
                player.x = obstacle(j).oldx
                player.y = obstacle(j).oldy
            else
                obstacle(j).oldx = player.x
                obstacle(j).oldy = player.y
            end if
        elseif obstacle(j).btype = "warp" then
            line (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2),_rgb32(obstacle(j).r,obstacle(j).g,obstacle(j).b),BF
            if collision%(obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2) then
                map = obstacle(i).wl
                openMap
            end if
        end if
    next j

    _display
    _limit 120
loop

sub openMap
    i=0
    open map for input as #1
    while not eof(1)
        i = i + 1
        input #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i). y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
        if obstacle(i).btype = "warp" then input #1, obstacle(i).wl
    wend
    close #1
end sub

sub makeplayer
    circle (player.x, player.y), 10, _rgb32(100,200,255)
    paint (player.x, player.y), _rgb32(100,200,255), _rgb32(100,200,255)
end sub

sub playerMovement
    if _keydown(18432) then player.y = player.y - player.speed
    if _keydown(20480) then player.y = player.y + player.speed
    if _keydown(19200) then player.x = player.x - player.speed
    if _keydown(19712) then player.x = player.x + player.speed
end sub

function collision%(x1,y1,x2,y2)
    if player.x >= x1 and player.x <= x2 and player.y >= y1 and player.y <= y2 then
        collision% = 1
    end if
end function

Map1.txt
Code: [Select]
obs,600,50,1280,100,100,200,255
obs,0,0,700,50,100,200,255
warp,500,500,550,550,255,200,100,map2.txt

Map2.txt
Code: [Select]
obs, 600,50,1280,100,242,242,242
obs, 500,0,700,50,242,242,242
warp, 100,100,150,150,255,200,100,map1.txt

-Prithak
Title: Re: Blocking stuff D:
Post by: TempodiBasic on April 05, 2020, 01:37:47 pm
Hi Prithak

you're welcome!
I can see that you go over very well choosing your  solution for the logical issue.
I'll wait news on this you project.

Good Luck
Title: Re: Blocking stuff D:
Post by: TempodiBasic on April 07, 2020, 05:17:45 am
Hi Prithak

looking at your new code I have got the error of Read past end of file...
it seems a glicth of EOF in fact if you see the debug output after reading the second line of data correctly EOF gives still 0 as output and not -1! so the loop tries to read another time and Runtime error!

here code with a raw tracing debug session
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1280, 720, 32)
  2.  
  3. TYPE vec1
  4.     x AS INTEGER
  5.     y AS INTEGER
  6.     speed AS INTEGER
  7.  
  8. TYPE vec2
  9.     x1 AS INTEGER
  10.     y1 AS INTEGER
  11.     x2 AS INTEGER
  12.     y2 AS INTEGER
  13.     r AS INTEGER
  14.     g AS INTEGER
  15.     b AS INTEGER
  16.     btype AS STRING
  17.     oldx AS INTEGER
  18.     oldy AS INTEGER
  19.     wl AS STRING
  20.  
  21.  
  22. DIM SHARED player AS vec1
  23. DIM SHARED obstacle(9) AS vec2
  24. player.speed = 5
  25.  
  26.  
  27. map = "map1.txt"
  28.  
  29. player.x = _WIDTH / 2
  30. player.y = _HEIGHT / 2
  31.  
  32. openMap
  33. j = 1
  34.     CLS
  35.  
  36.     makeplayer
  37.     playerMovement
  38.  
  39.     FOR j = 1 TO i
  40.         IF obstacle(j).btype = "obs" THEN
  41.             LINE (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2), _RGB32(obstacle(j).r, obstacle(j).g, obstacle(j).b), BF
  42.             IF player.x + 10 >= obstacle(j).x1 AND player.x - 10 <= obstacle(j).x2 AND player.y + 10 >= obstacle(j).y1 AND player.y - 10 <= obstacle(j).y2 THEN
  43.                 player.x = obstacle(j).oldx
  44.                 player.y = obstacle(j).oldy
  45.             ELSE
  46.                 obstacle(j).oldx = player.x
  47.                 obstacle(j).oldy = player.y
  48.             END IF
  49.         ELSEIF obstacle(j).btype = "warp" THEN
  50.             LINE (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2), _RGB32(obstacle(j).r, obstacle(j).g, obstacle(j).b), BF
  51.             IF collision%(obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2) THEN
  52.                 map = obstacle(j).wl ' here there was a mystyping i for j
  53.                 openMap
  54.             END IF
  55.         END IF
  56.     NEXT j
  57.  
  58.     _DISPLAY
  59.     _LIMIT 120
  60.  
  61. SUB openMap ' the program gets past end of file but reads obstacle(i).wl warps
  62.     i = 0
  63.     OPEN map FOR INPUT AS #1
  64.     LOCATE 10
  65.     WHILE NOT EOF(1)
  66.         i = i + 1
  67.         INPUT #1, obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i).y2, obstacle(i).r, obstacle(i).g, obstacle(i).b
  68.         IF obstacle(i).btype = "warp" THEN INPUT #1, obstacle(i).wl
  69.         ' just some debug prints...
  70.         PRINT "i = ", i
  71.         PRINT obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i).y2, obstacle(i).r, obstacle(i).g, obstacle(i).b, obstacle(i).wl
  72.         PRINT "EOF"; EOF(1); " press a key for the next run of cycle"
  73.         SLEEP ' just to trace into loop
  74.     WEND
  75.     CLOSE #1
  76.  
  77. SUB makeplayer
  78.     CIRCLE (player.x, player.y), 10, _RGB32(100, 200, 255)
  79.     PAINT (player.x, player.y), _RGB32(100, 200, 255), _RGB32(100, 200, 255)
  80.  
  81. SUB playerMovement
  82.     IF _KEYDOWN(18432) THEN player.y = player.y - player.speed
  83.     IF _KEYDOWN(20480) THEN player.y = player.y + player.speed
  84.     IF _KEYDOWN(19200) THEN player.x = player.x - player.speed
  85.     IF _KEYDOWN(19712) THEN player.x = player.x + player.speed
  86.  
  87. FUNCTION collision% (x1, y1, x2, y2)
  88.     IF player.x >= x1 AND player.x <= x2 AND player.y >= y1 AND player.y <= y2 THEN
  89.         collision% = 1
  90.     END IF
  91.  
But in your PC do all run good?
I have saved the map1.txt and map2.txt copying them and pasting into QB64IDE and after saving to the HDD as txt files.
Can this make a difference? (QB64ide adds something at the end of the file?... just a minute to create another 2 files using notepad.exe of windows....
Yes the solution is that QB64 adds something at the end of the file... now your code works very good
Title: Re: Blocking stuff D:
Post by: Ashish on April 07, 2020, 08:45:28 am
QB64 adds extra one line at the end of the file and this line just contain CRLF. Due to this, EOF(1) still give 0. Now, since EOF(1) is still false or 0,
input #1,.... is again executed but there are no data due to which "input past end of file" occur.
Title: Re: Blocking stuff D:
Post by: Prithak on April 07, 2020, 11:41:23 am
Well, that was odd but glad you got that sorted out lol
Title: Re: Blocking stuff D:
Post by: TempodiBasic on April 08, 2020, 04:57:32 pm
Hi Ashish
thanks to confirm my hypothesis...
so it can be useful to have the option in QB64IDE to save simple TXT file without this CFRL at the end... as can do Qbasic and QB45.

Yes we can do this using another editor for creating and modifing  simple text files... but is it the same thing?
Title: Re: Blocking stuff D:
Post by: EricE on April 09, 2020, 11:59:15 am
You could use LINE INPUT to read entire line into a string variable.
If the string variable is equal to CRLF, then read another line from the file.
Title: Re: Blocking stuff D:
Post by: TempodiBasic on April 09, 2020, 04:45:48 pm
@EricE 
thanks for your idea
but I'm focusing on the fact that for now it is better to use a filedata.txt made by notepad.exe  and not with QB64IDE to not get an error read past of end of file .
Then we can argue about if it is the best way to store informations for a program, but that is another issue about talk.
Title: Re: Blocking stuff D:
Post by: Unseen Machine on April 09, 2020, 07:27:34 pm
Quote
Then we can argue about if it is the best way to store informations for a program, but that is another issue about talk.

Glad to see this progressing, great work...now as for storing the data, now your using TYPE's to store the information i'd suggest a binary file and just use PUT/GET, it's easier and file reads are much much faster than INPUT/LINE INPUT.

Unseen
Title: Re: Blocking stuff D:
Post by: TempodiBasic on April 10, 2020, 08:14:33 pm
@Unseen Machine
yes this argument is interesting
about using GET and PUT and a different access to the file we must be careful to the UDT with string with variable lenght,
it is easy to GET wrong value from file. It is safe to use Fixed string.

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Blocking stuff D:
Post by: luke on April 10, 2020, 09:53:42 pm
Please note that a variable-length UDT (one with a variable-length string in it) cannot be used with GET or PUT.
Title: Re: Blocking stuff D:
Post by: TempodiBasic on April 11, 2020, 08:57:32 am
Hi Luke
Thanks to reply
I agree with you at 90%  for these two experiences .


this is about OPEN RANDOM and GET

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1280, 720, 32)
  2.  
  3. TYPE vec1
  4.     x AS INTEGER
  5.     y AS INTEGER
  6.     speed AS INTEGER
  7.  
  8. TYPE vec2
  9.     x1 AS INTEGER
  10.     y1 AS INTEGER
  11.     x2 AS INTEGER
  12.     y2 AS INTEGER
  13.     r AS INTEGER
  14.     g AS INTEGER
  15.     b AS INTEGER
  16.     btype AS STRING
  17.     oldx AS INTEGER
  18.     oldy AS INTEGER
  19.     wl AS STRING
  20.  
  21.  
  22. DIM SHARED player AS vec1
  23. DIM SHARED obstacle(9) AS vec2
  24. player.speed = 5
  25.  
  26.  
  27. map = "map1.new"
  28.  
  29. player.x = _WIDTH / 2
  30. player.y = _HEIGHT / 2
  31.  
  32. openMap
  33. j = 1
  34.     CLS
  35.     makeplayer
  36.     playerMovement
  37.  
  38.     FOR j = 1 TO i
  39.         IF obstacle(j).btype = "obs" THEN
  40.             LINE (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2), _RGB32(obstacle(j).r, obstacle(j).g, obstacle(j).b), BF
  41.             IF player.x + 10 >= obstacle(j).x1 AND player.x - 10 <= obstacle(j).x2 AND player.y + 10 >= obstacle(j).y1 AND player.y - 10 <= obstacle(j).y2 THEN
  42.                 player.x = obstacle(j).oldx
  43.                 player.y = obstacle(j).oldy
  44.             ELSE
  45.                 obstacle(j).oldx = player.x
  46.                 obstacle(j).oldy = player.y
  47.             END IF
  48.         ELSEIF obstacle(j).btype = "warp" THEN
  49.             LINE (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2), _RGB32(obstacle(j).r, obstacle(j).g, obstacle(j).b), BF
  50.             IF collision%(obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2) THEN
  51.                 map = obstacle(j).wl ' here there was a mystyping i for j
  52.                 openMap
  53.             END IF
  54.         END IF
  55.     NEXT j
  56.  
  57.     _DISPLAY
  58.     _LIMIT 120
  59.  
  60. SUB openMap ' the program gets past end of file but reads obstacle(i).wl warps
  61.     i = 0
  62.     OPEN map FOR RANDOM AS #1
  63.     'len  = len(obstacle()) len of record is managed at level of parser
  64.     'but you can open a file in RANDOM mode without say  how is the size of UDT
  65.     ' if you use the default size or if you use the single fields
  66.     WHILE NOT EOF(1)
  67.         i = i + 1
  68.         ' get #1, , obstacle()  <--  this is managed at level of parser
  69.         GET #1, , obstacle(i).btype
  70.         GET #1, , obstacle(i).x1
  71.         GET #1, , obstacle(i).y1
  72.         GET #1, , obstacle(i).x2
  73.         GET #1, , obstacle(i).y2
  74.         GET #1, , obstacle(i).r
  75.         GET #1, , obstacle(i).g
  76.         GET #1, , obstacle(i).b
  77.         GET #1, , obstacle(i).wl '  <-- this is not managed at level of parser
  78.         ' so a dummy use of GET (like me in this example),
  79.         'reading each field at a time, reads until out of file
  80.  
  81.         ' just some debug prints...
  82.         _DEST _CONSOLE
  83.         PRINT "i = ", i
  84.         PRINT obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i).y2, obstacle(i).r, obstacle(i).g, obstacle(i).b, obstacle(i).wl
  85.         PRINT "EOF"; EOF(1); " press a key for the next run of cycle"
  86.         _DEST 0
  87.     WEND
  88.     CLOSE #1
  89.  
  90. SUB makeplayer
  91.     CIRCLE (player.x, player.y), 10, _RGB32(100, 200, 255)
  92.     PAINT (player.x, player.y), _RGB32(100, 200, 255), _RGB32(100, 200, 255)
  93.  
  94. SUB playerMovement
  95.     IF _KEYDOWN(18432) THEN player.y = player.y - player.speed
  96.     IF _KEYDOWN(20480) THEN player.y = player.y + player.speed
  97.     IF _KEYDOWN(19200) THEN player.x = player.x - player.speed
  98.     IF _KEYDOWN(19712) THEN player.x = player.x + player.speed
  99.  
  100. FUNCTION collision% (x1, y1, x2, y2)
  101.     IF player.x >= x1 AND player.x <= x2 AND player.y >= y1 AND player.y <= y2 THEN
  102.         collision% = 1
  103.     END IF
  104.  

and this with OPEN BINARY and GET
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1280, 720, 32)
  2.  
  3. TYPE vec1
  4.     x AS INTEGER
  5.     y AS INTEGER
  6.     speed AS INTEGER
  7.  
  8. TYPE vec2
  9.     x1 AS INTEGER
  10.     y1 AS INTEGER
  11.     x2 AS INTEGER
  12.     y2 AS INTEGER
  13.     r AS INTEGER
  14.     g AS INTEGER
  15.     b AS INTEGER
  16.     btype AS STRING
  17.     oldx AS INTEGER
  18.     oldy AS INTEGER
  19.     wl AS STRING
  20.  
  21.  
  22. DIM SHARED player AS vec1
  23. DIM SHARED obstacle(9) AS vec2
  24. player.speed = 5
  25.  
  26.  
  27. map = "map1.new"
  28.  
  29. player.x = _WIDTH / 2
  30. player.y = _HEIGHT / 2
  31.  
  32. openMap
  33. j = 1
  34.     CLS
  35.     makeplayer
  36.     playerMovement
  37.  
  38.     FOR j = 1 TO i
  39.         IF obstacle(j).btype = "obs" THEN
  40.             LINE (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2), _RGB32(obstacle(j).r, obstacle(j).g, obstacle(j).b), BF
  41.             IF player.x + 10 >= obstacle(j).x1 AND player.x - 10 <= obstacle(j).x2 AND player.y + 10 >= obstacle(j).y1 AND player.y - 10 <= obstacle(j).y2 THEN
  42.                 player.x = obstacle(j).oldx
  43.                 player.y = obstacle(j).oldy
  44.             ELSE
  45.                 obstacle(j).oldx = player.x
  46.                 obstacle(j).oldy = player.y
  47.             END IF
  48.         ELSEIF obstacle(j).btype = "warp" THEN
  49.             LINE (obstacle(j).x1, obstacle(j).y1)-(obstacle(j).x2, obstacle(j).y2), _RGB32(obstacle(j).r, obstacle(j).g, obstacle(j).b), BF
  50.             IF collision%(obstacle(j).x1, obstacle(j).y1, obstacle(j).x2, obstacle(j).y2) THEN
  51.                 map = obstacle(j).wl ' here there was a mystyping i for j
  52.                 openMap
  53.             END IF
  54.         END IF
  55.     NEXT j
  56.  
  57.     _DISPLAY
  58.     _LIMIT 120
  59.  
  60. SUB openMap ' the program gets past end of file but reads obstacle(i).wl warps
  61.     i = 0
  62.     OPEN map FOR BINARY AS #1
  63.     WHILE NOT EOF(1)
  64.         i = i + 1
  65.         ' get #1, , obstacle()  <--  this is managed at level of parser
  66.         GET #1, , obstacle(i).btype
  67.         GET #1, , obstacle(i).x1
  68.         GET #1, , obstacle(i).y1
  69.         GET #1, , obstacle(i).x2
  70.         GET #1, , obstacle(i).y2
  71.         GET #1, , obstacle(i).r
  72.         GET #1, , obstacle(i).g
  73.         GET #1, , obstacle(i).b
  74.         GET #1, , obstacle(i).wl '  <-- this is not managed at level of parser
  75.         ' so a dummy use of GET (like me in this example),
  76.         'reading each field at a time, reads until out of file
  77.  
  78.         ' just some debug prints...
  79.         _DEST _CONSOLE
  80.         PRINT "i = ", i
  81.         PRINT obstacle(i).btype, obstacle(i).x1, obstacle(i).y1, obstacle(i).x2, obstacle(i).y2, obstacle(i).r, obstacle(i).g, obstacle(i).b, obstacle(i).wl
  82.         PRINT "EOF"; EOF(1); " press a key for the next run of cycle"
  83.         _DEST 0
  84.     WEND
  85.     CLOSE #1
  86.  
  87. SUB makeplayer
  88.     CIRCLE (player.x, player.y), 10, _RGB32(100, 200, 255)
  89.     PAINT (player.x, player.y), _RGB32(100, 200, 255), _RGB32(100, 200, 255)
  90.  
  91. SUB playerMovement
  92.     IF _KEYDOWN(18432) THEN player.y = player.y - player.speed
  93.     IF _KEYDOWN(20480) THEN player.y = player.y + player.speed
  94.     IF _KEYDOWN(19200) THEN player.x = player.x - player.speed
  95.     IF _KEYDOWN(19712) THEN player.x = player.x + player.speed
  96.  
  97. FUNCTION collision% (x1, y1, x2, y2)
  98.     IF player.x >= x1 AND player.x <= x2 AND player.y >= y1 AND player.y <= y2 THEN
  99.         collision% = 1
  100.     END IF
  101.  
to run these two examples you must use these two map files attached (so anybody cannot copy and past them into QB64Ide and live a different nightmare about code not working, think about EOF failure <-- also this is a possible not ordinary use of the QB64IDE).

in the two examples the code works bad, but that is the issue of coder, while the possibility to GET a variable string in RANDOM and BINARY mode is an issue of language.

Thanks to read and to talk

PS
I agree that best practice is to use RANDOM and BINARY mode with arrays loading in one shot the whole array, and with UDT loading in one shot the whole UDT, but the other way (using the single Field of an UDT) is there from so far time and it is possible.
I agree that best practice is to save/load data to/from file RANDOM and BINARY by code and not for working with a txt file created by text editor, but it is possible. So can be put a forbidden limit to the use of variable string in a BINARY/RANDOM mode?