Author Topic: Why is it not blocking?  (Read 2635 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 it not blocking?
« on: April 14, 2019, 03:20:29 am »
Man, I've tried everything! It is not working!
Code: QB64: [Select]
  1. 'First Prototype of my game project!
  2. 'Never knew I would get this far!
  3. 'A Special thanks to Petr, Pete, bplus, _vince, TempodiBasic, SMcNeil Let me know if I forgot your name here xD
  4. '-Prithak
  5.  
  6. _TITLE "Japang!"
  7. SCREEN _NEWIMAGE(800, 600, 32)
  8. COLOR _RGB32(0, 0, 0), _RGB32(211, 211, 211)
  9. DIM SHARED y_change AS SINGLE
  10.  
  11. main:
  12.     _PRINTSTRING (600, 100), "Japang!"
  13.     _PRINTSTRING (600, 300), "Play"
  14.     _PRINTSTRING (600, 350), "Help"
  15.  
  16.     _PRINTSTRING (600, 400), "Exit"
  17.     LINE (570, 340)-(600 + LEN("help") * 16, 375), _RGB32(255, 255, 255), B
  18.     LINE (570, 290)-(600 + LEN("play") * 16, 325), _RGB32(255, 255, 255), B
  19.     LINE (570, 390)-(600 + LEN("exit") * 16, 425), _RGB32(255, 255, 255), B
  20.     IF mouseclick(570, 290, 600 + LEN("play") * 16, 325) THEN
  21.         game
  22.     END IF
  23.     IF mouseclick(570, 340, 600 + LEN("help") * 16, 375) THEN
  24.         GOTO help
  25.     END IF
  26.     IF mouseclick(570, 290, 600 + LEN("exit") * 16, 425) THEN
  27.         SYSTEM
  28.     END IF
  29.     _DISPLAY
  30.  
  31. help:
  32. PRINT "Use the arrow keys to move around!"
  33. PRINT "Use the upper arrow key to jump!"
  34. PRINT "Get to the green block for victory!"
  35. PRINT "Touching any red blocks will kill you!"
  36. GOTO main
  37.  
  38. SUB game
  39. IF level = 2 THEN GOTO lvl2
  40. IF level = 3 THEN GOTO lvl3
  41. IF level = 4 THEN GOTO lvl4
  42. IF level = 5 THEN GOTO lvl5
  43. camx = 0
  44. camy = 0
  45. 'Just for testing!
  46. x = 200
  47. y = 200
  48. gravity = 9.8
  49.     CLS
  50.     makechar
  51.     IF _KEYDOWN(19200) THEN
  52.         x = x - 10
  53.  
  54.     END IF
  55.     IF _KEYDOWN(19712) THEN
  56.         x = x + 10
  57.  
  58.     END IF
  59.     IF _KEYDOWN(18432) THEN
  60.         jump_i = 1
  61.     END IF
  62.  
  63.     IF jump_i = 1 AND y > 100 THEN
  64.         y = y - 20
  65.         cycle = cycle + 1
  66.         IF cycle > 20 THEN cycle = 0: jump_i = -1
  67.     END IF
  68.  
  69.     y = y + gravity
  70.     'Blocks
  71.     makeobs 100, 400
  72.  
  73.     LINE (500, 400)-STEP(200, 50), _RGB32(127, 255, 127), BF
  74.     IF collision%(x, y, 20, 20, 500, 400, 200, 50) THEN
  75.         CLS
  76.         _PRINTSTRING (300, 300), "LEVEL 1 COMPLETE!"
  77.         _DISPLAY
  78.         _DELAY 2
  79.         GOTO lvl2
  80.         END
  81.     END IF
  82.     IF y > 600 THEN game
  83.  
  84.     _DISPLAY
  85.     _LIMIT 60
  86.  
  87. lvl2:
  88. level = 2
  89. x = 200
  90. y = 400
  91.     CLS
  92.     makechar
  93.     IF _KEYDOWN(19200) THEN
  94.         x = x - 10
  95.  
  96.     END IF
  97.     IF _KEYDOWN(19712) THEN
  98.         x = x + 10
  99.  
  100.     END IF
  101.     IF _KEYDOWN(18432) THEN
  102.         jump_i = 1
  103.     END IF
  104.  
  105.     IF jump_i = 1 AND y > 100 THEN
  106.         y = y - 20
  107.         cycle = cycle + 1
  108.         IF cycle > 20 THEN cycle = 0: jump_i = -1
  109.     END IF
  110.  
  111.     y = y + gravity
  112.  
  113.     IF y > 600 THEN GOTO lvl2
  114.     angle = angle + 5
  115.  
  116.     rotate _WIDTH / 2, _HEIGHT / 2, angle
  117.  
  118.  
  119.     makeobs 100, 500
  120.  
  121.     makeobs 500, 500
  122.  
  123.     IF coins < 1 THEN
  124.         CIRCLE (600, 480), 5, _RGB32(255, 255, 17)
  125.         PAINT (600, 480), _RGB32(255, 255, 17), _RGB32(255, 255, 17)
  126.         IF collision%(x, y, 20, 20, 600, 480, 5, 5) THEN
  127.             coins = coins + 1
  128.         END IF
  129.     END IF
  130.  
  131.     LINE (100, 300)-STEP(200, 50), _RGB32(127, 255, 127), BF
  132.     IF collision%(x, y, 20, 20, 100, 300, 200, 50) THEN
  133.         CLS
  134.         _PRINTSTRING (300, 300), "LEVEL 2 COMPLETE!"
  135.         _PRINTSTRING (300, 325), "Coins collected: " + STR$(coins)
  136.         _DISPLAY
  137.         _DELAY 2
  138.         GOTO lvl3
  139.         END
  140.     END IF
  141.  
  142.     _DISPLAY
  143.     _LIMIT 60
  144.  
  145. lvl3:
  146. level = 3
  147. x = 100
  148. y = 300
  149.     CLS
  150.     makechar
  151.  
  152.     IF _KEYDOWN(19200) THEN
  153.         x = x - 10
  154.  
  155.     END IF
  156.     IF _KEYDOWN(19712) THEN
  157.         x = x + 10
  158.  
  159.     END IF
  160.     IF _KEYDOWN(18432) THEN
  161.         jump_i = 1
  162.     END IF
  163.  
  164.     IF jump_i = 1 AND y > 100 THEN
  165.         y = y - 20
  166.         cycle = cycle + 1
  167.         IF cycle > 20 THEN cycle = 0: jump_i = -1
  168.     END IF
  169.  
  170.     y = y + gravity
  171.  
  172.     IF coins < 2 THEN
  173.         CIRCLE (650, 470), 5, _RGB32(255, 255, 17)
  174.         PAINT (650, 470), _RGB32(255, 255, 17), _RGB32(255, 255, 17)
  175.         IF collision%(x, y, 20, 20, 650, 470, 5, 5) THEN
  176.             coins = coins + 1
  177.         END IF
  178.     END IF
  179.  
  180.  
  181.     makesobs 50, 500, 100, 50
  182.  
  183.     angle = angle + 5
  184.  
  185.     rotate _WIDTH / 2, 400, angle
  186.     rotate _WIDTH / 2, 200, angle
  187.  
  188.     makesobs 600, 500, 100, 50
  189.  
  190.     LINE (100, 200)-STEP(200, 50), _RGB32(127, 255, 127), BF
  191.     IF collision%(x, y, 20, 20, 100, 50, 200, 50) THEN
  192.         CLS
  193.         _PRINTSTRING (300, 300), "LEVEL 3 COMPLETE!"
  194.         _PRINTSTRING (300, 325), "Total coins: " + STR$(coins)
  195.         _DISPLAY
  196.         _DELAY 2
  197.         GOTO lvl4
  198.         END
  199.     END IF
  200.  
  201.     IF y > 600 THEN GOTO lvl3
  202.  
  203.     _DISPLAY
  204.     _LIMIT 60
  205. lvl4:
  206. level = 4
  207. x = 100
  208. y = 300
  209. camx = 0
  210. camy = 0
  211.     CLS
  212.     makechar
  213.  
  214.     IF _KEYDOWN(19200) THEN
  215.         x = x - 10
  216.         camx = camx + 5
  217.     END IF
  218.     IF _KEYDOWN(19712) THEN
  219.         x = x + 10
  220.         camx = camx - 5
  221.     END IF
  222.     IF _KEYDOWN(18432) THEN
  223.         jump_i = 1
  224.     END IF
  225.  
  226.  
  227.     IF jump_i = 1 AND y > 100 THEN
  228.         y = y - 20
  229.         camy = camy - 5
  230.         cycle = cycle + 1
  231.         IF cycle > 20 THEN cycle = 0: jump_i = -1
  232.     END IF
  233.  
  234.     y = y + gravity
  235.     camy = camy + 2
  236.     _PRINTSTRING (0, 0), "Get used to the camera folks!"
  237.     IF camy > 50 THEN camy = 50
  238.  
  239.  
  240.     makeobs camx + 50, camy + 500
  241.     makeredobs camx + 300, camy + 500
  242.     makeobs camx + 550, camy + 500
  243.     makeredobs camx + 800, camy + 500
  244.  
  245.     LINE (camx + 1050, camy + 500)-STEP(200, 50), _RGB32(127, 255, 127), BF
  246.     IF collision%(x, y, 20, 20, camx + 1050, camy + 500, 200, 50) THEN
  247.         CLS
  248.         _PRINTSTRING (300, 300), "LEVEL 4 COMPLETE!"
  249.         _DISPLAY
  250.         _DELAY 2
  251.         GOTO lvl5
  252.         END
  253.     END IF
  254.  
  255.     IF y > 600 THEN GOTO lvl4
  256.  
  257.     _LIMIT 60
  258.     _DISPLAY
  259.  
  260. lvl5:
  261. level = 5
  262. x = 200
  263. y = 100
  264. camx = 0
  265. camy = 0
  266.     CLS
  267.     makechar
  268.  
  269.     IF _KEYDOWN(19200) THEN
  270.         x = x - 10
  271.         camx = camx + 5
  272.     END IF
  273.     IF _KEYDOWN(19712) THEN
  274.         x = x + 10
  275.         camx = camx - 5
  276.     END IF
  277.     IF _KEYDOWN(18432) THEN
  278.         jump_i = 1
  279.     END IF
  280.  
  281.  
  282.     IF jump_i = 1 AND y > 100 THEN
  283.         y = y - 20
  284.         camy = camy - 5
  285.         cycle = cycle + 1
  286.         IF cycle > 20 THEN cycle = 0: jump_i = -1
  287.     END IF
  288.  
  289.     y = y + gravity
  290.     camy = camy + 2
  291.  
  292.     IF camy > 50 THEN camy = 50
  293.  
  294.     makeobs camx + 50, camy + 500
  295.     angle = angle + 5
  296.     rotate camx + 350, camy + 450, angle
  297.  
  298.     makeobs camx + 500, camy + 400
  299.     makeobs camx + 800, camy + 400
  300.  
  301.     IF y > 600 THEN GOTO lvl5
  302.  
  303.     _DISPLAY
  304.     _LIMIT 60
  305.  
  306.  
  307.  
  308. SUB makechar
  309. 'CIRCLE (x, y), 20, _RGB32(100, 150, 255)
  310. 'PAINT (x, y), _RGB32(100, 150, 255), _RGB32(100, 150, 255)
  311. LINE (x, y)-STEP(20, 20), _RGB32(100, 150, 255), BF
  312.  
  313. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  314. IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  315.     collision% = 0
  316.     collision% = 1
  317.  
  318. SUB makeobs (bx, by)
  319. LINE (bx, by)-STEP(200, 50), _RGB32(255, 150, 100), BF
  320. IF collision%(x, y, 20, 20, bx, by, 200, 50) THEN
  321.     y = oldy
  322.     gravity = 0
  323.     y_change = 0
  324.     oldy = y
  325.     gravity = 9.8
  326.  
  327. SUB makesobs (bx, by, w, h)
  328. LINE (bx, by)-STEP(w, h), _RGB32(255, 150, 100), BF
  329. IF collision%(x, y, 20, 20, bx, by, w, h) THEN
  330.     y = oldy
  331.     gravity = 0
  332.     y_change = 0
  333.     oldy = y
  334.     gravity = 9.8
  335.  
  336. SUB makeredobs (bx, by)
  337. LINE (bx, by)-STEP(200, 50), _RGB32(255, 0, 0), BF
  338. IF collision(x, y, 20, 20, bx, by, 200, 50) THEN
  339.     CLS
  340.     _PRINTSTRING (300, 300), "GAME OVER!"
  341.     _DISPLAY
  342.     _DELAY 2
  343.     game
  344. SUB rotate (px, py, deg)
  345. px2 = px + COS(_D2R(deg)) * 100
  346. py2 = py + SIN(_D2R(deg)) * 100
  347.  
  348. CIRCLE (px2, py2), 10, _RGB32(255, 0, 0)
  349. PAINT (px2, py2), _RGB32(255, 0, 0), _RGB32(255, 0, 0)
  350.  
  351. LINE (px, py)-(px2, py2), _RGB32(0, 0, 0)
  352.  
  353. IF collision(x, y, 20, 20, px2, py2, 10, 10) THEN
  354.     CLS
  355.     _PRINTSTRING (300, 300), "GAME OVER!"
  356.     _DISPLAY
  357.     _DELAY 2
  358.     game
  359.  
  360. FUNCTION mouseclick (x1, y1, x2, y2)
  361. mx = _MOUSEX
  362. my = _MOUSEY
  363. mb = _MOUSEBUTTON(1)
  364.  
  365. IF mx >= x1 AND mx <= x2 AND my >= y1 AND my <= y2 AND mb THEN
  366.     mouseclick = 1
In the 5th level, why is the second obstacle not blocking the character?
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Why is it not blocking?
« Reply #1 on: April 14, 2019, 05:46:19 am »
well
Idea is not bad but you simply must slow down moving of player object.
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why is it not blocking?
« Reply #2 on: April 14, 2019, 09:56:50 am »
Scanning level 5 code:
Code: QB64: [Select]
  1. lvl5:
  2. level = 5
  3. x = 200
  4. y = 100
  5. camx = 0
  6. camy = 0
  7.     CLS
  8.     makechar
  9.  
  10.     IF _KEYDOWN(19200) THEN
  11.         x = x - 10
  12.         camx = camx + 5
  13.     END IF
  14.     IF _KEYDOWN(19712) THEN
  15.         x = x + 10
  16.         camx = camx - 5
  17.     END IF
  18.     IF _KEYDOWN(18432) THEN
  19.         jump_i = 1
  20.     END IF
  21.  
  22.  
  23.     IF jump_i = 1 AND y > 100 THEN
  24.         y = y - 20
  25.         camy = camy - 5
  26.         cycle = cycle + 1
  27.         IF cycle > 20 THEN cycle = 0: jump_i = -1
  28.     END IF
  29.  
  30.     y = y + gravity
  31.     camy = camy + 2
  32.  
  33.     IF camy > 50 THEN camy = 50
  34.  
  35.     makeobs camx + 50, camy + 500
  36.     angle = angle + 5
  37.     rotate camx + 350, camy + 450, angle
  38.  
  39.     makeobs camx + 500, camy + 400
  40.     makeobs camx + 800, camy + 400   '<<<<<<<<<<<<<<<<<<<<<< x coord looks to be farther than screen x width = 800
  41.  
  42.     IF y > 600 THEN GOTO lvl5
  43.  
  44.     _DISPLAY
  45.     _LIMIT 60
  46.  

I have commented a line where your makeobs camx looks too far right of screen width.  But I don't see collision check in level 5 either.
« Last Edit: April 14, 2019, 10:03:28 am by bplus »