Author Topic: 15 days for Halloween code challenge  (Read 9691 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: 15 days for Halloween code challenge
« Reply #30 on: October 31, 2018, 08:16:15 pm »
Hi Guys  here my code
be patient downloading ZIP.

Your goal is to give sweet to not get the treat (spider)

Code: QB64: [Select]
  1. 'Music downloaded from  [No Copyright Music] Halloween - (Royalty Free Halloween Music)
  2. ' Images taken from Images downloaded from www.freepik.com
  3. '
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5. _TITLE "Halloween Challenge"
  6. _SCREENMOVE 200, 20
  7.  
  8. DIM a AS LONG, b AS LONG, c AS LONG, d AS LONG, e(1 TO 9) AS LONG, f AS LONG, f2 AS LONG, g AS INTEGER
  9. DIM RightGo AS _BYTE, LeftGo AS _BYTE, FireActive AS _BYTE, ChangeMusic AS _BYTE, OffMusic AS _BYTE
  10. DIM Xpump(1 TO 9) AS INTEGER, Ypump(1 TO 9) AS INTEGER, xShip AS INTEGER, xFire AS INTEGER, yFire AS INTEGER
  11. DIM Fpump(1 TO 9) AS _BYTE, Lpump(1 TO 9) AS INTEGER, FireActiveP AS _BYTE, xFireP(1 TO 9) AS INTEGER, yFireP(1 TO 9) AS INTEGER
  12. DIM VelP AS INTEGER, GoDown AS _BYTE, path AS STRING, files$, cmd AS INTEGER, GameOver AS INTEGER
  13. DIM SHARED mus(1 TO 4) AS LONG, snd(1 TO 4) AS LONG, ActualMus AS LONG, IndexMus AS INTEGER
  14.  
  15. CONST RedBack = _RGB(210, 0, 0, 32), Color1 = _RGB(255, 255, 255, 32), Color2 = _RGB(255, 177, 0, 32), Color3 = _RGB(255, 150, 80, 32)
  16. CONST Color4 = _RGB(255, 100, 0, 32), Color5 = _RGB(255, 55, 0, 32), Color6 = _RGB(100, 130, 20, 32)
  17. CONST FireS = 4, FireP = 3, DeathS = 2, DeathP = 1
  18. CONST False = 0, True = -1, Quit = 999, Fire = 100, FireVel = 10, Right = 150, Left = 200, yShip = 500
  19. CONST CMusic = 250, OMusic = 300, widS = 100, higS = 100
  20.  
  21. ' inizialize fonts----------------------------------------------------------
  22. path$ = "C:\Windows\Fonts\"
  23. files$ = "Tahoma.ttf" '  "lucon.ttf"  <-- Alternative fonts of system
  24. loadfont path$ + files$, f, 68
  25. files$ = "seguihis.ttf"
  26. loadfont path$ + files$, f2, 36
  27.  
  28. ' initialize music and sounds------------------------------------------------
  29. loadMusic
  30.  
  31. ' initialize images----------------------------------------------------------
  32. a = _LOADIMAGE("assets_H\moon.jpg") 'BACKGROUND
  33. IF a >= -1 THEN PRINT "Image not loaded moon"
  34. b = _LOADIMAGE("assets_H\ship_.png") 'USER
  35. IF b >= -1 THEN PRINT "Image not loaded ship"
  36. c = _LOADIMAGE("assets_H\sweet.png") 'FIRE OF USER
  37. IF c >= -1 THEN PRINT "Image not loaded sweet"
  38. d = _LOADIMAGE("assets_H\spider.png") ' FIRE OF PUMPKINS
  39. IF d >= -1 THEN PRINT "Image not loaded spider"
  40. ' PUMPKINS
  41. FOR g = 1 TO 9 STEP 1
  42.     e(g) = _LOADIMAGE("assets_H\pum" + LTRIM$(STR$(g)) + ".png") ' FIRE OF PUMPKINS
  43.     IF e(g) >= -1 THEN PRINT "Image not loaded pum" + LTRIM$(STR$(g)) + ".png", e(g)
  44.     Xpump(g) = 80 * g: Ypump(g) = 70
  45.     Lpump(g) = True 'life
  46.     Fpump(g) = False 'firing
  47. VelP = 5 ' pumpkins go towards right
  48.  
  49. ' setting for starting------------------------------------------------------------
  50. _PUTIMAGE (1, 1)-(800, 600), a
  51. Credits
  52. IntroStart f, f2
  53. xShip = 200
  54. GoDown = False
  55. GameOver = 0
  56. m = _NEWIMAGE(800, 600, 32)
  57. 'main loop
  58.  
  59.     cmd = 0 ' clear buffer commands
  60.     '---------------------------INPUT MANAGEMENT--------------------------------
  61.     ' take commands
  62.     IF _KEYDOWN(32) AND FireActive = 0 THEN cmd = Fire
  63.     IF _KEYDOWN(27) THEN cmd = quits
  64.     IF _KEYDOWN(19200) THEN cmd = Right
  65.     IF _KEYDOWN(19712) THEN cmd = Left
  66.     IF _KEYDOWN(77) OR _KEYDOWN(109) THEN cmd = CMusic
  67.     IF _KEYDOWN(13) AND (_KEYDOWN(77) OR _KEYDOWN(109)) THEN cmd = OMusic
  68.  
  69.     '---------------------------COMMAND TRANSLATOR------------------------------
  70.     ' command Fire
  71.     IF cmd = Fire THEN
  72.         FireActive = True
  73.         yFire = yShip
  74.     END IF
  75.     ' command go right
  76.     IF cmd = Right THEN RightGo = True ELSE RightGo = False
  77.     ' command go left
  78.     IF cmd = Left THEN LeftGo = True ELSE LeftGo = False
  79.     ' command change music
  80.     IF OffMusic = False AND cmd = CMusic THEN ChangeMusic = True
  81.     ' command off music
  82.     IF cmd = OMusic THEN OffMusic = NOT OffMusic
  83.  
  84.  
  85.     '-------------------------EVENT MANAGEMENT-----------------------------------
  86.  
  87.     ' event Fire  @----------------------------------------------
  88.     IF FireActive THEN
  89.         IF yFire = yShip THEN _SNDPLAY snd(FireS): xFire = xShip + INT(widS / 2) - 20
  90.         IF yFire - FireVel > 0 THEN
  91.             yFire = yFire - FireVel
  92.         ELSE
  93.             yFire = 0
  94.             FireActive = False
  95.         END IF
  96.     END IF
  97.  
  98.     ' event go to right  @----------------------------------------------
  99.     IF RightGo THEN
  100.         IF xShip - 10 < 1 THEN xShip = 1 ELSE xShip = xShip - 10
  101.         RightGo = False
  102.     END IF
  103.  
  104.     ' event go to left  @----------------------------------------------
  105.     IF LeftGo THEN
  106.         IF xShip + 10 + widS > 800 THEN xShip = 800 - widS ELSE xShip = xShip + 10
  107.         LeftGo = False
  108.     END IF
  109.  
  110.  
  111.     ' event  change music   @----------------------------------------------
  112.     IF ChangeMusic = True THEN
  113.         IF IndexMus = 4 THEN IndexMus = 1 ELSE IndexMus = IndexMus + 1
  114.         _SNDSTOP ActualMus
  115.         ActualMus = mus(IndexMus)
  116.         _SNDPLAY ActualMus
  117.         ChangeMusic = False
  118.     END IF
  119.  
  120.     ' event Off music  @----------------------------------------------
  121.     IF OffMusic = True THEN _SNDSTOP ActualMus
  122.  
  123.     ' event end music on playing  @----------------------------------------------
  124.     IF _SNDPLAYING(ActualMus) = False THEN _SNDPLAY ActualMus
  125.  
  126.     ' event Move Pumpkins  @----------------------------------------------
  127.     IF VelP > 0 THEN ' moving to right
  128.         FOR j = 9 TO 1 STEP -1
  129.             IF Lpump(j) = True THEN EXIT FOR
  130.         NEXT
  131.         IF Xpump(j) + VelP + 60 > 800 THEN
  132.             VelP = VelP * -1 ' it changes direction
  133.             GoDown = True ' pumpkins go towards ship
  134.         ELSE
  135.             GoDown = False
  136.         END IF
  137.     ELSE 'moving to left
  138.         FOR j = 1 TO 9 STEP 1
  139.             IF Lpump(j) = True THEN EXIT FOR
  140.         NEXT
  141.         IF Xpump(j) + VelP < 0 THEN
  142.             VelP = VelP * -1 ' it changes direction
  143.             GoDown = True ' pumpkins go towards ship
  144.         ELSE
  145.             GoDown = False
  146.         END IF
  147.     END IF
  148.     ' it calculates the new position for pumpkins
  149.     FOR k = 1 TO 9 STEP 1
  150.         IF Lpump(k) = True THEN
  151.             Xpump(k) = Xpump(k) + VelP
  152.             IF GoDown = True THEN Ypump(k) = Ypump(k) + 10
  153.         END IF
  154.     NEXT
  155.  
  156.     ' event Fire Pumpkins  @----------------------------------------------
  157.     FOR h = 1 TO 9 STEP 1
  158.         IF Lpump(h) = True THEN ' if pumpkin exists
  159.             IF (INT(RND * 3) + 1) > 2 THEN
  160.                 IF Fpump(h) = False THEN 'if pumpkin is not firing
  161.                     Fpump(h) = True ' it starts to fire
  162.                     yFireP(h) = Ypump(h)
  163.                 END IF
  164.             END IF
  165.         END IF
  166.  
  167.         ' event firing pumpkins       @----------------------------------------------
  168.         IF Fpump(h) = True THEN
  169.             IF yFireP(h) = Ypump(h) THEN _SNDPLAY snd(FireP): xFireP(h) = Xpump(h) + INT(widS / 2) - 20
  170.             IF yFireP(h) + FireVel < 600 THEN
  171.                 yFireP(h) = yFireP(h) + FireVel
  172.             ELSE
  173.                 yFireP(h) = 540
  174.                 Fpump(h) = False
  175.             END IF
  176.         END IF
  177.         ' event destroy ship       @----------------------------------------------
  178.         IF (xFireP(h) > xShip AND xFireP(h) < xShip + widS) OR (xFireP(h) + 40 > xShip AND xFireP(h) + 40 < xShip + widS) THEN
  179.             IF (yFireP(h) > yShip AND yFireP(h) < yShip + higS) OR (yFireP(h) + 20 > yShip AND yFireP(h) + 20 < yShip + higS) THEN
  180.                 _SNDPLAY snd(2)
  181.                 cmd = Quit
  182.             END IF
  183.         END IF
  184.         ' event destroy fireship   @----------------------------------------------
  185.         IF (xFireP(h) >= xFire AND xFireP(h) <= xFire + 40) OR (xFireP(h) + 40 >= xFire AND xFireP(h) + 40 <= xFire + 40) THEN
  186.             IF (yFireP(h) >= yFire AND yFireP(h) <= yFire + 20) OR (yFireP(h) + 20 >= yFire AND yFireP(h) + 20 <= yFire + 20) THEN
  187.                 _SNDPLAY snd(1)
  188.                 Fpump(h) = False
  189.                 FireActive = False
  190.             END IF
  191.         END IF
  192.  
  193.     NEXT
  194.  
  195.     ' event destroy pumpkin @----------------------------------------------
  196.     IF FireActive THEN
  197.         FOR l = 1 TO 9 STEP 1
  198.             IF Lpump(l) = True THEN ' pumpkins is alive
  199.                 IF (xFire > Xpump(l)) AND (Xpump(l) + 60 > xFire) OR (xFire + 40 > Xpump(l)) AND (Xpump(l) + 60 > xFire + 40) THEN
  200.                     IF (yFire < Ypump(l) + 60) AND (yFire > Ypump(l)) OR (yFire + 20 < Ypump(l) + 60) AND (yFire + 20 > Ypump(l)) THEN
  201.                         Lpump(l) = False
  202.                         FireActive = False
  203.                         GameOver = GameOver + 1
  204.                         _SNDPLAY snd(1)
  205.                     END IF
  206.                 END IF
  207.             END IF
  208.         NEXT
  209.     END IF
  210.  
  211.  
  212.     '-----------------------------------------OUTPUT --------------------
  213.     ' all graphic operations go onto hidden panel m
  214.     _PUTIMAGE (1, 1)-(800, 600), a, m 'background layer
  215.     _PUTIMAGE (xShip, yShip)-(xShip + widS, yShip + higS), b, m ' astroship layer
  216.     IF FireActive THEN _PUTIMAGE (xFire, yFire)-(xFire + 40, yFire + 20), c, m ' FireShip
  217.     FOR i = 1 TO 9 STEP 1
  218.         IF Lpump(i) THEN _PUTIMAGE (Xpump(i), Ypump(i))-(Xpump(i) + 60, Ypump(i) + 60), e(i), m
  219.         IF Fpump(i) THEN _PUTIMAGE (xFireP(i), yFireP(i))-(xFireP(i) + 40, yFireP(i) + 20), d, m
  220.     NEXT
  221.     _PUTIMAGE , m, 0 'copy panel m to screen
  222.     _LIMIT 10
  223.  
  224.     'debug
  225.  
  226.  
  227. LOOP UNTIL GameOver = 9 OR cmd = Quit ' events GameOver and Quit
  228. IF GameOver = 9 THEN
  229.     _SNDPLAY snd(1)
  230.     CLS
  231.     PRINT "YOU WIN"
  232.     PRINT "GAME OVER"
  233.     SLEEP 5
  234. IF cmd = Quit THEN
  235.     _SNDPLAY snd(2)
  236.     CLS
  237.     PRINT "YOU LOOSE"
  238.     PRINT "GAME OVER"
  239.     SLEEP 5
  240.  
  241.  
  242. SUB IntroStart (fn1&, fn2&)
  243.     DIM cont%
  244.     FOR cont% = 1 TO 2 STEP 1
  245.         IF cont% = 1 THEN
  246.             CLS
  247.             IF fn2& > 0 THEN _FONT fn2& ELSE PRINT "No file font loaded" + " seguihis.ttf"
  248.             COLOR Color6
  249.             _PRINTSTRING (260, 200), "Press a key to start"
  250.         ELSE
  251.             CLS , RedBack
  252.             _FONT fn1&
  253.             COLOR Color1
  254.             _PRINTSTRING (30, 120), "INSTRUCTIONS:"
  255.             _PRINTSTRING (30, 200), "space = fire"
  256.             _PRINTSTRING (30, 250), "arrows left/right = move"
  257.             _PRINTSTRING (30, 300), "M = change music"
  258.             _PRINTSTRING (30, 350), "Enter+M =Off/On music"
  259.             _PRINTSTRING (30, 410), "ESC to quit"
  260.         END IF
  261.         IF fn1& > 0 THEN _FONT fn1& ELSE PRINT "No file font loaded" + " Tahoma.ttf"
  262.         COLOR Color1
  263.         _PRINTSTRING (2, 20), "HALLOWEEN CHALLENGE", 0
  264.         COLOR Color2
  265.         _PRINTSTRING (6, 28), "HALLOWEEN CHALLENGE", 0
  266.         COLOR Color3
  267.         _PRINTSTRING (10, 36), "HALLOWEEN CHALLENGE", 0
  268.         COLOR Color4
  269.         _PRINTSTRING (14, 40), "HALLOWEEN CHALLENGE", 0
  270.         COLOR Color5
  271.         _PRINTSTRING (18, 44), "HALLOWEEN CHALLENGE", 0
  272.         IF cont% = 1 THEN SLEEP ELSE SLEEP 4
  273.     NEXT cont%
  274.  
  275. SUB loadfont (pat$, h&, size)
  276.     IF _FILEEXISTS(pat$) THEN
  277.         h& = _LOADFONT(pat$, size, "BOLD")
  278.     ELSE
  279.         PRINT "File font not found"
  280.     END IF
  281.  
  282. SUB Credits
  283.     DIM cp AS STRING
  284.     COLOR Color4
  285.     _FONT 16
  286.     PRINT "CREDITS:"
  287.     cp = " MUSIC: downloaded from  [No Copyright Music] Halloween - (Royalty Free Halloween Music)" + CHR$(13) + "  on www.youtube.com"
  288.     LOCATE 2, 2
  289.     PRINT cp
  290.     cp = " IMAGES: downloaded from freepik.com created: Pumpkin and Starship by Freepik, Moon by Kjpargeter,  Sweet and spider by Titusurya"
  291.     PRINT cp
  292.     LOCATE 12, 2
  293.     PRINT "PRESS A KEY TO CONTINE..."
  294.     SLEEP
  295.     CLS
  296.  
  297. SUB loadMusic
  298.     SHARED OffMusic AS _BYTE
  299.     DIM a AS INTEGER
  300.     CLS
  301.     LOCATE 2, 2
  302.     PRINT "LOADING MUSIC....."
  303.     mus(1) = _SNDOPEN("assets_H\UnknownFear.mp3")
  304.     mus(2) = _SNDOPEN("assets_H\Halloween.mp3")
  305.     mus(3) = _SNDOPEN("assets_H\WonderAndMagic.mp3")
  306.     mus(4) = _SNDOPEN("assets_H\Spook.mp3")
  307.     FOR a = 1 TO 4 STEP 1
  308.         IF mus(a) = 0 THEN PRINT "No music " + STR$(a) + " found!"
  309.     NEXT
  310.     _SNDPLAY mus(a - 1) ' it plays the last music loaded
  311.     ActualMus = mus(a - 1)
  312.     OffMusic = False
  313.     PRINT
  314.     PRINT "LOADING SOUND....."
  315.     snd(1) = _SNDOPEN("assets_H\DeathPump.mp3")
  316.     snd(2) = _SNDOPEN("assets_H\DeathShip.mp3")
  317.     snd(3) = _SNDOPEN("assets_H\FirePump.mp3")
  318.     snd(4) = _SNDOPEN("assets_H\FireShip.mp3")
  319.     FOR a = 1 TO 4 STEP 1
  320.         IF snd(a) = 0 THEN PRINT "No music " + STR$(a) + " found!"
  321.     NEXT
  322.     SLEEP 2
  323.     CLS
  324.  
  325. FUNCTION quits
  326.     DIM a$
  327.     COLOR , 0
  328.     _PRINTSTRING (200, 200), "Do you Quit?"
  329.     _PRINTSTRING (300, 260), "Y/N"
  330.     DO
  331.         a$ = LCASE$(INPUT$(1))
  332.     LOOP UNTIL a$ = "y" OR a$ = "n"
  333.     IF a$ = "y" THEN quits = Quit ELSE quits = False

Thanks to try
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: 15 days for Halloween code challenge
« Reply #31 on: October 31, 2018, 08:21:35 pm »
If you find too huge .ZIP here 2 screenshots


About midnight... someoneelse can post somethingelse before his/her midnight... it can be...
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: 15 days for Halloween code challenge
« Reply #32 on: October 31, 2018, 08:56:34 pm »
@Bplus

Fine your rotating sphere  :-)
Programming isn't difficult, only it's  consuming time and coffee

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: 15 days for Halloween code challenge
« Reply #33 on: October 31, 2018, 09:04:03 pm »
ROFL. It went from a basketball to a sphere... anything but a pumpkin!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/