What is wrong with this code?

Just starting out? Need help? Post your questions and find answers here.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: What is wrong with this code?

Post by IdeasVacuum »

Hi J@ckWhiteIII (are there 3 of you?)
....If you compile the code I have uploaded (without any changes :mrgreen: - use the sprites I have uploaded), it should be absolutely fine on your machine. The ElseIfs are a red herring, they will not affect the app one way or another since they do not do anything. As Kenmo has explained, the hardware is fine too.

The code is not organised the way it would be for an 'office app', in fact to me it looks 'old fashioned' using the Gosubs (I didn't even know PB had them! :oops: ). However, it is perfectly logical, and with the code indentation done it is much easier to understand.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: What is wrong with this code?

Post by IdeasVacuum »

.... restructuring the code will make it easier to expand, so that's a good idea. Will be interesting to see if Procedures can replace Gosubs without a performance penalty.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: What is wrong with this code?

Post by J@ckWhiteIII »

Hello IdeasVacuum,
I don't think there are 3 of me, but the III stand for Jack White's (a guitarist) favourite number ^^

Gonna try for code with the sprites now :D

If I click anywhere on the screen, the program automatically jumps to the NewGame procedure (using them now). I wonder how that is possible o.O

Performance Penalty: Well, the first "hit" lags a bit xD but I don't really see any other performance penalties

Code: Select all

InitSprite ()
InitKeyboard ()
InitSound ()
InitMouse ()

Global timer.i = 0
Global musix.i = 100
Global effects.i = 100
Global musixtate.i = 1
Global effectsstate.i = 1
Global spriteloader.i = 0
Global Number.i = 1
Global ScreenOpen.i = 0
Global screenx.i = 640
Global screeny.i = 480
Global circlex.i = 200        
Global Black.i = RGB (0,0,0)
Global White.i = RGB (255,255,255)
Global Font1Colour.i = White
Global Font1ColourBk.i = Black
Global Font2Colour.i =White
Global Font2ColourBk.i = Black
Global Font3Colour.i = White
Global Font2ColourBk.i = Black
Global Font4Colour.i = White
Global Font4ColourBk.i = Black
Global Font5Colour.i = White
Global Font5ColourBk.i = Black
Global circley.i = Random (480)
Global move.i = 1
Global move2.i = 1
Global player1x.i = 0
Global player1y.i = 200
Global player2x.i = 635
Global player2y.i = 200
Global ticker = Random (3)
Global  zieg.s = "C:\Program Files\ProgMaster\Pong\"

 If ticker = 1
   move * -1
     ElseIf ticker = 3
       move2 * -1
     ElseIf ticker = 0
       move * 1
     ElseIf ticker = 2
       move2 * 1
 EndIf
        
        
Procedure Ending ()
CloseScreen ()
End
EndProcedure
  
Procedure Controls ()
ending ()
EndProcedure

Procedure Videosettings ()
ending ()
EndProcedure


Procedure Options ()
  Repeat
    ClearScreen (0)
    ExamineKeyboard ()
    ExamineMouse ()
      StartDrawing (ScreenOutput ())
        DrawingFont (FontID(0))
        twho = TextWidth ("Options")
        twho2 = TextHeight ("Options")
        awidth1 = (screenx / 2) - (twho / 2)
        DrawText (awidth1,200,"Options",RGB(255,255,255),0)
        DrawingFont (FontID(1))
          
        If musixtate = 1
          twmo = TextWidth ("Music on")
          twmo2 = TextHeight ("Music on")
          bwidth1 = (screenx / 2) - (twmo / 2)
          DrawText (bwidth1,250,"Music on",Font1Colour,Font1ColourBk) ;200,250
            Else
          twmof = TextWidth ("Music off")
          twmof2 = TextHeight ("Music off")
          gwidth1 = (screenx / 2) - (twmof / 2)
          DrawText (gwidth1,250,"Music off",Font1Colour,Font1ColourBk)
        EndIf
          
        If effectsstate = 1
          tweo = TextWidth ("Effects on")
          tweo2 = TextHeight ("Effects on")
          cwidth1 = (screenx / 2) - (tweo / 2)
          DrawText (cwidth1,280, "Effects on",Font2Colour,Font2ColourBk)
            Else
          tweof = TextWidth ("Effects off")
          tweof2 = TextHeight ("Effects off")
          hwidth1 = (screenx / 2) - (tweof / 2)
          DrawText (hwidth1,280, "Effects off",Font2Colour,Font2ColourBk)
      EndIf
          
      twvs = TextWidth ("Video Settings")
      twc = TextWidth ("Controls")
      twb = TextWidth ("Back")
      twvs2 = TextHeight ("Video Settings")
      twc2 = TextHeight ("Controls")
      twb2 = TextHeight ("Back")
      dwidth1 = (screenx / 2) - (twvs / 2)
      ewidth1 = (screenx / 2) - (twc / 2)
      fwidth1 = (screenx / 2) - (twb / 2)
      DrawText (dwidth1,310,"Video Settings",Font3Colour,Font3ColourBk)
      DrawText (ewidth1,340, "Controls",Font4Colour,Font4ColourBk)
      DrawText (fwidth1,370, "Back",Font5Colour,Font5ColourBk)
          
      Box (MouseX (), MouseY (), 3,3,255)
      StopDrawing ()
        If MouseX () > bwidth1 And MouseX () < bwidth1 + twmo And MouseY () > 250 And MouseY () < 250 + twmo2 ;200,263,250,270
          Font1Colour = Black
          Font1ColourBk = White
          Covered = 1
            Else
          Font1Colour = White
          Font1ColourBk = Black
        EndIf
          
        If MouseX () > cwidth1 And MouseX () < cwidth1 + tweo And MouseY () > 280 And MouseY () < 280 + tweo2
          Font2Colour = Black
          Font2ColourBk = White
          Covered = 2
            Else
          Font2Colour = White
          Font2ColourBk = Black
        EndIf
          
        If MouseX () > dwidth1 And MouseX () < dwidth1 + twvs And MouseY () > 310 And MouseY () < 310 + twvs2
          Font3Colour = Black
          Font3ColourBk = White
          Covered = 3
        Else
          Font3Colour = White
          Font3ColourBk = Black
        EndIf
          
        If MouseX () > ewidth1 And MouseX () < ewidth1 + twc And MouseY () > 340 And MouseY () < 340 + twc2
          Font4Colour = Black
          Font4ColourBk = White
          Covered = 4
            Else
          Font4Colour = White
          Font4ColourBk = Black
        EndIf
          
        If MouseX () > fwidth1 And MouseX () < fwidth1 + twb And MouseY () > 370 And MouseY () < 370 + twb2
          Font5Colour = Black
          Font5ColourBk = White
          Covered = 5
            Else
          Font5Colour = White
          Font5ColourBk = Black
        EndIf
          
        If mouseblock > 0
          mouseblock - 1
        EndIf
          
        If Covered = 1 And MouseButton (1) And mouseblock < 1
          Musix = 0
          musixtate + 1
          mouseblock = 30
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
        If Covered = 2 And MouseButton (1) And mouseblock < 1
          Effects = 0
          effectsstate + 1
          mouseblock = 30
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
        If Covered = 3 And MouseButton (1) And mouseblock < 1
          mouseblock = 30
          VideoSettings ()
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
        If Covered = 4 And MouseButton (1) And mouseblock < 1
          mouseblock = 30
          Controls ()
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
        If Covered = 5 And MouseButton (1) And mouseblock < 1
          mouseblock = 30
          Ending ()
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
        If musixtate > 1
          musixtate = 0
        EndIf
          
        If effectsstate > 1
          effectsstate = 0
        EndIf
        
        FlipBuffers ()
      Until KeyboardPushed (1)
    EndProcedure
    




  
  
Procedure NewGame ()
  Repeat
    ClearScreen (0)
          
    ExamineKeyboard ()
          
      StartDrawing (ScreenOutput ())
        DrawText (20,20,Str (Score),255,0)
        DrawText (400,20, Str (Score1),255,0)
      StopDrawing ()
          
      SoundVolume (0,Musix)
      SoundVolume (1,Effects)
      circlex + move
      circley + move2
          
      DisplayTransparentSprite (2,circlex,circley)
      DisplayTransparentSprite (0,player1x,player1y)
      DisplayTransparentSprite (1,player2x,player2y)
                    
      If circlex < 0
        If timer < 1
          PlaySound (1)
          timer = 20
        EndIf
        circlex = screenx / 2
        circley = screeny / 2
        Score + 1
      EndIf
          
      If circlex > 640
        If timer < 1
          PlaySound (1)
          timer = 20
        EndIf
        circlex = screenx / 2
        circley = screeny / 2
        Score2 + 1
      EndIf
          
      If circley + 10 > 478 Or circley < 0
        move2 * -1
        If timer < 1
          PlaySound (0)
          timer = 20
        EndIf
      EndIf
          
      If SpritePixelCollision (1,player2x,player2y,2,circlex,circley) Or SpritePixelCollision (0,player1x,player1y,2,circlex,circley)
        move * -1
        If timer < 1
          PlaySound (0)
          timer = 20
        EndIf
      EndIf
          
      If timer > 0
        timer - 1
      EndIf
          
      If KeyboardPushed (#PB_Key_Up)
        player2y - 2
      EndIf
      If KeyboardPushed (#PB_Key_Down)
      player2y + 2
      EndIf
          
      If KeyboardPushed (#PB_Key_W)
        player1y - 2
      EndIf
      If KeyboardPushed (#PB_Key_S)
      player1y + 2
      EndIf
          
      If player2y < 0
        player2y + 2
      ElseIf player2y + 70 > 480
        player2y - 2
      EndIf
          
      If player1y < 0
        player1y + 2
      ElseIf player1y + 70 > 480
        player1y - 2
      EndIf
          
  FlipBuffers ()
Until KeyboardPushed (1)

EndProcedure
  
  
  
  
  
  
  
  
  
  
Procedure mainMenu ()
  Repeat
    ClearScreen (0)
    ExamineKeyboard ()
    ExamineMouse ()
          
      StartDrawing (ScreenOutput ())
        DrawingFont (FontID(0))
        awidth = (screenx / 2) - (TextWidth ("Pong") / 2)
        aheight = (screeny / 2) - (TextHeight ("Pong") / 2)
        DrawText (awidth,aheight,"Pong",RGB(255,255,255),0)
          
        DrawingFont (FontID(1))
        twng = TextWidth ("New Game")
        twp = TextWidth ("Play")
        two = TextWidth ("Options")
        twq = TextWidth ("Quit")
          
        twng2 = TextHeight ("New Game")
        twp2 = TextHeight ("Play")
        two2 = TextHeight ("Options")
        twq2 = TextHeight ("Quit")
          
        bwidth = (screenx / 2) - (twng / 2)
        cwidth = (screenx / 2) - (twp / 2)
        dwidth = (screenx / 2) - (two / 2)
        ewidth = (screenx / 2) - (twq / 2)
          
        DrawText (bwidth,250,"New Game",Font1Colour,Font1ColourBk)
        DrawText (cwidth,280, "Play",Font2Colour,Font2ColourBk)
        DrawText (dwidth,310,"Options",Font3Colour,Font3ColourBk)
        DrawText (ewidth,340, "Quit",Font4Colour,Font4ColourBk)
        DrawText (20,30,Str (MouseX ()) + "    " + Str (MouseY ()),255,0)
        Box (MouseX (), MouseY (), 3,3,255)
      StopDrawing ()
          
        If MouseX () > bwidth   And MouseX () < bwidth + twng And MouseY () > 250 And MouseY () < 250 + twng2 ;298,364,250,270
          Font1Colour = Black
          Font1ColourBk = White
          Covered = 1
            Else
          Font1Colour = White
          Font1ColourBk = Black
        EndIf
          
        If MouseX () > cwidth And MouseX () < cwidth + twp And MouseY () > 280 And MouseY () < 280 + twp2
          Font2Colour = Black
          Font2ColourBk = White
          Covered = 2
            Else
          Font2Colour = White
          Font2ColourBk = Black           
        EndIf
          
        If MouseX () > dwidth And MouseX () < dwidth + two And MouseY () > 310 And MouseY () < 310 + two2
          Font3Colour = Black
          Font3ColourBk = White
          Covered = 3
            Else
          Font3Colour = White
          Font3ColourBk = Black            
        EndIf
          
        If MouseX () > ewidth And MouseX () < ewidth + twq And MouseY () > 340 And MouseY () < 340 + twq2
          Font4Colour = Black
          Font4ColourBk = White
          Covered = 4
            Else
          Font4Colour = White
          Font4ColourBk = Black           
        EndIf
          
        If Covered = 1 And MouseButton (1)
          mouseblock = 30
          NewGame ()
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
        If Covered = 2 And MouseButton (1)
            mouseblock = 30
            ;Gosub Game
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
        If Covered = 3 And MouseButton (1)
          mouseblock = 30
          Options ()
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
        If Covered = 4 And MouseButton (1)
          mouseblock = 30
          Ending ()
          ElseIf Covered = 0 And MouseButton (1)
        EndIf
          
    FlipBuffers ()
  Until KeyboardPushed (1)
EndProcedure
  
  
  OpenScreen (screenx,screeny,32,"kdfhlsk")
  UsePNGImageDecoder ()
  LoadSound (0,zieg + "Lazer.wav")
  LoadSound (1,zieg + "Explosion.wav")
  LoadSprite (0,zieg + "bat.png")
  CopySprite (0,1)
  LoadSprite (2,zieg + "ball.png")
  LoadFont (0,"Arial",15)
  LoadFont (1,"Arial",10)
        
  MouseLocate (screenx / 2, screeny / 2)
        
  mainMenu () ;loooooop
        
Question: Does it cause errors if I call a procedure inside a procedure?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: What is wrong with this code?

Post by IdeasVacuum »

Question: Does it cause errors if I call a procedure inside a procedure?
Not at all. In fact (don't make a habit of it), it's possible for a Procedure to call itself. I Often use a Procedure to call a succession of other procedures, so then I have one call in a loop rather than many - just keeps the code tidy.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: What is wrong with this code?

Post by J@ckWhiteIII »

That's great, now I have a better idea of what tidy code looks like :)
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: What is wrong with this code?

Post by kenmo »

IdeasVacuum wrote:Will be interesting to see if Procedures can replace Gosubs without a performance penalty.
I think gosubs are very very slightly faster than procedures in terms of instructions / CPU clock cycles (less overhead).... but the only time I ever saw a noticeable difference was back in the day when I was writing some graphics rendering programs... like when you are manually calculating hundreds of thousands of pixels per every single screen update... gosubs and macros gave me a slight speedup.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: What is wrong with this code?

Post by J@ckWhiteIII »

Okay, there's another problem. Probably has something to do with variable "mouseblock", but I'm not sure. clicking on "Options" seems to lead to Procedure "Ending()".
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: What is wrong with this code?

Post by Danilo »

J@ckWhiteIII wrote:Okay, there's another problem. Probably has something to do with variable "mouseblock", but I'm not sure. clicking on "Options" seems to lead to Procedure "Ending()".
If it opens the options screen and the mouse button is still pressed, it calls
Videosettings() because it is at the same screen position.
Out-comment the "ending()" in Videosettings() and it does not end. ;)

You don't check 'mouseblock' now.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: What is wrong with this code?

Post by J@ckWhiteIII »

Oh god you're right! Didn't see that :oops:
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: What is wrong with this code?

Post by IdeasVacuum »

....Once you have got it going as a screen-only game, how about converting it to a screen-in-window game (OpenWindowedScreen). That gives the best of both worlds - the game doesn't 'take over' the User's PC, The User can re-size the Window, the Options, score display etc can use the regular Window Gadgets.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: What is wrong with this code?

Post by J@ckWhiteIII »

I tried to do that with Waponez II and well...the results were not very satisfactory. But I'm gonna pick that idea up. But I don't know what to do at the very beginning. A window with two option gadgets asking you whether you want to run it in windowed or fullscreen mode?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: What is wrong with this code?

Post by IdeasVacuum »

...nah, I don't think those options are necessary, just ensure that the Window gadgets do not use more real estate than is necessary and it will be fine.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: What is wrong with this code?

Post by J@ckWhiteIII »

So, the main idea is a windowed Pong that can be played in fullscreen mode if wanted. Okay, gonna start that now
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: What is wrong with this code?

Post by IdeasVacuum »

...nah, I don't think those options are necessary
:shock: i.e. I think a maximized Window will do and probably nobody will play with it maximized anyway...........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: What is wrong with this code?

Post by J@ckWhiteIII »

Hm..probably. Atleast I'm sure the user wants to see the clock/taskbar
Post Reply