Author Topic: Game for JRS  (Read 2918 times)

0 Members and 1 Guest are viewing this topic.

Peter

  • Guest
Game for JRS
« on: March 20, 2013, 02:43:34 PM »
deleted
« Last Edit: April 14, 2015, 04:23:30 AM by Peter »

JRS

  • Guest
Re: Game for JRS
« Reply #1 on: March 20, 2013, 05:38:34 PM »
I finally got this working. Peter got a little sloppy and O2 didn't care.   :-\

Change List
  • The Sub MoveBloc() is terminated with a End Function.
  • In ScriptBasic you can't call an array element as a function and expect a correct result. pz = Buffer[pa]
  • I guess O2 returns a default of zero for a sys return type if you bail out of a function without assigning a function return value. (see my repairs to the Buffer function)
  • The CheatCode option doesn't do anything of value for me so I removed it.




Code: [Select]
' Magic24 by Peter Wirbelauer

INCLUDE "sbsw.inc"

SPLITA STRING(64,"0") BY "" TO Map
SPLITA STRING(26,"0") BY "" TO Buf
num = 0
xm = 0
ym = 0
ibx = 0
iax = 0
Ras = 0
bDir = 0
x1 = 0
y1 = 0
z1 = 0
a = 0
move = 0

'   ********
SUB MoveBloc
'   ********
  IF Ras = 0 THEN EXIT SUB
    IF bDir = 1 THEN
      SW_DrawBmp num, x1, y1, 64, 64, iax
      x1 += 2
      z1 += 2
      IF z1 = 64 THEN
        Ras = 0
        z1 = 0
        x1 = x1 / 64
        y1 = y1 / 64
        ibx = y1 * 5 + x1
        Map[ibx] = iax
      END IF
    ELSE IF bDir = 2 THEN
      SW_DrawBmp num, x1, y1, 64, 64, iax
      x1 -= 2
      z1 += 2
      IF z1 = 64 THEN
        Ras = 0
        z1 = 0
        x1 = x1 / 64
        y1 = y1 / 64
        ibx = y1 * 5 + x1
        Map[ibx] = iax
      END IF
    ELSEIF bDir = 3 THEN
      SW_DrawBmp num, x1, y1, 64, 64, iax
      y1 -= 2
      z1 += 2
      IF z1 = 64 THEN
        Ras = 0
        z1 = 0
        x1 = x1 / 64
        y1 = y1 / 64
        ibx = y1 * 5 + x1
        Map[ibx] = iax
      END IF
    ELSE IF bDir = 4 THEN
      SW_DrawBmp num, x1, y1, 64, 64, iax
      y1 += 2
      z1 += 2
      IF z1 = 64 THEN
        Ras = 0
        z1 =0
        x1 = x1 / 64
        y1 = y1 / 64
        ibx = y1 * 5 + x1
        Map[ibx] = iax
      END IF
    END IF
END SUB

'   ******
SUB IsBloc
'   ******
  IF Ras > 0 THEN EXIT SUB
  xm = FIX(SW_xMouse() / 64)
  ym = FIX(SW_yMouse() / 64)
  ibx = ym * 5 + xm
  IF SW_MouseButton() = 1 THEN
    iax = Map[ibx]
    IF iax = 0 THEN EXIT SUB
    IF Map[ibx+1] = 0 THEN
      Ras = 1
      bDir = 1
      Map[ibx] = 0
      x1 = xm * 64
      y1 = ym * 64
      SW_PlayWav w2
      move += 1
    ELSE IF Map[ibx-1] = 0 AND xm > 0 THEN
      Ras = 1
      bDir = 2
      Map[ibx] = 0
      x1 = xm * 64
      y1 = ym * 64
      SW_PlayWav w2
      move += 1
    ELSE IF Map[ibx - 5] = 0 AND ym > 0 THEN
      Ras = 1
      bDir = 3
      Map[ibx] = 0
      x1 = xm * 64
      y1 = ym * 64
      SW_PlayWav w2
      move += 1
    ELSE IF Map[ibx+5] = 0 THEN
      Ras = 1
      bDir = 4
      Map[ibx] = 0
      x1 = xm * 64
      y1 = ym * 64
      SW_PlayWav w2
      move += 1
    END IF
  END IF
END SUB

'   *******
SUB ShowMap
'   *******
  LOCAL px, py, pz, pa
  px = 0
  py = 0
  pz = 0
  pa = 0
  FOR py = 0 TO 4
    FOR px = 0 TO 4
      pa = py * 5 + px
      pz = Map[pa]
      SW_DrawBmp num, px * 64, py * 64, 64, 64, pz
    NEXT
  NEXT
END SUB

'        ******
FUNCTION Buffer(rd)
'        ******
  LOCAL ix
  ix = 0
  WHILE ix < 24
    IF Buf[ix] = rd THEN
      Buffer = 1
      EXIT FUNCTION
    END IF
    ix += 1
  WEND
  ix = 0
  WHILE ix < 24
    IF Buf[ix] = 0 THEN
      Buf[ix] = rd
      Buffer = 0
      EXIT FUNCTION
    END IF
    ix += 1
  WEND
  Buffer = 0
END FUNCTION

'   *****
SUB Start
'   *****
  LOCAL pa, px, pz
  pa = 0
  px = 0
  pz = 0
  FOR pa = 0 TO 64
    Map[pa] = 255
  NEXT
  move = 0
  Ras = 0
  z1 = 0
  a = 0
  FOR pa = 0 TO 24
    Buf[pa] = 0
    Map[pa] = 0
  NEXT
  WHILE px < 24
    pa = SW_Rnd(1,24)
    pz = Buffer(pa)
    IF pz <> 1 THEN
      Map[px] = pa
      px += 1
    END IF
  WEND
END SUB

'   *****
SUB Ready
'   *****
  LOCAL ix
  IF a = 1 THEN EXIT SUB
  FOR ix = 0 TO 23
    IF Map[ix] <> ix + 1 THEN EXIT SUB
  NEXT
  SW_PlayWav w1
  a = 1
END SUB


' ******
' MAIN *
' ******

SW_Window 320, 350, 1
SW_SetFPS 60
SW_SetCaption "ScriptBasic Magic 24"
SW_SetFont 16, 38, bold, ""
num = SW_LoadBmp("media/blocks.bmp", 25)
w1 = "media/o23.wav"
w2 = "media/clic.wav"
Start
WHILE SW_Key(27) = 0
  SW_Cls 0
  ShowMap
  IsBloc
  MoveBloc
  Ready
  IF a = 1 THEN
    SW_SetText 16, 100, "    YOU GOT IT ", SW_RGB(255,255,255)
    SW_SetText 16, 140, "  PRESS SPACE BAR", SW_RGB(255,255,255)
    IF SW_Key(32) THEN Start()
  END IF
  SW_SetText 100, 320, "MOVES " & move, SW_RGB(255,255,255)
  SW_Sync
WEND
SW_Quit
« Last Edit: March 20, 2013, 07:14:30 PM by JRS »

JRS

  • Guest
Re: Game for JRS
« Reply #2 on: March 20, 2013, 07:33:53 PM »
You have to be careful about SB expecting a valid value and you not coming through.

Code: [Select]
FUNCTION test(arg)
' not doing anything
END FUNCTION

a = 0
a = test(5)
PRINT a,"\n"
IF a = 0 THEN PRINT "This should never print\n"
IF a <> 0 THEN PRINT "undef IS <> 0 in SB\n"

C:\SB22\test>scriba noreturn.sb
undef
undef IS <> 0 in SB

C:\SB22\test>

JRS

  • Guest
Re: Game for JRS
« Reply #3 on: March 21, 2013, 07:01:42 AM »
Quote
End Sub and End Function could  be replaced  with " END"

PLEASE don't do this as END (and STOP) are SB keywords. I already have to deal with RETURN being bipolar with GOSUB & FUNCTION. SB is pretty relaxed about not requiring you make the assignment for a function and treat it like a SUB. SB actually didn't complain about the END FUNCTION with a SUB and it was my observation looking at the code. The BIG thing that cost me was the Buffer[] being used for a function call. Keep in mind that SB arrays are dynamic and you can reference any dimension and it will return an undef rather than an error.

Thanks for the PONG game and I'll get it converter to SB today. Keep those games coming as it helps SB attract more users and realize SB isn't just for web scripting like I've heard over and over by the big A.

Charles Pegge

  • Guest
Re: Game for JRS
« Reply #4 on: March 21, 2013, 11:55:15 AM »

SUBs create a lot of disambiguation work for the compiler, since it is also an assembly code instruction (subtract). I would much prefer to ditch it and use only function..end function and method..end method..

END on its own, is currently reserved for program termination.

You can also use curly braces if it helps your fingers. :)

function curly(sys a,b,c)
{
}



JRS

  • Guest
Re: Game for JRS
« Reply #5 on: March 21, 2013, 12:26:05 PM »
If its legacy disposal day, take out GOSUB as well and leave RETURN as a FUNCTION only directive.

Can you explain the difference between FUNCTION & METHOD?


Charles Pegge

  • Guest
Re: Game for JRS
« Reply #6 on: March 21, 2013, 12:41:51 PM »
In OxygenBasic, gosub is terminated by ret. Return is used for sub, function and method. It is occasionally useful when porting code.

methods are only used inside classes, and have a hidden 1st parameter: the object pointer.

Peter Verhas uses OOP techniques in the C source of ScriptBasic. The interesting thing is, he has swapped the object pointer and function table pointer (comparing with C++ and COM conventions).

Usually the 1st entry in an object is a pointer to the function table.

Charles

JRS

  • Guest
Re: Game for JRS
« Reply #7 on: March 21, 2013, 02:08:19 PM »
I think Peter formulated his OOP (BOM) before COM existed. I don't know of many languages of a OOP nature written in ANSI/ISO C. The gain for this pain is portability. SB was 10 years before its time. IMHO

 
« Last Edit: March 21, 2013, 02:43:43 PM by JRS »

JRS

  • Guest
Re: Game for JRS
« Reply #8 on: March 21, 2013, 04:22:10 PM »
FYI - SUB & FUNCTION is interchangeable in ScriptBasic.

Code: [Select]
SUB test(arg)
  test = arg +1
END SUB

PRINT test(5),"\n"

jrs@laptop:~/sb/sb22/test$ scriba subfunc.sb
6
jrs@laptop:~/sb/sb22/test$

JRS

  • Guest
Re: Game for JRS
« Reply #9 on: March 21, 2013, 07:26:49 PM »


Code: OxygenBasic
  1. ' ScriptBasic PONG by Peter Wirbelauer
  2.  
  3. INCLUDE "sbsw.inc"
  4.  
  5. SW_Window 320, 240, 1
  6. SW_SetCaption "ScriptBasic Pong"
  7.  
  8. SW_SetFPS 60
  9.  
  10. x = 50
  11. y = 50
  12. x2 = 130
  13. y2 = 150
  14. pspeed = 2
  15. xadj = 2
  16. yadj = 2
  17. delay = 1
  18. score = 0
  19.  
  20. haha = "wav/haha.wav"
  21. pong = "wav/pong.wav"
  22.  
  23. WHILE SW_Key(27) = 0
  24.   SW_Cls SW_RGB(200, 200, 247)
  25.   SW_FillCircle x, y, 8, SW_RGB(0, 0, 255)
  26.   SW_FillBox x2, y2, 30, 4, SW_RGB(255, 255, 255)
  27.   SW_Box 20, 20, 280, 160, 4, SW_RGB(255, 255, 0)
  28.   SW_SetText 10, 0, "SCORE:" & score, SW_RGB(255, 0, 0)
  29.   IF y <= 20 THEN
  30.     yadj = 1
  31.     SW_PlayWav pong
  32.   END IF
  33.   IF y >= 180 THEN
  34.     yadj = -1
  35.     SW_PlayWav pong
  36.   END IF
  37.   IF x >= 300 THEN
  38.     xadj = -1
  39.     SW_PlayWav pong
  40.   END IF
  41.   IF x <= 20 THEN
  42.     xadj = 1
  43.     SW_PlayWav pong
  44.   END IF
  45.   IF SW_Key(37) AND x2 > 18 THEN x2 = x2 - pspeed
  46.   IF SW_Key(39) AND x2 < 270 THEN x2 = x2 + pspeed
  47.   x += xadj
  48.   y += yadj
  49.   IF y > y2 - 7 AND y2 < y2 + 2 AND x < x2 + 30 AND x > x2 THEN
  50.     yadj = -1
  51.     score += 1
  52.   END IF
  53.   IF y > y2 + 10 THEN
  54.     SW_SetText 100, 200, "GAME OVER!", SW_RGB(0, 160, 55)
  55.     SW_Playwav haha
  56.     SW_Waitkey
  57.     score = 0
  58.     x = 50
  59.     y = 50
  60.     x2 = 130
  61.     y2 = 150
  62.   END IF
  63.   SW_Sync
  64. WEND
  65. SW_CloseWindow
  66.  


X