Author Topic: Why is this happening?  (Read 3949 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
Why is this happening?
« on: April 13, 2019, 12:37:46 am »
Here is a code:
Code: QB64: [Select]
  1. _TITLE "A Promising Game!"
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. COLOR , _RGB32(211, 211, 211)
  4. DIM SHARED y_change AS SINGLE
  5. restart:
  6. x = 200
  7. y = 200
  8. gravity = 9.8
  9.     CLS
  10.     makechar
  11.     k$ = UCASE$(INKEY$)
  12.     IF _KEYDOWN(18432) AND NOT y_change > 50 THEN
  13.         y = y - 25
  14.         y_change = y_change + 1
  15.         y = y + gravity
  16.     ELSEIF _KEYDOWN(20480) AND NOT y_change > 50 THEN
  17.         y = y + 25
  18.         y_change = y_change + 1
  19.         y = y + gravity
  20.     ELSEIF _KEYDOWN(19200) THEN
  21.         x = x - 10
  22.     ELSEIF _KEYDOWN(19712) THEN
  23.         x = x + 10
  24.     END IF
  25.     y = y + gravity
  26.     'Blocks
  27.     'LINE (100, 400)-STEP(200, 50), _RGB32(255, 83, 33), BF
  28.     'IF collision%(x, y, 20, 20, 100, 400, 200, 50) THEN
  29.     '    y = oldy
  30.     '    y_change = 0
  31.     '    gravity = 0
  32.     'ELSE
  33.     '    gravity = 9.8
  34.     '    oldy = y
  35.     'END IF
  36.     makeobs 100, 400
  37.     LINE (500, 400)-STEP(200, 50), _RGB32(127, 255, 127), BF
  38.     IF collision%(x, y, 20, 20, 500, 400, 200, 50) THEN
  39.         CLS
  40.         _PRINTSTRING (300, 300), "LEVEL 1 COMPLETE!"
  41.         END
  42.     END IF
  43.     IF y > 700 THEN GOTO restart
  44.  
  45.     _DISPLAY
  46.     _LIMIT 60
  47.  
  48. SUB makechar
  49.     CIRCLE (x, y), 20, _RGB32(100, 150, 255)
  50.     PAINT (x, y), _RGB32(100, 150, 255), _RGB32(100, 150, 255)
  51.  
  52. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  53.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  54.         collision% = 0
  55.     ELSE
  56.         collision% = 1
  57.     END IF
  58.  
  59. SUB makeobs (bx, by)
  60.     LINE (bx, by)-STEP(200, 50), _RGB32(255, 150, 100), BF
  61.     IF collision%(x, y, 20, 20, bx, by, 200, 50) THEN
  62.         y = oldy
  63.         gravity = 0
  64.         y_change = 0
  65.     ELSE
  66.         oldy = y
  67.         gravity = 9.8
  68.     END IF
  69.  

What is the bug that brings the player to it's initial position?
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: Why is this happening?
« Reply #1 on: April 13, 2019, 12:40:19 am »
Here is a code:
Code: QB64: [Select]
  1. _TITLE "A Promising Game!"
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. COLOR , _RGB32(211, 211, 211)
  4. DIM SHARED y_change AS SINGLE
  5. restart:
  6. x = 200
  7. y = 200
  8. gravity = 9.8
  9.     CLS
  10.     makechar
  11.     k$ = UCASE$(INKEY$)
  12.     IF _KEYDOWN(18432) AND NOT y_change > 50 THEN
  13.         y = y - 25
  14.         y_change = y_change + 1
  15.         y = y + gravity
  16.     ELSEIF _KEYDOWN(20480) AND NOT y_change > 50 THEN
  17.         y = y + 25
  18.         y_change = y_change + 1
  19.         y = y + gravity
  20.     ELSEIF _KEYDOWN(19200) THEN
  21.         x = x - 10
  22.     ELSEIF _KEYDOWN(19712) THEN
  23.         x = x + 10
  24.     END IF
  25.     y = y + gravity
  26.     'Blocks
  27.     'LINE (100, 400)-STEP(200, 50), _RGB32(255, 83, 33), BF
  28.     'IF collision%(x, y, 20, 20, 100, 400, 200, 50) THEN
  29.     '    y = oldy
  30.     '    y_change = 0
  31.     '    gravity = 0
  32.     'ELSE
  33.     '    gravity = 9.8
  34.     '    oldy = y
  35.     'END IF
  36.     makeobs 100, 400
  37.     LINE (500, 400)-STEP(200, 50), _RGB32(127, 255, 127), BF
  38.     IF collision%(x, y, 20, 20, 500, 400, 200, 50) THEN
  39.         CLS
  40.         _PRINTSTRING (300, 300), "LEVEL 1 COMPLETE!"
  41.         END
  42.     END IF
  43.     IF y > 700 THEN GOTO restart
  44.  
  45.     _DISPLAY
  46.     _LIMIT 60
  47.  
  48. SUB makechar
  49.     CIRCLE (x, y), 20, _RGB32(100, 150, 255)
  50.     PAINT (x, y), _RGB32(100, 150, 255), _RGB32(100, 150, 255)
  51.  
  52. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  53.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  54.         collision% = 0
  55.     ELSE
  56.         collision% = 1
  57.     END IF
  58.  
  59. SUB makeobs (bx, by)
  60.     LINE (bx, by)-STEP(200, 50), _RGB32(255, 150, 100), BF
  61.     IF collision%(x, y, 20, 20, bx, by, 200, 50) THEN
  62.         y = oldy
  63.         gravity = 0
  64.         y_change = 0
  65.     ELSE
  66.         oldy = y
  67.         gravity = 9.8
  68.     END IF
  69.  

What is the bug that brings the player to it's initial position?

Nevermind, Just realizedthat the value of oldy wasn't global xD
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: Why is this happening?
« Reply #2 on: April 13, 2019, 12:42:13 am »
Alright, another question!

When I press the upper arrow key and the left arrow key at the same time, only the upper arrow key works but not the left arrow key. What is causing it and How do I solve it? HELP!

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

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why is this happening?
« Reply #3 on: April 13, 2019, 01:54:23 am »
Alright, another question!

When I press the upper arrow key and the left arrow key at the same time, only the upper arrow key works but not the left arrow key. What is causing it and How do I solve it? HELP!

-Prithak

It’s because the _KEYDOWN check is in an ELSEIF statement.  Make all those become independent IF checks instead.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: Why is this happening?
« Reply #4 on: April 13, 2019, 06:11:58 am »
It’s because the _KEYDOWN check is in an ELSEIF statement.  Make all those become independent IF checks instead.

Thanks man, wouldh've never figured out that on my own!
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline AmandaC

  • Newbie
  • Posts: 1
    • View Profile
Re: Why is this happening?
« Reply #5 on: May 06, 2019, 12:37:13 pm »
Here is a code:
Code: QB64: [Select]
  1. _TITLE "A Promising Game!"
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. COLOR , _RGB32(211, 211, 211)
  4. DIM SHARED y_change AS SINGLE
  5. restart:
  6. x = 200
  7. y = 200
  8. gravity = 9.8
  9.     CLS
  10.     makechar
  11.     k$ = UCASE$(INKEY$)
  12.     IF _KEYDOWN(18432) AND NOT y_change > 50 THEN
  13.         y = y - 25
  14.         y_change = y_change + 1
  15.         y = y + gravity
  16.     ELSEIF _KEYDOWN(20480) AND NOT y_change > 50 THEN
  17.         y = y + 25
  18.         y_change = y_change + 1
  19.         y = y + gravity
  20.     ELSEIF _KEYDOWN(19200) THEN
  21.         x = x - 10
  22.     ELSEIF _KEYDOWN(19712) THEN
  23.         x = x + 10
  24.     END IF
  25.     y = y + gravity
  26.     'Blocks
  27.     'LINE (100, 400)-STEP(200, 50), _RGB32(255, 83, 33), BF
  28.     'IF collision%(x, y, 20, 20, 100, 400, 200, 50) THEN
  29.     '    y = oldy
  30.     '    y_change = 0
  31.     '    gravity = 0
  32.     'ELSE
  33.     '    gravity = 9.8
  34.     '    oldy = y
  35.     'END IF
  36.     makeobs 100, 400
  37.     LINE (500, 400)-STEP(200, 50), _RGB32(127, 255, 127), BF
  38.     IF collision%(x, y, 20, 20, 500, 400, 200, 50) THEN
  39.         CLS
  40.         _PRINTSTRING (300, 300), "LEVEL 1 COMPLETE!"
  41.         END
  42.     END IF
  43.     IF y > 700 THEN GOTO restart
  44.  
  45.     _DISPLAY
  46.     _LIMIT 60
  47.  
  48. SUB makechar
  49.     CIRCLE (x, y), 20, _RGB32(100, 150, 255)
  50.     PAINT (x, y), _RGB32(100, 150, 255), _RGB32(100, 150, 255)
  51.  
  52. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  53.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  54.         collision% = 0
  55.     ELSE
  56.         collision% = 1
  57.     END IF
  58.  
  59. SUB makeobs (bx, by)
  60.     LINE (bx, by)-STEP(200, 50), _RGB32(255, 150, 100), BF
  61.     IF collision%(x, y, 20, 20, bx, by, 200, 50) THEN
  62.         y = oldy
  63.         gravity = 0
  64.         y_change = 0
  65.     ELSE
  66.         oldy = y
  67.         gravity = 9.8
  68.     END IF
  69.  

What is the bug that brings the player to it's initial position?
It’s because the _KEYDOWN check gimp freejobalert notepad++  is in an ELSEIF statement.  Make all those become independent IF checks instead.
Thanks Both of you for this. I was also facing the same and happened to find this has already been answered.

Regards,
Amanda
« Last Edit: May 20, 2019, 04:44:31 pm by AmandaC »