Page 1 of 2

What is wrong with this code?

Posted: Fri Jun 29, 2012 2:46 pm
by J@ckWhiteIII
Okay, so this is Pong, a little modified. I know it looks horrible (Started writing it with an old version of PB, that's why there are no spaces before If etc.) and it may need some more modifications because I didn't remove variables I once added, but I hope someone sees what's the mistake.

The problem is, that if the code is run as a .exe file, the program opens a black screen and then windows tells me that it has encountered a problem etc. All sprites are in the file's directory (The path comments next to the LoadSprite lines are outdated, so ignore them).

Code: Select all

EnableExplicit

If InitKeyboard () And InitSprite () And InitMouse () And InitSound ()


  Define.i timer, musix, effects, musixtate, effectsstate, spriteloader,Number, screenx, screeny, ScreenOpen, start, Black, White
  Global Font1Colour,Font1ColourBk,Font2Colour,Font2ColourBk,Font3Colour,Font3ColourBk,Font4Colour,Font4ColourBk,Font5Colour,Font5ColourBk
  Define.s zieg
  Global awidth,awidth1,twng,twng2,tweo,tweo2,tweof,tweof2,twp2,aheight,twp,two,twq,two2,twq2,bwidth,cwidth,dwidth,ewidth,fwidth1,bwidth1,cwidth1,dwidth1,ewidth1
  Global mouseblock, Covered,circlex,circley,player1x,player1y,player2x,player2y,move,move2,ticker,Score,Score1,Score2,twho,twho2
  Global twmo,twmo2,twmof,twmof2,gwidth1,hwidth1,twvs,twvs2,twc2,twc,twb,twb2
  timer = 0
musix = 100
effects = 100
musixtate = 1
effectsstate = 1
spriteloader = 0
Number = 1
ScreenOpen = 0
screenx = 640
screeny = 480
zieg = GetPathPart (ProgramFilename())
UsePNGImageDecoder ()
If LoadSound (0,zieg + "Lazer.wav") ;C:\Benutzer\Marius\PureBasic\Examples\Sources - Advanced\Waponez II\Data\Lazer.wav
If LoadSound (1,zieg + "Explosion.wav") ;C:\Benutzer\Marius\PureBasic\Examples\Sources - Advanced\Waponez II\Data\Explosion.wav






If OpenScreen (screenx,screeny,16,"Pong")

;-Menu
Start:

If start = 0
start = 1
EndIf 






LoadFont (0,"Arial",15)
LoadFont (1,"Arial",10)

Black = RGB (0,0,0)
White = RGB (255,255,255)
Font1Colour = White
Font1ColourBk = Black
Font2Colour =White
Font2ColourBk = Black
Font3Colour = White
Font2ColourBk = Black
Font4Colour = White
Font4ColourBk = Black


MouseLocate (screenx / 2, screeny / 2)
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
Gosub 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
Gosub Options
ElseIf Covered = 0 And MouseButton (1)
EndIf
If Covered = 4 And MouseButton (1)
mouseblock = 30
Gosub Ending
ElseIf Covered = 0 And MouseButton (1)
EndIf
FlipBuffers ()
Until KeyboardPushed (1)



;-Ending
Ending:
CloseScreen ()
End 
Return



;-New Game
NewGame: 
circlex=200
circley=Random (480)
move = 1
move2 = 1
player1x = 0
player1y = 200
player2x = 635
player2y = 200
ticker = Random (3)
If ticker = 1
move * -1
ElseIf ticker = 3
move2 * -1
ElseIf ticker = 0
move * 1
ElseIf ticker = 2
move2 * 1
EndIf 

LoadSprite (0,zieg + "bat.png")
CopySprite (0,1)
LoadSprite (2,zieg + "ball.png")



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)
End
Return


;-ContinueOldGame
Game:
End
Return

;-Options
Options:
Covered = 0
Musix = 100
Effects = 100
Black = RGB (0,0,0)
White = RGB (255,255,255)
Font1Colour = White
Font1ColourBk = Black
Font2Colour =White
Font2ColourBk = Black
Font3Colour = White
Font2ColourBk = Black
Font4Colour = White
Font4ColourBk = Black
Font5Colour = White
Font5ColourBk = Black

;twmo = TextWidth ("Music on")
;twmof = TextWidth ("Music off")
;tweo = TextWidth ("Effects on")
;tweof = TextWidth ("Effects off")
;twvs = TextWidth ("Video Settings")
;twc = TextWidth ("Controls")
;twb = TextWidth ("Back")


;bwidth1 = (width / 5) - (twmo / 2)
;cwidth1 = (width / 5) - (tweo / 2)
;dwidth1 = (width / 5) - (twvs / 2)
;ewidth1 = (width / 5) - (twc / 2)
;fwidth1 = (width / 5) - (twb / 2)
;gwidth1 = (width / 5) - (twmof / 2)
;hwidth1 = (width / 5) - (tweof / 2)


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
Gosub VideoSettings
ElseIf Covered = 0 And MouseButton (1)
EndIf

If Covered = 4 And MouseButton (1) And mouseblock < 1
mouseblock = 30
Gosub Controls
ElseIf Covered = 0 And MouseButton (1)
EndIf

If Covered = 5 And MouseButton (1) And mouseblock < 1
mouseblock = 30
Gosub Quit
ElseIf Covered = 0 And MouseButton (1)
EndIf

If musixtate > 1
musixtate = 0
EndIf

If effectsstate > 1
effectsstate = 0
EndIf

FlipBuffers ()
Until KeyboardPushed (1)

Return

Quit:
ScreenOpen = 1
Gosub Start
Return
Return


;-VideoSettings
VideoSettings:

End

Return


;-Controls
Controls:

End
Return

Else
  MessageRequester ("Openscreen failed!","")
EndIf
Else
  MessageRequester ("Sound 2 fail!","")
EndIf
Else 
  MessageRequester ("Sound 1 fail!","")
EndIf
Else
MessageRequester ("Info", "Sprite fail!")
EndIf
This is the code. As I said, it looks horrible and complicated. I hope anyone here can help me...

Thanks in advance :)
P.s.: Tipps are desired ^^

Re: What is wrong with this code?

Posted: Fri Jun 29, 2012 3:27 pm
by Demivec
J@ckWhiteIII wrote:The problem is, that if the code is run as a .exe file, the program opens a black screen and then windows tells me that it has encountered a problem etc. All sprites are in the file's directory (The path comments next to the LoadSprite lines are outdated, so ignore them).
Perhaps you are missing the sound files.

Re: What is wrong with this code?

Posted: Fri Jun 29, 2012 4:57 pm
by J@ckWhiteIII
Good idea, but they should be safe because I put them into If:Else:Endif.

Re: What is wrong with this code?

Posted: Fri Jun 29, 2012 10:31 pm
by IdeasVacuum

Code: Select all

screenx = 640
screeny = 480
A lot of monitors will struggle to support 640 x 480. If you only need a small screen for the game, then probably the way to go would be to use OpenWindowedScreen(), which is a screen region displayed within a regular Window.

......you might get more help if you move this post to the right home! Tricks and Tips is there for us to publish working solutions that others might find useful this post ought to be in the Game Programing section. :wink:

Re: What is wrong with this code?

Posted: Fri Jun 29, 2012 10:36 pm
by J@ckWhiteIII
Hey,
Would 800x600 be better?

It was quite an accident, forgot to go back to the forum part i usually post stuff ^^ Question: How do I move this post?

Re: What is wrong with this code?

Posted: Fri Jun 29, 2012 10:49 pm
by kenmo
Various tips:

1. You SHOULD check for failures (result zero) every time you load a resource... such as sprites or fonts, just like you did with the sounds. This will save you a lot of debugging time. And when you have a problem with an EXE which you don't have from the PB editor, it's often a resource-not-loaded-correctly error.

2. You NEED to check the result of StartDrawing(), I don't think it's "safe" to call other drawing commands if StartDrawing() failed. Good job remembering StopDrawing().

3. You should start using arrays! And later on, start using structures. Instead of writing "player1x" and "player1y" and "Font3Colour" and so on, you can make it much cleaner:

Code: Select all

Structure PlayerStruct
  x.i
  y.i
EndStructure

Dim Player.PlayerStruct(1) ; (1) creates two Players, indexed 0 and 1

Player(0)\x = 100
Player(0)\y = 200

Player(1)\x = 50
Player(1)\y = 400

; -----------------

Structure FontStruct
  Color.i
  Background.i
EndStructure

Dim Font.FontStruct(3) ; (3) creates four Fonts

Font(0)\Background = #Black
Font(1)\Color = #White
4. Most programmers will tell you: try to package your sections of code into organized Procedures, rather than Gosub/Returns. (Yes, I know Gosub is a classic feature of BASICs...) But also, and sort of contradictory to that, many people recommend minimizing how many Global variables you use... although I am guilty of this!

4a. You have two Return statements in a row, around line 475. Is that a mistake?

5. Please indent your code before posting it :)



For everyone's sanity, here's the same code as above, but with some indentation.....

Code: Select all

EnableExplicit

If InitKeyboard () And InitSprite () And InitMouse () And InitSound ()


  Define.i timer, musix, effects, musixtate, effectsstate, spriteloader,Number, screenx, screeny, ScreenOpen, start, Black, White
  Global Font1Colour,Font1ColourBk,Font2Colour,Font2ColourBk,Font3Colour,Font3ColourBk,Font4Colour,Font4ColourBk,Font5Colour,Font5ColourBk
  Define.s zieg
  Global awidth,awidth1,twng,twng2,tweo,tweo2,tweof,tweof2,twp2,aheight,twp,two,twq,two2,twq2,bwidth,cwidth,dwidth,ewidth,fwidth1,bwidth1,cwidth1,dwidth1,ewidth1
  Global mouseblock, Covered,circlex,circley,player1x,player1y,player2x,player2y,move,move2,ticker,Score,Score1,Score2,twho,twho2
  Global twmo,twmo2,twmof,twmof2,gwidth1,hwidth1,twvs,twvs2,twc2,twc,twb,twb2
  timer = 0
  musix = 100
  effects = 100
  musixtate = 1
  effectsstate = 1
  spriteloader = 0
  Number = 1
  ScreenOpen = 0
  screenx = 640
  screeny = 480
  zieg = GetPathPart (ProgramFilename())
  UsePNGImageDecoder ()
  If LoadSound (0,zieg + "Lazer.wav") ;C:\Benutzer\Marius\PureBasic\Examples\Sources - Advanced\Waponez II\Data\Lazer.wav
    If LoadSound (1,zieg + "Explosion.wav") ;C:\Benutzer\Marius\PureBasic\Examples\Sources - Advanced\Waponez II\Data\Explosion.wav
      
      
      
      
      
      
      If OpenScreen (screenx,screeny,16,"Pong")
      
        ;-Menu
        Start:
        
        If start = 0
          start = 1
        EndIf
        
        
        
        
        
        
        LoadFont (0,"Arial",15)
        LoadFont (1,"Arial",10)
        
        Black = RGB (0,0,0)
        White = RGB (255,255,255)
        Font1Colour = White
        Font1ColourBk = Black
        Font2Colour =White
        Font2ColourBk = Black
        Font3Colour = White
        Font2ColourBk = Black
        Font4Colour = White
        Font4ColourBk = Black
        
        
        MouseLocate (screenx / 2, screeny / 2)
        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
            Gosub 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
            Gosub Options
          ElseIf Covered = 0 And MouseButton (1)
          EndIf
          If Covered = 4 And MouseButton (1)
            mouseblock = 30
            Gosub Ending
          ElseIf Covered = 0 And MouseButton (1)
          EndIf
          FlipBuffers ()
        Until KeyboardPushed (1)
        
        
        
        ;-Ending
        Ending:
        CloseScreen ()
        End
        Return
        
        
        
        ;-New Game
        NewGame:
        circlex=200
        circley=Random (480)
        move = 1
        move2 = 1
        player1x = 0
        player1y = 200
        player2x = 635
        player2y = 200
        ticker = Random (3)
        If ticker = 1
          move * -1
        ElseIf ticker = 3
          move2 * -1
        ElseIf ticker = 0
          move * 1
        ElseIf ticker = 2
          move2 * 1
        EndIf
        
        LoadSprite (0,zieg + "bat.png")
        CopySprite (0,1)
        LoadSprite (2,zieg + "ball.png")
        
        
        
        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)
        End
        Return
        
        
        ;-ContinueOldGame
        Game:
        End
        Return
        
        ;-Options
        Options:
        Covered = 0
        Musix = 100
        Effects = 100
        Black = RGB (0,0,0)
        White = RGB (255,255,255)
        Font1Colour = White
        Font1ColourBk = Black
        Font2Colour =White
        Font2ColourBk = Black
        Font3Colour = White
        Font2ColourBk = Black
        Font4Colour = White
        Font4ColourBk = Black
        Font5Colour = White
        Font5ColourBk = Black
        
        ;twmo = TextWidth ("Music on")
        ;twmof = TextWidth ("Music off")
        ;tweo = TextWidth ("Effects on")
        ;tweof = TextWidth ("Effects off")
        ;twvs = TextWidth ("Video Settings")
        ;twc = TextWidth ("Controls")
        ;twb = TextWidth ("Back")
        
        
        ;bwidth1 = (width / 5) - (twmo / 2)
        ;cwidth1 = (width / 5) - (tweo / 2)
        ;dwidth1 = (width / 5) - (twvs / 2)
        ;ewidth1 = (width / 5) - (twc / 2)
        ;fwidth1 = (width / 5) - (twb / 2)
        ;gwidth1 = (width / 5) - (twmof / 2)
        ;hwidth1 = (width / 5) - (tweof / 2)
        
        
        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
            Gosub VideoSettings
          ElseIf Covered = 0 And MouseButton (1)
          EndIf
          
          If Covered = 4 And MouseButton (1) And mouseblock < 1
            mouseblock = 30
            Gosub Controls
          ElseIf Covered = 0 And MouseButton (1)
          EndIf
          
          If Covered = 5 And MouseButton (1) And mouseblock < 1
            mouseblock = 30
            Gosub Quit
          ElseIf Covered = 0 And MouseButton (1)
          EndIf
          
          If musixtate > 1
            musixtate = 0
          EndIf
          
          If effectsstate > 1
            effectsstate = 0
          EndIf
        
        FlipBuffers ()
        Until KeyboardPushed (1)
        
        Return
        
        Quit:
        ScreenOpen = 1
        Gosub Start
        Return
        Return
        
        
        ;-VideoSettings
        VideoSettings:
        
        End
        
        Return
        
        
        ;-Controls
        Controls:
        
        End
        Return
      
      Else
        MessageRequester ("Openscreen failed!","")
      EndIf
    Else
      MessageRequester ("Sound 2 fail!","")
    EndIf
  Else
    MessageRequester ("Sound 1 fail!","")
  EndIf
Else
  MessageRequester ("Info", "Sprite fail!")
EndIf
[/size]


PS. I've never had a problem with 640x480.... I use it all the time, it seems to work on every desktop/laptop. Maybe newer displays are stopping support for resolutions that low?

PPS. I HAVE had problems with sprite commands in 16-bit screens... but only on my laptop. Not many other people seem to have this problem... maybe try a 32-bit screen and see if your problem still occurs?

Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 12:03 am
by IdeasVacuum
Mostly the problem is the untidiness of the code - you can't see the wood for the trees and so it is difficult to spot missing functions/Keywords etc.

There are numerous ElseIf conditions that do not do anything.

It would be easier to use Procedures, though I think some aficionados might say that they could slow things down.

32bit instead of 16 sounds good since most people will have that set. 640x480 is a problem for those whose Desktop Monitors minimum resolution is typically 800 x 600 (Is that the res you meant too Kenmo? It is pretty common these days and of course laptops are another thing altogether since you might need to match the widescreen ratio for best graphics text).

The re-jiggled rather than re-designed code below 'works' in that you are presented with a black screen with writing on it, but I don't know if the game is going to work - may be the screen text should include some basic instructions on how to proceed?

Code: Select all

EnableExplicit
UsePNGImageDecoder()

If InitKeyboard() And InitSprite() And InitMouse() And InitSound()

        ExamineDesktops()

        Define.i timer, musix, effects, musixtate, effectsstate, spriteloader,Number, screenx, screeny, ScreenOpen, start, Black, White
        Global Font1Colour,Font1ColourBk,Font2Colour,Font2ColourBk,Font3Colour,Font3ColourBk,Font4Colour,Font4ColourBk,Font5Colour,Font5ColourBk
        Define.s zieg
        Global awidth,awidth1,twng,twng2,tweo,tweo2,tweof,tweof2,twp2,aheight,twp,two,twq,two2,twq2,bwidth,cwidth,dwidth,ewidth,fwidth1,bwidth1,cwidth1,dwidth1,ewidth1
        Global mouseblock, Covered,circlex,circley,player1x,player1y,player2x,player2y,move,move2,ticker,Score,Score1,Score2,twho,twho2
        Global twmo,twmo2,twmof,twmof2,gwidth1,hwidth1,twvs,twvs2,twc2,twc,twb,twb2
               timer = 0
               musix = 100
             effects = 100
           musixtate = 1
        effectsstate = 1
        spriteloader = 0
              Number = 1
          ScreenOpen = 0
             screenx = DesktopWidth(0)
             screeny = DesktopHeight(0)
                zieg = GetPathPart(ProgramFilename())

        If LoadSound(0,zieg + "Lazer.wav") : Else : MessageRequester("Problem","Lazer.wav did not load") : EndIf 
        If LoadSound(1,zieg + "Explosion.wav") : Else : MessageRequester("Problem","Explosion.wav did not load") : EndIf 

        If OpenScreen(screenx,screeny,32,"Pong")

               ;################# GoSub Start
               Start:
               
                     If start = 0
                     start = 1
                     EndIf
                     
                     LoadFont(0,"Arial",15)
                     LoadFont(1,"Arial",10)
                     
                     Black = RGB(0,0,0)
                     White = RGB(255,255,255)
                     Font1Colour = White
                     Font1ColourBk = Black
                     Font2Colour =White
                     Font2ColourBk = Black
                     Font3Colour = White
                     Font2ColourBk = Black
                     Font4Colour = White
                     Font4ColourBk = Black
                     
                     MouseLocate(screenx / 2, screeny / 2)
                     
                     Repeat
                              ClearScreen(0)
                              ExamineKeyboard()

                              If 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()
                              EndIf
                     
                              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
                                    Gosub 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
                                    Gosub Options
                              ;ElseIf Covered = 0 And MouseButton(1)
                              EndIf 
                     
                              If Covered = 4 And MouseButton(1)
                                    mouseblock = 30
                                    Gosub Ending
                              ;ElseIf Covered = 0 And MouseButton(1)
                              EndIf
                     
                              FlipBuffers()
                     
                     Until KeyboardPushed(1)
                     
                     ;################# GoSub Ending
                     Ending:
                              CloseScreen()
                              End
                     Return
                     ;################# Ending
                     
                     ;################# GoSub New Game
                     NewGame:
                               circlex=200
                               circley=Random(480)
                               move = 1
                               move2 = 1
                               player1x = 0
                               player1y = 200
                               player2x = 635
                               player2y = 200
                               ticker = Random(3)
                               If ticker = 1
                               move * -1
                               ElseIf ticker = 3
                               move2 * -1
                               ElseIf ticker = 0
                               move * 1
                               ElseIf ticker = 2
                               move2 * 1
                               EndIf
                               
                               If LoadSprite(0,zieg + "bat.png") 

                                      CopySprite(0,1)
                               Else
                                      MessageRequester("Problem","bat.png did not load") 
                               EndIf

                               If LoadSprite(2,zieg + "ball.png") : Else : MessageRequester("Problem","ball.png did not load") : EndIf
                               
                               Repeat
                                      ClearScreen(0)
                                      ExamineKeyboard()
                                      
                                      If StartDrawing(ScreenOutput())

                                              DrawText(20,20,Str(Score),255,0)
                                              DrawText(400,20, Str(Score1),255,0)
                                              StopDrawing()
                                      EndIf

                                      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)
                                      End
                     Return
                     ;################# New Game
                     
                     ;################# GoSub Game(Continue Old Game)
                     Game:
                               End
                     Return
                     ;################# Game
                     
                     ;################# GoSub Options
                     Options:
                               Covered = 0
                               Musix = 100
                               Effects = 100
                               Black = RGB(0,0,0)
                               White = RGB(255,255,255)
                               Font1Colour = White
                               Font1ColourBk = Black
                               Font2Colour =White
                               Font2ColourBk = Black
                               Font3Colour = White
                               Font2ColourBk = Black
                               Font4Colour = White
                               Font4ColourBk = Black
                               Font5Colour = White
                               Font5ColourBk = Black
                               
                               ;twmo = TextWidth("Music on")
                               ;twmof = TextWidth("Music off")
                               ;tweo = TextWidth("Effects on")
                               ;tweof = TextWidth("Effects off")
                               ;twvs = TextWidth("Video Settings")
                               ;twc = TextWidth("Controls")
                               ;twb = TextWidth("Back")
                               
                               
                               ;bwidth1 =(width / 5) -(twmo / 2)
                               ;cwidth1 =(width / 5) -(tweo / 2)
                               ;dwidth1 =(width / 5) -(twvs / 2)
                               ;ewidth1 =(width / 5) -(twc / 2)
                               ;fwidth1 =(width / 5) -(twb / 2)
                               ;gwidth1 =(width / 5) -(twmof / 2)
                               ;hwidth1 =(width / 5) -(tweof / 2)
                               
                               
                               Repeat
                                        ClearScreen(0)
                                        ExamineKeyboard()
                                        ExamineMouse()
                                        If 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() 
                                        EndIf
                                        
                                        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
                                              Gosub VideoSettings
                                        ;ElseIf Covered = 0 And MouseButton(1)
                                        EndIf
                                        
                                        If Covered = 4 And MouseButton(1) And mouseblock < 1
                                              mouseblock = 30
                                              Gosub Controls
                                        ;ElseIf Covered = 0 And MouseButton(1)
                                        EndIf
                                        
                                        If Covered = 5 And MouseButton(1) And mouseblock < 1
                                              mouseblock = 30
                                              Gosub Quit
                                        ;ElseIf Covered = 0 And MouseButton(1)
                                        EndIf
                                        
                                        If musixtate > 1
                                              musixtate = 0
                                        EndIf
                                        
                                        If effectsstate > 1
                                              effectsstate = 0
                                        EndIf
                                        
                                        FlipBuffers()

                               Until KeyboardPushed(1)
                     Return
                     ;################# Options

                     ;################# GoSub Quit
                     Quit:
                               ScreenOpen = 1
                               Gosub Start
                     Return
                     ;################# Quit
               Return
               ;################# Start

               ;################# GoSub VideoSettings
               VideoSettings:
                               End
               Return
               ;################# VideoSettings

               ;################# GoSub Controls
               Controls:
                               End
               Return

        Else
               MessageRequester("Problem","Openscreen Failed!")
        EndIf

Else
        MessageRequester("Problem", "Initialisation Failed")
EndIf


Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 12:21 am
by J@ckWhiteIII
First of all: Thanks for the replies! Got some GOOD tips here :)

My screen resolution is 1600 x 1024 (Or something like that). As most of the text is displayed in the centre of the screen, I'm dividing the screenwidth by 2. But even though I opened a 800 x 600 screen, DesktopWidth () still returns the desktop width (1600). This means on other computers, the text would never be in the middle (atleast I tried it on two computers, and it didn't work properly). Any way to fix that?

One more thing: IdeasVacuum, the sounds/sprites aren't the problem (None of our messagerequesters popped up). But still I can't run the .exe for it always crashes when the screen is opened. o.O weird thing

Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 1:06 am
by IdeasVacuum
DesktopWidth(iMonitorNumber) -this is always going to return the actual resolution of the monitor screen, so using it will make your game 'fullscreen' on any PC. The code used to determine the Text Position is good, the text will always be central to the screen, no matter the screen width. I do not know if that is true of the game itself but it looks like it......

I suspect this code includes content that is recycled from an old forum post? Have you ever actually got it to work?

The 'tidy' version of the code that I posted does not crash. I also ensured that the sprite and sound files existed in the app's folder, so that the game had the maximum chance of working - but as I said, I do not know how to start it! :mrgreen:

Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 1:12 am
by kenmo
IdeasVacuum wrote:640x480 is a problem for those whose Desktop Monitors minimum resolution is typically 800 x 600 (Is that the res you meant too Kenmo? It is pretty common these days and of course laptops are another thing altogether since you might need to match the widescreen ratio for best graphics text).
I am on a widescreen laptop... Windows (and/or my display drivers?) only lets me choose from four "desktop" resolutions, from 800x600 to 1440x900. But for fullscreen games my display is capable of many resolutions, and PureBasic is fully capable of using them. Try running this:

Code: Select all

If InitSprite()
  If ExamineScreenModes()
    While NextScreenMode()
      If ScreenModeDepth() = 32
        Debug Str(ScreenModeWidth()) + "x" + Str(ScreenModeHeight())
      EndIf
    Wend
  EndIf
EndIf
For me, it returns TEN resolutions from 1440x900 all the way down to 320x200. (Although I rarely go below 640x480). Of course the aspect ratio can become a problem, but most drivers give some aspect ratio options: stretch to fill screen, stretch but maintain aspect ratio, and don't stretch at all (resulting in a tiny game screen in the middle of your display!)


J@ckWhite... There is no ScreenWidth() or ScreenHeight() command. Although you can call OutputWidth() and OutputHeight() inside a Start/StopDrawing() block. Or better yet, since you know the width and height when you open the screen, you should just keep track of them using your own variables.

Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 1:26 am
by IdeasVacuum
...ah, you are right kenmo. When I run your snippet, I get 11 resolutions listed and the very first is 640x480! If I run Jack's app at 640x480 (32 or 16bit), the text is positioned OK but it's very rough (not 'blocky' but just not very clear). 320 240 is printed (in red) in the top left-hand corner.

I do not see the mouse pointer on screen.

Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 1:39 am
by IdeasVacuum
In the loop of Sub 'Start' I have added the following two lines after FlipBuffers() :

Code: Select all

If(KeyboardPushed(#PB_Key_N)) : Gosub NewGame : EndIf
If(KeyboardPushed(#PB_Key_O)) : Gosub Options : EndIf
They in fact work and the game does start!

Also, in the options menu, the mouse works and I can see I was looking for the wrong mouse pointer - it is in fact just a red dot.

Edit - seems to work better at the 'regular' (640x480) screen res but it's much more difficult to play than I remember the Pong we had on a black and white TV :mrgreen: Also, only the left-hand player seems to score? It could be me, I don't have the time to mess with it.

Edit2 - I originally made the bats 50pix high. A good cheat for hopeless players like me is to double that height.......

Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 2:40 am
by IdeasVacuum
OK, got the scores working too, so this is the state of play now:

Code: Select all

EnableExplicit
UsePNGImageDecoder()

If InitKeyboard() And InitSprite() And InitMouse() And InitSound()

        ExamineDesktops()

        Define.i timer, musix, effects, musixtate, effectsstate, spriteloader,Number, screenx, screeny, ScreenOpen, start, Black, White
        Global Font1Colour,Font1ColourBk,Font2Colour,Font2ColourBk,Font3Colour,Font3ColourBk,Font4Colour,Font4ColourBk,Font5Colour,Font5ColourBk
        Define.s zieg
        Global awidth,awidth1,twng,twng2,tweo,tweo2,tweof,tweof2,twp2,aheight,twp,two,twq,two2,twq2,bwidth,cwidth,dwidth,ewidth,fwidth1,bwidth1,cwidth1,dwidth1,ewidth1
        Global mouseblock, Covered,circlex,circley,player1x,player1y,player2x,player2y,move,move2,ticker,Score1,Score2,twho,twho2
        Global twmo,twmo2,twmof,twmof2,gwidth1,hwidth1,twvs,twvs2,twc2,twc,twb,twb2
               timer = 0
               musix = 100
             effects = 100
           musixtate = 1
        effectsstate = 1
        spriteloader = 0
              Number = 1
          ScreenOpen = 0
             screenx = 640
             screeny = 480
                zieg = GetPathPart(ProgramFilename())

        If LoadSound(0,zieg + "Lazer.wav") : Else : MessageRequester("Problem","Lazer.wav did not load") : EndIf
        If LoadSound(1,zieg + "Explosion.wav") : Else : MessageRequester("Problem","Explosion.wav did not load") : EndIf

        If OpenScreen(screenx,screeny,32,"Pong")

               MouseLocate(screenx/2,screeny/2) ;Just to make sure it can be seen

               ;################# GoSub Start
               Start:

                     If start = 0
                     start = 1
                     EndIf

                     LoadFont(0,"Arial",15)
                     LoadFont(1,"Arial",10)

                     Black = RGB(0,0,0)
                     White = RGB(255,255,255)
                     Font1Colour = White
                     Font1ColourBk = Black
                     Font2Colour =White
                     Font2ColourBk = Black
                     Font3Colour = White
                     Font2ColourBk = Black
                     Font4Colour = White
                     Font4ColourBk = Black

                     MouseLocate(screenx / 2, screeny / 2)

                     Repeat
                                  ClearScreen(0)
                              ExamineKeyboard()
                                 ExamineMouse()

                              If 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()
                              EndIf

                              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
                                    Gosub 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
                                    Gosub Options
                              ;ElseIf Covered = 0 And MouseButton(1)
                              EndIf

                              If Covered = 4 And MouseButton(1)
                                    mouseblock = 30
                                    Gosub Ending
                              ;ElseIf Covered = 0 And MouseButton(1)
                              EndIf

                              FlipBuffers()

                              If( KeyboardPushed(#PB_Key_N) Or KeyboardPushed(#PB_Key_N) ) : Gosub NewGame : EndIf
                              If( KeyboardPushed(#PB_Key_O) Or KeyboardPushed(#PB_Key_O) ) : Gosub Options : EndIf

                     Until KeyboardPushed(#PB_Key_Escape)

                     ;################# GoSub Ending
                     Ending:
                              CloseScreen()
                              End
                     Return
                     ;################# Ending

                     ;################# GoSub New Game
                     NewGame:
                               circlex=200
                               circley=Random(screeny)
                               move = 1
                               move2 = 1
                               player1x = 0
                               player1y = 200
                               player2x = 635
                               player2y = 200
                               ticker = Random(3)
                               If ticker = 1
                               move * -1
                               ElseIf ticker = 3
                               move2 * -1
                               ElseIf ticker = 0
                               move * 1
                               ElseIf ticker = 2
                               move2 * 1
                               EndIf

                               If LoadSprite(0,zieg + "bat.png")

                                      CopySprite(0,1)
                               Else
                                      MessageRequester("Problem","bat.png did not load")
                               EndIf

                               If LoadSprite(2,zieg + "ball.png") : Else : MessageRequester("Problem","ball.png did not load") : EndIf

                               Repeat
                                      ClearScreen(0)
                                      ExamineKeyboard()

                                      If StartDrawing(ScreenOutput())

                                                 DrawText(240,20,Str(Score2),RGB(255,0,0),RGB(0,0,0))
                                                 DrawText(440,20,Str(Score1),RGB(255,0,0),RGB(0,0,0))
                                              StopDrawing()
                                      EndIf

                                      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
                                              Score1 + 1
                                      EndIf

                                      If circlex > screenx

                                              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 > screeny

                                             player2y - 2
                                      EndIf

                                      If player1y < 0

                                             player1y + 2
                                      ElseIf player1y + 70 > screeny

                                             player1y - 2
                                      EndIf

                                      If( KeyboardPushed(#PB_Key_O) Or KeyboardPushed(#PB_Key_O) ) : Gosub Options : EndIf

                                      FlipBuffers()

                              Until KeyboardPushed(#PB_Key_Escape)
                              Gosub Start
                     Return
                     ;################# New Game

                     ;################# GoSub Game(Continue Old Game)
                     Game:
                               End
                     Return
                     ;################# Game

                     ;################# GoSub Options
                     Options:
                               Covered = 0
                               Musix = 100
                               Effects = 100
                               Black = RGB(0,0,0)
                               White = RGB(255,255,255)
                               Font1Colour = White
                               Font1ColourBk = Black
                               Font2Colour =White
                               Font2ColourBk = Black
                               Font3Colour = White
                               Font2ColourBk = Black
                               Font4Colour = White
                               Font4ColourBk = Black
                               Font5Colour = White
                               Font5ColourBk = Black

                               ;twmo = TextWidth("Music on")
                               ;twmof = TextWidth("Music off")
                               ;tweo = TextWidth("Effects on")
                               ;tweof = TextWidth("Effects off")
                               ;twvs = TextWidth("Video Settings")
                               ;twc = TextWidth("Controls")
                               ;twb = TextWidth("Back")


                               ;bwidth1 =(width / 5) -(twmo / 2)
                               ;cwidth1 =(width / 5) -(tweo / 2)
                               ;dwidth1 =(width / 5) -(twvs / 2)
                               ;ewidth1 =(width / 5) -(twc / 2)
                               ;fwidth1 =(width / 5) -(twb / 2)
                               ;gwidth1 =(width / 5) -(twmof / 2)
                               ;hwidth1 =(width / 5) -(tweof / 2)


                               Repeat
                                        ClearScreen(0)
                                        ExamineKeyboard()
                                        ExamineMouse()
                                        If 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()
                                        EndIf

                                        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
                                              Gosub VideoSettings
                                        ;ElseIf Covered = 0 And MouseButton(1)
                                        EndIf

                                        If Covered = 4 And MouseButton(1) And mouseblock < 1
                                              mouseblock = 30
                                              Gosub Controls
                                        ;ElseIf Covered = 0 And MouseButton(1)
                                        EndIf

                                        If Covered = 5 And MouseButton(1) And mouseblock < 1
                                              mouseblock = 30
                                              Gosub Quit
                                        ;ElseIf Covered = 0 And MouseButton(1)
                                        EndIf

                                        If musixtate > 1
                                              musixtate = 0
                                        EndIf

                                        If effectsstate > 1
                                              effectsstate = 0
                                        EndIf

                                        FlipBuffers()

                               Until KeyboardPushed(#PB_Key_Escape)
                               Gosub Start
                     Return
                     ;################# Options

                     ;################# GoSub Quit
                     Quit:
                               ScreenOpen = 1
                               Gosub Start
                     Return
                     ;################# Quit
               Return
               ;################# Start

               ;################# GoSub VideoSettings
               VideoSettings:
                               Gosub Start
                               ;End
               Return
               ;################# VideoSettings

               ;################# GoSub Controls
               Controls:
                               Gosub Start
                               ;End
               Return
               ;################# Controls
        Else
               MessageRequester("Problem","Openscreen Failed!")
        EndIf

Else
        MessageRequester("Problem", "Initialisation Failed")
EndIf
Sprites Download: Pong.zip

Edit: Got the Main Menu working with the mouse - ExamineMouse() was missing. :wink:

Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 12:38 pm
by J@ckWhiteIII
First of all: The code has worked before I started trying to make it look nicer ^^ (how ironical) I even commented the "getpathpart.." line out and used the proper paths...but it still didn't work when I ran it in PB. Could all the problems be caused by the hardware?

i probably accidentally deleted the examine mouse line ^^

To the empty "ElseIf"s: Well without them, the program quits everytime I click the mousebutton. no matter where the mouse is located at the moment :(

Re: What is wrong with this code?

Posted: Sat Jun 30, 2012 3:11 pm
by J@ckWhiteIII
I copied the Main Menu, New Game and Ending Gosubs into a new PB project. Also, I used proper paths. NOW it works. Interesting, because I haven't changed the actual code. I'm structuring it a bit more now, so it looks nicer...