Author Topic: Not working!  (Read 3444 times)

0 Members and 1 Guest are viewing this topic.

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Not working!
« on: July 22, 2019, 03:50:59 am »
Alright! Back to programming with my exams now gone! Here I coded this stuff:

Code: [Select]
screen _newimage(800,600,32)

dim shared x as integer
dim shared y as integer
dim shared oldx as integer
dim shared oldy as integer

x = 100
y = 200

color , _rgb32(50,200,0)

cls

do
cls

open "blocks.txt" for input as #1

while not eof(1)

input #1, x1,y1,x2,y2,r,g,b

makeobs x1,y1,x2,y2,r,g,b
wend

close #1

    IF _KEYDOWN(CVI(CHR$(0) + "P")) THEN y = y - 5   '_KEYDOWN(20480)
    IF _KEYDOWN(CVI(CHR$(0) + "H")) THEN y = y + 5   '_KEYDOWN(18432)
    IF _KEYDOWN(CVI(CHR$(0) + "K")) THEN x = x + 5   '_KEYDOWN(19200)
    IF _KEYDOWN(CVI(CHR$(0) + "M")) THEN x = x - 5   '_KEYDOWN(19712)

'The sprites will be 20x20 pixels, I believe...
line (_width/2,_height/2)-step(20,20),_rgb32(255,200,100),BF

_printstring(0,0),str$(x) + " " + str$(y)

_limit 100
_Display
loop
 
FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
    IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
        collision% = 0
    ELSE
        collision% = 1
    END IF
END FUNCTION

sub makeobs (x1,y1,w,h,r,g,b)

line(x + x1, y + y1)-step(w,h),_rgb32(r,g,b),BF
Line(x + x1, y + y1)-step(w,h),_Rgb32(255,255,255),B
'200, 200 - 220, 220

if collision%(_width/2,_height/2,20,20,x + x1, y + y1,w,h) then
y = oldy
x = oldx
else
oldy = y
oldx = x
end if


end sub

And here's "blocks.txt"
Code: [Select]
200,100,100,100,100,200,255
200,300,100,100,100,200,255
100,200,100,100,100,200,255
300,200,100,100,100,200,255

Ok. For some reason, the character's only being blocked by the first obstacle? WHY??

-Prithak
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Not working!
« Reply #1 on: July 22, 2019, 10:39:20 am »
EDIT: oops! now I see where you are checking collisions...

Here is code without extra file:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3.  
  4. x = 100
  5. y = 200
  6.  
  7. COLOR , _RGB32(50, 200, 0)
  8.  
  9.  
  10.     CLS
  11.  
  12.     'OPEN "blocks.txt" FOR INPUT AS #1
  13.  
  14.     'WHILE NOT EOF(1)
  15.  
  16.     '    INPUT #1, x1, y1, x2, y2, r, g, b
  17.  
  18.     '    makeobs x1, y1, x2, y2, r, g, b
  19.     'WEND
  20.  
  21.     'CLOSE #1
  22.     RESTORE blocks
  23.     FOR b = 1 TO 4
  24.         READ x1, y1, x2, y2, r, g, b
  25.         makeobs x1, y1, x2, y2, r, g, b
  26.     NEXT
  27.     IF _KEYDOWN(CVI(CHR$(0) + "P")) THEN y = y - 5 '_KEYDOWN(20480)
  28.     IF _KEYDOWN(CVI(CHR$(0) + "H")) THEN y = y + 5 '_KEYDOWN(18432)
  29.     IF _KEYDOWN(CVI(CHR$(0) + "K")) THEN x = x + 5 '_KEYDOWN(19200)
  30.     IF _KEYDOWN(CVI(CHR$(0) + "M")) THEN x = x - 5 '_KEYDOWN(19712)
  31.  
  32.     'The sprites will be 20x20 pixels, I believe...
  33.     LINE (_WIDTH / 2, _HEIGHT / 2)-STEP(20, 20), _RGB32(255, 200, 100), BF
  34.  
  35.     _PRINTSTRING (0, 0), STR$(x) + " " + STR$(y)
  36.  
  37.     _LIMIT 100
  38.     _DISPLAY
  39. blocks:
  40. DATA 200,100,100,100,100,200,255
  41. DATA 200,300,100,100,100,200,255
  42. DATA 100,200,100,100,100,200,255
  43. DATA 300,200,100,100,100,200,255
  44.  
  45. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  46.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  47.         collision% = 0
  48.     ELSE
  49.         collision% = 1
  50.     END IF
  51.  
  52. SUB makeobs (x1, y1, w, h, r, g, b)
  53.  
  54.     LINE (x + x1, y + y1)-STEP(w, h), _RGB32(r, g, b), BF
  55.     LINE (x + x1, y + y1)-STEP(w, h), _RGB32(255, 255, 255), B
  56.     '200, 200 - 220, 220
  57.  
  58.     IF collision%(_WIDTH / 2, _HEIGHT / 2, 20, 20, x + x1, y + y1, w, h) THEN
  59.         y = oldy
  60.         x = oldx
  61.     ELSE
  62.         oldy = y
  63.         oldx = x
  64.     END IF
  65.  

When I press up key, "sprite" goes down and vice versa..
When I press left arrow, "sprite" goes right and vice versa...


« Last Edit: July 22, 2019, 10:54:57 am by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Not working!
« Reply #2 on: July 22, 2019, 10:51:53 am »
Just a shot in the dark here Prithak - might it be the Do loop, on the 2nd loop is already reading an EOF(1) in the inner While Loop (so Wend Until EOF(1)) rather than While not eof(1), if that can be done. (I haven't worked with While/Wend). I would be interested in knowing what the solution turned out to be.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Not working!
« Reply #3 on: July 22, 2019, 11:29:35 am »
Heck! the first question is why aren't all the obstacles being drawn!!!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Not working!
« Reply #4 on: July 22, 2019, 11:49:17 am »
There was some kind of naming conflict when reading into variables the data when I changed variable names I got the 4 boxes to draw, but the logic of acting as obstacle was off.

In main loop,
1. clear screen
2. draw obstacles
3. update sprite x, y with key press updates
4. check if can move sprite with updates with collision detection
5. if OK use new x, y else use saved old x, y to update sprite position
6. draw sprite

This works:
Code: QB64: [Select]
  1. DEFINT A-Z
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3.  
  4. DIM SHARED oldx
  5. DIM SHARED oldy
  6. DIM i, x1, y1, w1, h1, r1, g1, b1
  7. x = 0
  8. y = 0
  9.  
  10. COLOR , _RGB32(50, 200, 0)
  11.  
  12.  
  13. 'Alternate Method check
  14. 'read data into arrays and try drawing from arrays
  15. 'DIM ox(3), oy(3), ow(3), oh(3), ored(3), og(3), ob(3)
  16. 'FOR i = 0 TO 3
  17. '    READ x1, y1, w1, h1, r1, g1, b1
  18. '    ox(i) = x1: oy(i) = y1: ow(i) = w1: oh(i) = h1: ored(i) = r1: og(i) = g1: ob(i) = b1
  19. 'NEXT
  20.  
  21.     CLS
  22.  
  23.     'OPEN "blocks.txt" FOR INPUT AS #1
  24.     'WHILE NOT EOF(1)
  25.     '    INPUT #1, x1, y1, x2, y2, r, g, b
  26.     '    makeobs x1, y1, x2, y2, r, g, b
  27.     'WEND
  28.     'CLOSE #1
  29.  
  30.     RESTORE blocks 'OK works now, there was a problem with variable naming
  31.     FOR i = 0 TO 3
  32.         READ x1, y1, w1, h1, r1, g1, b1
  33.         makeobs x1, y1, w1, h1, r1, g1, b1
  34.     NEXT
  35.     '_DISPLAY
  36.  
  37.     'ok this draws all obstacles
  38.     'FOR i = 0 TO 3
  39.     '    makeobs ox(i), oy(i), ow(i), oh(i), ored(i), og(i), ob(i)
  40.     'NEXT
  41.  
  42.     oldy = y: oldx = x
  43.     IF _KEYDOWN(CVI(CHR$(0) + "P")) THEN y = y + 5 '_KEYDOWN(20480)
  44.     IF _KEYDOWN(CVI(CHR$(0) + "H")) THEN y = y - 5 '_KEYDOWN(18432)
  45.     IF _KEYDOWN(CVI(CHR$(0) + "K")) THEN x = x - 5 '_KEYDOWN(19200)
  46.     IF _KEYDOWN(CVI(CHR$(0) + "M")) THEN x = x + 5 '_KEYDOWN(19712)
  47.  
  48.     RESTORE blocks 'OK works now, there was a problem with variable naming
  49.     FOR i = 0 TO 3
  50.         READ x1, y1, w1, h1, r1, g1, b1
  51.         IF collision%(x, y, 20, 20, x1, y1, w1, h1) THEN
  52.             y = oldy
  53.             x = oldx
  54.             'ELSE
  55.             '    oldy = y
  56.             '    oldx = x
  57.         END IF
  58.  
  59.     NEXT
  60.  
  61.  
  62.     'The sprites will be 20x20 pixels, I believe...
  63.     LINE (x, y)-STEP(20, 20), _RGB32(255, 200, 100), BF
  64.  
  65.     _PRINTSTRING (0, 0), STR$(x) + " " + STR$(y)
  66.  
  67.     _LIMIT 100
  68.     _DISPLAY
  69. blocks:
  70. DATA 100,100,100,100,255,0,0
  71. DATA 200,200,100,100,0,0,255
  72. DATA 300,300,100,100,255,255,0
  73. DATA 400,400,100,100,0,128,128
  74.  
  75. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  76.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  77.         collision% = 0
  78.     ELSE
  79.         collision% = 1
  80.     END IF
  81.  
  82. SUB makeobs (x1, y1, w, h, r, g, b)
  83.     LINE (x1, y1)-STEP(w, h), _RGB32(r, g, b), BF
  84.     LINE (x1, y1)-STEP(w, h), _RGB32(255, 255, 255), B
  85.  
  86.  
  87.  

EDIT: commented out code no longer using, ie the alternate arrays method to hold block data.
« Last Edit: July 22, 2019, 11:58:35 am by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Not working!
« Reply #5 on: July 22, 2019, 05:21:45 pm »
Solved, now it works

before showing it please read:

2 remarks:

1. no need two consecutive CLS
Code: QB64: [Select]
  1. 'CLS   <-- no needed
  2.  
  3.     CLS

2. why do you use a single letter name variable? that is the trap that you have used against yourself!

Code: QB64: [Select]
  1.     FOR b = 1 TO 4
  2.         READ x1, y1, x2, y2, r, g, b
  3.         makeobs x1, y1, x2, y2, r, g, b
O_o  you use b as counter of loop FOR NEXT and as value of rgb parameter read from file (original version) or from DATA block (Bplus version) in teh FOR NEXT loop.
So what's happening? Nothing else that the for next loop runs once a time and shows one block.

and this is the result
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3.  
  4. x = 100
  5. y = 200
  6.  
  7. COLOR , _RGB32(50, 200, 0)
  8.  
  9. 'CLS   <-- no needed
  10.  
  11.     CLS
  12.  
  13.     'OPEN "blocks.txt" FOR INPUT AS #1
  14.  
  15.     'WHILE NOT EOF(1)
  16.  
  17.     '    INPUT #1, x1, y1, x2, y2, r, g, b
  18.  
  19.     '    makeobs x1, y1, x2, y2, r, g, b
  20.     'WEND
  21.  
  22.     'CLOSE #1
  23.     RESTORE blocks
  24.     FOR b1 = 1 TO 4 ' here counter must have a different name of variables used into it
  25.         READ x1, y1, x2, y2, r, g, b
  26.         makeobs x1, y1, x2, y2, r, g, b
  27.  
  28.     NEXT
  29.     IF _KEYDOWN(CVI(CHR$(0) + "P")) THEN y = y - 5 '_KEYDOWN(20480)
  30.     IF _KEYDOWN(CVI(CHR$(0) + "H")) THEN y = y + 5 '_KEYDOWN(18432)
  31.     IF _KEYDOWN(CVI(CHR$(0) + "K")) THEN x = x + 5 '_KEYDOWN(19200)
  32.     IF _KEYDOWN(CVI(CHR$(0) + "M")) THEN x = x - 5 '_KEYDOWN(19712)
  33.  
  34.     'The sprites will be 20x20 pixels, I believe...
  35.     LINE (_WIDTH / 2, _HEIGHT / 2)-STEP(20, 20), _RGB32(255, 200, 100), BF
  36.  
  37.     _PRINTSTRING (0, 0), STR$(x) + " " + STR$(y)
  38.  
  39.     _LIMIT 100
  40.     _DISPLAY
  41. blocks:
  42. DATA 200,100,100,100,100,200,255
  43. DATA 200,300,100,100,100,200,255
  44. DATA 100,200,100,100,100,200,255
  45. DATA 300,200,100,100,100,200,255
  46.  
  47. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  48.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  49.         collision% = 0
  50.     ELSE
  51.         collision% = 1
  52.     END IF
  53.  
  54. SUB makeobs (x1, y1, w, h, r, g, b)
  55.  
  56.     LINE (x + x1, y + y1)-STEP(w, h), _RGB32(r, g, b), BF
  57.     LINE (x + x1, y + y1)-STEP(w, h), _RGB32(255, 255, 255), B
  58.     '200, 200 - 220, 220
  59.  
  60.     IF collision%(_WIDTH / 2, _HEIGHT / 2, 20, 20, x + x1, y + y1, w, h) THEN
  61.         y = oldy
  62.         x = oldx
  63.     ELSE
  64.         oldy = y
  65.         oldx = x
  66.     END IF
  67.  
  68.  

Well,
now you must fix some bugs:
1. collision detects only the collision of the first square (that is drawn first, the top of the crux)
2. when collision happens the top square dances whiel the rest of the crux stands there.

this second issue is fixed if in the sub Makeobs you first test the collision and then you draw the square/sprite
like here
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3.  
  4. x = 100
  5. y = 200
  6.  
  7. COLOR , _RGB32(50, 200, 0)
  8.  
  9. 'CLS   <-- no needed
  10.  
  11.     CLS
  12.  
  13.     'OPEN "blocks.txt" FOR INPUT AS #1
  14.  
  15.     'WHILE NOT EOF(1)
  16.  
  17.     '    INPUT #1, x1, y1, x2, y2, r, g, b
  18.  
  19.     '    makeobs x1, y1, x2, y2, r, g, b
  20.     'WEND
  21.  
  22.     'CLOSE #1
  23.     RESTORE blocks
  24.     FOR b1 = 1 TO 4 ' here counter must have a different name of variables used into it
  25.         READ x1, y1, x2, y2, r, g, b
  26.         makeobs x1, y1, x2, y2, r, g, b
  27.  
  28.     NEXT
  29.     IF _KEYDOWN(CVI(CHR$(0) + "P")) THEN y = y - 5 '_KEYDOWN(20480)
  30.     IF _KEYDOWN(CVI(CHR$(0) + "H")) THEN y = y + 5 '_KEYDOWN(18432)
  31.     IF _KEYDOWN(CVI(CHR$(0) + "K")) THEN x = x + 5 '_KEYDOWN(19200)
  32.     IF _KEYDOWN(CVI(CHR$(0) + "M")) THEN x = x - 5 '_KEYDOWN(19712)
  33.  
  34.     'The sprites will be 20x20 pixels, I believe...
  35.     LINE (_WIDTH / 2, _HEIGHT / 2)-STEP(20, 20), _RGB32(255, 200, 100), BF
  36.  
  37.     _PRINTSTRING (0, 0), STR$(x) + " " + STR$(y)
  38.  
  39.     _LIMIT 100
  40.     _DISPLAY
  41. blocks:
  42. DATA 200,100,100,100,100,200,255
  43. DATA 200,300,100,100,100,200,255
  44. DATA 100,200,100,100,100,200,255
  45. DATA 300,200,100,100,100,200,255
  46.  
  47. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  48.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  49.         collision% = 0
  50.     ELSE
  51.         collision% = 1
  52.     END IF
  53.  
  54. SUB makeobs (x1, y1, w, h, r, g, b)
  55.  
  56.  
  57.     IF collision%(_WIDTH / 2, _HEIGHT / 2, 20, 20, x + x1, y + y1, w, h) THEN
  58.         y = oldy
  59.         x = oldx
  60.     ELSE
  61.         oldy = y
  62.         oldx = x
  63.     END IF
  64.     LINE (x + x1, y + y1)-STEP(w, h), _RGB32(r, g, b), BF
  65.     LINE (x + x1, y + y1)-STEP(w, h), _RGB32(255, 255, 255), B
  66.     '200, 200 - 220, 220
  67.  
  68.  
  69.  

the first issue IMHO is fixed if you use an array of dimensional cohordinates for each square/object/sprite of the compilation
for example
Code: QB64: [Select]
  1. TYPE Cohor
  2.     x AS INTEGER
  3.     y AS INTEGER
  4.     OldX AS INTEGER
  5.     OldY AS INTEGER
  6.  
  7. DIM SHARED co_Sprites(1 TO 4) AS Cohor

and it would be better to divide the two action test_collision and Draw_Sprite with no nesting.

Thanks to read
and Good Coding
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Not working!
« Reply #6 on: July 22, 2019, 05:46:40 pm »
Ah yes! I doubled up on using the variable b in the first code mod avoiding the file load.

Thanks TempodiBasic, mystery solved, explains why my first mod wasn't drawing correctly.

Still my 2nd code mod works just fine.

Prithak had arguments to collision function pretty messed up.
This sprite ain't going nowhere:
Code: QB64: [Select]
  1.    'The sprites will be 20x20 pixels, I believe...
  2.     LINE (_WIDTH / 2, _HEIGHT / 2)-STEP(20, 20), _RGB32(255, 200, 100), BF
  3.  

Wait are we moving the sprite around the blocks or the blocks around the sprite? It didn't occur to me it might be the 2nd thing. ;( That would explain why the arrows worked in reverse!
« Last Edit: July 22, 2019, 05:52:26 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Not working! > Fixed for both ways
« Reply #7 on: July 22, 2019, 07:24:43 pm »
TempodiBasic:
Quote
and it would be better to divide the two action test_collision and Draw_Sprite with no nesting.

Yep! You have to check that the sprite is clear of all the obstacles before drawing any, because you can't draw some at new x, y and some at old x, y.

OK so we are moving the blocks around the sprite :)
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3.  
  4. 'this makes blocks visible in top left corner
  5. x = -100
  6. y = -100
  7. COLOR , _RGB32(50, 200, 0)
  8.     CLS
  9.  
  10.     'The sprites will be 20x20 pixels, I believe...
  11.     LINE (_WIDTH / 2, _HEIGHT / 2)-STEP(20, 20), _RGB32(255, 200, 100), BF
  12.  
  13.     'save oldx, oldy NOW before changing x and y
  14.     oldx = x: oldy = y
  15.  
  16.     'now change x, y and test with blocks
  17.     IF _KEYDOWN(CVI(CHR$(0) + "P")) THEN y = y - 5 '_KEYDOWN(20480)
  18.     IF _KEYDOWN(CVI(CHR$(0) + "H")) THEN y = y + 5 '_KEYDOWN(18432)
  19.     IF _KEYDOWN(CVI(CHR$(0) + "K")) THEN x = x + 5 '_KEYDOWN(19200)
  20.     IF _KEYDOWN(CVI(CHR$(0) + "M")) THEN x = x - 5 '_KEYDOWN(19712)
  21.  
  22.     'test new x, y with all the blocks before drawing anything, we must establish x, y is clear
  23.     RESTORE blocks
  24.     FOR b1 = 1 TO 4 ' here counter must have a different name of variables used into it
  25.         READ x1, y1, x2, y2, r, g, b
  26.         IF collision(x1 + x, y1 + y, x2, y2, _WIDTH / 2, _HEIGHT / 2, 20, 20) THEN
  27.             x = oldx: y = oldy
  28.             EXIT FOR
  29.         END IF
  30.     NEXT
  31.     'OK now draw blocks with x, y
  32.     RESTORE blocks
  33.     FOR b1 = 1 TO 4 ' here counter must have a different name of variables used into it
  34.         READ x1, y1, x2, y2, r, g, b
  35.         makeobs x1, y1, x2, y2, r, g, b
  36.     NEXT
  37.     _PRINTSTRING (0, 0), STR$(x) + " " + STR$(y)
  38.     _DISPLAY
  39.     _LIMIT 100
  40. blocks:
  41. DATA 200,100,100,100,100,200,255
  42. DATA 200,300,100,100,100,200,255
  43. DATA 100,200,100,100,100,200,255
  44. DATA 300,200,100,100,100,200,255
  45.  
  46. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  47.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  48.         collision% = 0
  49.     ELSE
  50.         collision% = 1
  51.     END IF
  52.  
  53. SUB makeobs (x1, y1, w, h, r, g, b)
  54.     LINE (x + x1, y + y1)-STEP(w, h), _RGB32(r, g, b), BF
  55.     LINE (x + x1, y + y1)-STEP(w, h), _RGB32(255, 255, 255), B
  56.  

OK so now it's works both ways!
« Last Edit: July 22, 2019, 07:30:37 pm by bplus »

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: Not working!
« Reply #8 on: July 22, 2019, 11:17:31 pm »
Thank you everyone for helping me out! Thanks to you now I can really continue making this game xD! Well... I guess I'm gonna go coding whilst solving a Rubik's cube lol!

-Prithak
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END