What is wrong with this code?
Posted: Fri Jun 29, 2012 2:46 pm
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).
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 ^^
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
Thanks in advance

P.s.: Tipps are desired ^^