Problems

Just starting out? Need help? Post your questions and find answers here.
Dark Mars Software
User
User
Posts: 68
Joined: Sat Jun 19, 2004 3:34 am
Location: USA
Contact:

Problems

Post by Dark Mars Software »

1.The Demo Timer only updates when i move the mouse or press a key, yet when i run a Time Attack Game its automatic. Why isn't this working like the Timer() Function if its almost the same code.

2.When it goes into Demo Mode after a user presses a key it exits like its supposed to, but, when it goes back into demo mode it exits like a key was pressed when in fact it wasn't. After that it works normal and then it returns to the same behavor. It repeats this on and off behavor everytime.

Any ideas?

Code: Select all

Declare TwoPlayerGame()
Declare Timer()
Declare TimeAttackMoveBall()
Declare TimeAttackPrintScore()
Declare TimeAttackGame()
Declare TitleScreen()
Declare ResetScores()
Declare ResetLevel()
Declare PrintScores()
Declare PositionObjects()
Declare Player2Movement()
Declare Player1Movement()
Declare PlayerAI()
Declare OnePlayerGame()
Declare MoveBall()
Declare MountAComebackMod()
Declare MountAComebackGame()
Declare LoadSprites()
Declare LoadSounds()
Declare DemoTimer()
Declare DemoInput()
Declare DemoGame()
Declare ComputerAI()
Declare CleanUp()
Declare CheckForPause()
Declare CheckForMute()
Declare CDPlayer()
Declare BallBouncing()
;Initialize the Keyboard libraries
If InitKeyboard() = 0
  MessageRequester("Error",  "Can't open DirectX 7", 0)
  End
EndIf

;Initialize the Sprite libraries
If InitSprite() = 0
  MessageRequester("Error",  "Can't open DirectX 7", 0)
  End
EndIf

Global Sound_Possible.b

;Initialize the Sound libraries
If InitSound() = 0
  Sound_Possible  = 0
Else
  Sound_Possible  = 1
EndIf

Global CDPlayback_Possible.b

;Initalize the CDAudio libraries
If InitAudioCD() = 0
  CDPlayback_Possible = 0
  Else
  CDPlayback_Possible = 1
  UseAudioCD(0)
  EndIf

;setup the player structure
Structure Player
  X_Position.w
  Y_Position.w
  Score.b
EndStructure

;create player 1 object
Global Player1.Player

;create player 2 object
Global Player2.Player

;setup the ball structure
Structure Ball
  X_Position.w
  Y_Position.w
  X_Velocity.b
  Y_Velocity.b
EndStructure

;crreate ball object
Global Ball.Ball

;player muted sound
Global Mute.b

;CD Playback Variables
Global TotalTracks.b
Global CurrentTrack.b

;used to determine if a game is finished
Global Done.b

;used for the Time Attack Timer
Global Timer.b

;used for the Demo Mode Eclapsed Timer
Global DemoTimer.b

;used as a timer helper
Global lastRunMs.l

;setup load balance variable
Global dynamic_delay_set
dynamic_delay_set  = 50

TitleScreen()

Procedure BallBouncing()
  ;if the ball is at the left edge bounce it off
  If Ball\X_Position  > 432
    Ball\X_Position  = 431
    Ball\X_Velocity  =  - Ball\X_Velocity
  EndIf
  
  ;if the ball is at the right edge bounce it off
  If Ball\X_Position  < 10
    Ball\X_Position  = 11
    Ball\X_Velocity  =  - Ball\X_Velocity
  EndIf
EndProcedure

Procedure CDPlayer()
;if CD Playback is Possible
If CDPlayback_Possible = 1 

;Get the Total Number of Tracks on the CD
TotalTracks = AudioCDTracks()

ExamineKeyboard()

If ((KeyboardPushed(#PB_Key_F5) And (AudioCDStatus() > 0)))
;Decrease the Curent Track
CurrentTrack = CurrentTrack - 1

;if the Current Track is Less Then 1 Which is Not Possible Then Current Track is 1
If CurrentTrack < 1
CurrentTrack = 1
EndIf

PlayAudioCD(CurrentTrack, TotalTracks)
Else
CurrentTrack = CurrentTrack - 1

;if the Current Track is Less Then 1 Which is Not Possible Then Current Track is 1
If CurrentTrack < 1
CurrentTrack = 1
EndIf

EndIf

;If the User Presses F6 and a CD is not Playing Play it From The Current Track
If ((KeyboardPushed(#PB_Key_F6) And (AudioCDStatus() = 0)))
PlayAudioCD(CurrentTrack, TotalTracks)
;Otherwise Stop Playing It
Else
StopAudioCD()
EndIf

;If the User Presses F7 and a CD is not Paused Resume Playing it From The Current Track
If ((KeyboardPushed(#PB_Key_F7) And (AudioCDStatus() = 0)))
ResumeAudioCD()
Else
;Otherwise Pause It
PauseAudioCD()
EndIf


If ((KeyboardPushed(#PB_Key_F8) And (AudioCDStatus() > 0)))
CurrentTrack = CurrentTrack + 1

;if the Current Track is Greater Then The Total Number Of Tracks on The CD
;Which is Not Possible Then Current Track is the Last Track on the CD
If CurrentTrack > TotalTracks
CurrentTrack = TotalTracks
EndIf

PlayAudioCD(CurrentTrack, TotalTracks)
Else
CurrentTrack = CurrentTrack + 1

;if the Current Track is Greater Then The Total Number Of Tracks on The CD
;Which is Not Possible Then Current Track is the Last Track on the CD
If CurrentTrack > TotalTracks
CurrentTrack = TotalTracks
   EndIf
 EndIf
EndIf
EndProcedure

Procedure CheckForMute()
  ;check for keyboard input
  ExamineKeyboard()
  
  ;if either player presses M
  If KeyboardPushed(#PB_Key_M)
    If Mute  = 0
      mute  = 1
    Else
      mute  = 0
    EndIf
  EndIf
EndProcedure

Procedure CheckForPause()
  ;check for keyboard input
  ExamineKeyboard()
  
  ;if either player presses P
  If KeyboardPushed(#PB_Key_P)
    
    ;wait 75ms
    Delay(75)
    
    ;clear the screen to black
    ClearScreen(Black)
    If StartDrawing(ScreenOutput())
      
      ;let the user know the game is paused
      DrawText(300,  100, "Press P to unpause")
      StopDrawing()
      FlipBuffers()
      Repeat
        ExamineKeyboard()
        Delay(75)
        
        ;and wait until the user unpauses it by pressing P again
      Until KeyboardPushed(#PB_Key_P)
    EndIf
  EndIf
EndProcedure

Procedure CleanUp()
  ;The game is finished so remove all the sprites And sounds from memory
  
  ;remove background
  FreeSprite(0)
  
  ;remove player 1's paddles sprite
  FreeSprite(1)
  
  ;remove player'2 paddles sprite
  FreeSprite(2)
  
  ;remove ball sprite
  FreeSprite(3)
  
  ;if no sound was possible then the sound files
  ;weren't loaded and we don't need To unload them.
  If sound_possible  <  > 0
    
    ;remove applause.wav
    FreeSound(0)
    
    ;remove boink1.wav
    FreeSound(1)
    
    ;remove boink2.wav
    FreeSound(2)
    
    ;remove explosion.wav
    FreeSound(3)
  EndIf
EndProcedure

Procedure ComputerAI()
  ;if the ball is to the left of the computers paddle, move the computers paddle left
  If ((Ball\X_Position  < Player2\X_Position) And (Player2\X_Position > 5))
    Player2\X_Position  - 6
  EndIf
  
  ;if the ball is to the right of the computers paddle, move the computers paddle right
  If ((Ball\X_Position  > Player2\X_Position) And (Player2\X_Position < 425))
    Player2\X_Position  + 6
  EndIf

  ;Update the Game Screen
  PositionObjects()
EndProcedure

Procedure DemoGame()
;if a window was successfully opened
  If OpenWindow(0, 0, 0, 480, 640, #PB_Window_SystemMenu, "Pong Master v0.1 - Demo Mode")
    
    ; Now, open a 640*480 - 16 bits (65000 colours) screen
    OpenWindowedScreen(WindowID(0),  0, 0, 480, 640, 0, 1, 1)
    
    ;Load the Sprites into Memory
    LoadSprites()
    
    ;Load the Sound Effects into Memory
    LoadSounds()
    
    ;ResetTheScores
    ResetScores()
    
    ;Position the Sprites
    PositionObjects()
    
    ;set done to 0
    Done  = 0
    
    ;start the game loop
    Repeat
      
      ;Process Window Events
      Event  = WindowEvent()
      
      ;if the user tries to close the window then go to the title screen
      If Event  = #PB_Event_CloseWindow
        Done  = 1
      EndIf
      
      ;Delay the game 1 millisecand
      Delay(1)
      
      ;Game Interface Procedures
      BallBouncing()
      MoveBall()
      PlayerAI()
      ComputerAI()
      PrintScores()
      DemoInput()
            
      ; Inverse the buffers (the back become the front (visible)... And we can do the rendering on the back)
      
      FlipBuffers()
      
      ;increase the loop check by 1
      loopcounter  + 1
      ;if the game has looped 50 times then delay for 1 second
      If loopcounter  > =dynamic_delay_set
        Delay(1)
        loopcounter  = 0
      EndIf
      
    Until KeyboardPushed(#PB_Key_Escape)Or Done  = 1
    
    ;the player just won so wait a few secs so the player can see the win message
    Delay(1000)
    
    ;The game is finished so remove all the sprites and sounds from memory
    CleanUp()
    
    ;close the window
    CloseWindow(0)
    
    ;close the current screen
    CloseScreen()
    
    ;and reopen the titlescreen
    TitleScreen()
    
  Else
    MessageRequester("Error",  "Can't open a 480*640 - 16 bit screen !", 0)
  EndIf
  
  ;remove all the sprites And sounds from memory
  CleanUp()
EndProcedure

Procedure DemoInput()
;check for keyboard input
  ExamineKeyboard()
  
  ;if player 1 presses left
  If KeyboardPushed(#PB_Key_All)
Done = 1
EndIf
EndProcedure

Procedure DemoTimer()
If ElapsedMilliseconds() - lastRunMs  > 1000
    lastRunMs  = ElapsedMilliseconds()
    
    DemoTimer = DemoTimer + 1
    
    Debug DemoTimer
    
   ;If no game mode has been seleted for 10 seconds then start demo mode and reset the timer
   If DemoTimer = 10
   DemoTimer = 0
   DemoGame()
    EndIf
  EndIf
EndProcedure

Procedure LoadSounds()
  ;if we were able to initalize the sound drivers successfully
  If Sound_Possible  = 1
    
    ;load the sound effect
    LoadSound(0,  "Sound\applause.wav")
    
    ;if the sound effect couldn't be loaded because the file doesn't excist
    If IsSound(0) = 0
      
      ;give an error message and quit
      MessageRequester("Error",  "Can't open Sound\applause.wav", 0)
      End
    EndIf
    
    ;load the sound effect
    LoadSound(1,  "Sound\boink1.wav")
    
    ;if the sound effect couldn't be loaded because the file doesn't excist
    If IsSound(1) = 0
      
      ;give an error message and quit
      MessageRequester("Error",  "Can't open Sound\boink1.wav", 0)
      End
    EndIf
    
    ;load the sound effect
    LoadSound(2,  "Sound\boink2.wav")
    
    ;if the sound effect couldn't be loaded because the file doesn't excist
    If IsSound(2) = 0
      
      ;give an error message and quit
      MessageRequester("Error",  "Can't open Sound\boink2.wav", 0)
      End
    EndIf
    
    ;load the sound effect
    LoadSound(3,  "Sound\explosion.wav")
    
    ;if the sound effect couldn't be loaded because the file doesn't excist
    If IsSound(3) = 0
      
      ;give an error message and quit
      MessageRequester("Error",  "Can't open Sound\explosion.wav", 0)
      End
    EndIf
  EndIf
EndProcedure

Procedure LoadSprites()
  ;load the background
  LoadSprite(0,  "Sprite\background.bmp", 0)
  
  ;if the background couldn't be loaded because the file doesn't excist
  If IsSprite(0) = 0
    
    ;give an error message and quit
    MessageRequester("Error",  "Can't open Sprite\background.bmp", 0)
    End
  EndIf
  
  ;load player 1's bat sprite
  LoadSprite(1,  "Sprite\bat1.bmp", 0)
  
  ;if the sprite couldn't be loaded because the file doesn't excist
  If IsSprite(1) = 0
    
    ;give an error message and quit
    MessageRequester("Error",  "Can't open Sprite\bat1.bmp", 0)
    End
  EndIf
  
  ;otherwise set player 1's sprite initial positions
  Player1\X_Position  = 300
  
  ;load player 2's bat sprite
  LoadSprite(2,  "Sprite\bat2.bmp", 0)
  
  ;if the sprite couldn't be loaded because the file doesn't excist
  If IsSprite(2) = 0
    
    ;give an error message and quit
    MessageRequester("Error",  "Can't open Sprite\bat2.bmp", 0)
    End
  EndIf
  
  player2\X_Position  = 300
  
  ;load the sprite
  LoadSprite(3,  "Sprite\ball.bmp", 0)
  
  ;if the sprite couldn't be loaded because the file doesn't excist
  If IsSprite(3) = 0
    
    ;give an error message and quit
    MessageRequester("Error",  "Can't open Sprite\ball.bmp", 0)
    End
  EndIf
  
  ;otherwise set the ball sprite initial position
  Ball\X_Position  = 240
  ball\Y_Position  = 320
  
  ;setup the ball's inital x velocity
  Ball\X_Velocity  = (Random(-3) - Random(3))
  
  ;if the balls x velocity is 0 make it equal To 3
  If Ball\X_Velocity = 0
  Ball\X_Velocity = 3
  EndIf
  
  ;setup the ball's inital y velocity
  Ball\Y_Velocity  = (Random(-3) - Random(3))
  
  ;if the balls x velocity is 0 make it equal To 3
  If Ball\Y_Velocity = 0
  Ball\Y_Velocity = 3
  EndIf

EndProcedure

Procedure MountAComebackGame()
  ;if a window was opened successfully
  If OpenWindow(0, 0, 0, 480, 640, #PB_Window_SystemMenu, "Pong Master v0.1 - Mount-A-Comeback")
    
    ; Now, open a 640*480 - 16 bits (65000 colours) screen
    OpenWindowedScreen(WindowID(0),  0, 0, 480, 640, 0, 1, 1)
    
    ;Load the Sprites into Memory
    LoadSprites()
    
    ;Load the Sound Effects into Memory
    LoadSounds()
    
    ;Apply Mount-A-Comeback Mod
    MountAComebackMod()
    
    ;Set Done to 0
    Done  = 0
    
    ;Position the Sprites
    PositionObjects()
    
    ;start the game loop
    Repeat
      
      ;Process Window Events
      Event  = WindowEvent()
      
      ;if the user tries to close the window then go to the title screen
      If Event  = #PB_Event_CloseWindow
        Done  = 1
      EndIf
      
      ;Delay the game 1 millisecand
      Delay(1)
      
      ;Game Interface Procedures
      CheckForMute()
      CheckForPause()
      BallBouncing()
      MoveBall()
      Player1Movement()
      ComputerAI()
      PrintScores()
      CDPlayer()
      
      ; Inverse the buffers (the back become the front (visible)... And we can do the rendering on the back)
      
      FlipBuffers()
      
      loopcounter  + 1
      
      If loopcounter  > =dynamic_delay_set
        Delay(1)
        loopcounter  = 0
      EndIf
      
    Until KeyboardPushed(#PB_Key_Escape)Or Done  = 1
    
    ;the player just won so wait a few secs so the player can see the win message
    Delay(1000)
    
    ;The game is finished so remove all the sprites and sounds from memory
    CleanUp()
    
    ;and close the window
    CloseWindow(0)
    
    ;close the current screen
    CloseScreen()
    
    ;and reopen the titlescreen
    TitleScreen()
    
  Else
    
    ;otherwise let the user know we couldn't open the game window
    MessageRequester("Error",  "Can't open a 480*640 - 16 bit screen !", 0)
  EndIf
  
  ;remove all the sprites and sounds from memory
  CleanUp()
EndProcedure

Procedure MountAComebackMod()
  ;Modification Code for Mount-A-Comeback Game
  
  ;set player 1's score to 13
  Player1\Score  = 13
  
  ;set player 2's score to 14
  Player2\Score  = 14
EndProcedure

Procedure MoveBall()
  Ball\X_Position  = Ball\X_Position  + Ball\X_Velocity
  Ball\Y_Position  = Ball\Y_Position  + ball\Y_Velocity
  
  ;if the ball gets past player 2's paddle player 1 scores a point
  If Ball\Y_Position  < 10
    Player1\Score  + 1
    
    ;and reset all the sprites position
    ResetLevel()
  EndIf
  
  ;if the ball gets past player 1's paddle player 2 scores a point
  If Ball\Y_Position  > 615
    Player2\Score  + 1
    ;and reset all the sprites position
    ResetLevel()
  EndIf
  
  ;if the ball collides with player 1's paddle, bounce the ball off it
  If SpriteCollision(3, Ball\X_Position, Ball\Y_Position, 1, Player1\X_Position, 610)
    Ball\X_Position  = Player1\X_Position  + 10
    Ball\Y_Velocity  =  - Ball\Y_Velocity
    
    ;increase the ball's x velocity
    If Ball\X_Velocity  < 0
      Ball\X_Velocity  - 1
    Else
      Ball\X_Velocity  + 1
    EndIf
    
    ;increase the ball's y velocity
    If Ball\Y_Velocity  < 0
      Ball\Y_Velocity  - 1
    Else
      Ball\Y_Velocity  + 1
    EndIf
    
    ;Play the Boink1.wav Sound if sound_effects are possible and the player hasn't muted sounds
    If ((Sound_Possible  = 1)And (Mute  = 0))
      PlaySound(1,  0)
    EndIf
    
  EndIf
  
  
  ;if the ball collides with player 2's paddle, bounce the ball off it
  If SpriteCollision(3, Ball\X_Position, Ball\Y_Position, 2, Player2\X_Position, 15)
    Ball\X_Position  = Player2\X_position  + 10
    Ball\Y_Velocity  =  - Ball\Y_velocity
    
    ;increase the ball's x velocity
    If Ball\X_Velocity  < 0
      Ball\X_Velocity  - 1
    Else
      Ball\X_Velocity  + 1
    EndIf
    
    ;increase the ball's y velocity
    If Ball\Y_Velocity  < 0
      Ball\Y_Velocity  - 1
    Else
      Ball\Y_Velocity  + 1
    EndIf
    
    ;Play the Boink2.wav Sound if sound_effects are possible and the player hasn't muted sounds
    If ((Sound_Possible  = 1)And (Mute  = 0))
      PlaySound(2,  0)
    EndIf
    
  EndIf
EndProcedure

Procedure OnePlayerGame()
  ;if a window was successfully opened
  If OpenWindow(0, 0, 0, 480, 640, #PB_Window_SystemMenu, "Pong Master v0.1 - One Player")
    
    ; Now, open a 640*480 - 16 bits (65000 colours) screen
    OpenWindowedScreen(WindowID(0),  0, 0, 480, 640, 0, 1, 1)
    
    ;Load the Sprites into Memory
    LoadSprites()
    
    ;Load the Sound Effects into Memory
    LoadSounds()
    
    ;ResetTheScores
    ResetScores()
    
    ;Position the Sprites
    PositionObjects()
    
    ;set done to 0
    Done  = 0
    
    ;start the game loop
    Repeat
      
      ;Process Window Events
      Event  = WindowEvent()
      
      ;if the user tries to close the window then go to the title screen
      If Event  = #PB_Event_CloseWindow
        Done  = 1
      EndIf
      
      ;Delay the game 1 millisecand
      Delay(1)
      
      ;Game Interface Procedures
      CheckForMute()
      CheckForPause()
      BallBouncing()
      MoveBall()
      Player1Movement()
      ComputerAI()
      PrintScores()
      CDPlayer()
      
      ; Inverse the buffers (the back become the front (visible)... And we can do the rendering on the back)
      
      FlipBuffers()
      
      ;increase the loop check by 1
      loopcounter  + 1
      ;if the game has looped 50 times then delay for 1 second
      If loopcounter  > =dynamic_delay_set
        Delay(1)
        loopcounter  = 0
      EndIf
      
    Until KeyboardPushed(#PB_Key_Escape)Or Done  = 1
    
    ;the player just won so wait a few secs so the player can see the win message
    Delay(1000)
    
    ;The game is finished so remove all the sprites and sounds from memory
    CleanUp()
    
    ;close the window
    CloseWindow(0)
    
    ;close the current screen
    CloseScreen()
    
    ;and reopen the titlescreen
    TitleScreen()
    
  Else
    MessageRequester("Error",  "Can't open a 480*640 - 16 bit screen !", 0)
  EndIf
  
  ;remove all the sprites And sounds from memory
  CleanUp()
EndProcedure

Procedure PlayerAI()
;if the ball is to the left of the playerss paddle, move the players paddle left
  If ((Ball\X_Position  < Player1\X_Position) And (Player1\X_Position > 5))
    Player1\X_Position  - 5
  EndIf
  
  ;if the ball is to the right of the players paddle, move the players paddle right
  If ((Ball\X_Position  > Player1\X_Position) And (Player1\X_Position < 425))
    Player1\X_Position  + 5
  EndIf

;Update the Game Screen
  PositionObjects()
EndProcedure

Procedure Player1Movement()
  
  ;check for keyboard input
  ExamineKeyboard()
  
  ;if player 1 presses left
  If KeyboardPushed(#PB_Key_Left)And Player1\X_Position  >  5
    Player1\X_Position  - 5
  EndIf
  
  ;if player 1 presses right
  If KeyboardPushed(#PB_Key_Right)And Player1\X_Position  < 425
    Player1\X_Position  + 5
  EndIf

;Update the Game Screen
  PositionObjects()
EndProcedure

Procedure Player2Movement()
  ;check for keyboard input
  ExamineKeyboard()
  
  ;if player 2 presses left
  If KeyboardPushed(#PB_Key_A)And Player2\X_Position  >  5
    Player2\X_Position  - 5
  EndIf
  
  ;if player 2 presses right
  If KeyboardPushed(#PB_Key_S)And Player2\X_Position  < 425
    Player2\X_Position  + 5
  EndIf
  
  ;Update the Game Screen
  PositionObjects()
EndProcedure

Procedure PositionObjects()
  ;Clear the screen to Black
  ClearScreen(Black)
  
  ;Position The Background
  DisplaySprite(0,  0, 0)
  
  ;Position Player 1's Sprite
  DisplayTransparentSprite(1,  Player1\X_Position, 610)
  
  ;Position Player 2's Sprite
  DisplayTransparentSprite(2,  Player2\X_Position, 15)
  
  ;Position the Ball Sprite
  DisplayTransparentSprite(3,  Ball\X_Position, Ball\Y_Position)
EndProcedure

Procedure PrintScores()
  ;If no one has won print the current scores
  If (Player1\Score  <  > 15)Or (Player2\Score  <  > 15)
    If StartDrawing(ScreenOutput())
      
      ;draw player 1's score at x 400 y 100
      DrawText(400,  100, Str(Player1\Score))
      
      ;draw a dash at x 420 y 100
      DrawText(420,  100, "-")
      
      ;draw player 2's score at x 430 y 100
      DrawText(430,  100, Str(Player2\Score))
      StopDrawing()
    EndIf
  EndIf
  
  ;if Player 1 has won
  If Player1\Score  = 15
    If StartDrawing(ScreenOutput())
      
      ;draw the "Player 1 has won" win message at x 350 y 100
      DrawText(350,  100, "Player 1 has Won")
      StopDrawing()
      
      ;since someone won set done to 1 to close the game window
      Done  = 1
    EndIf
    
    ;Play the Applause.wav Sound if sound_effects are possible and the player hasn't muted sounds
    If ((Sound_Possible  = 1)And (Mute  = 0))
      PlaySound(0,  0)
    EndIf
    
  EndIf
  
  ;if Player 2 has won
  If Player2\Score  = 15
    If StartDrawing(ScreenOutput())
      
      ;draw the "Player 2 has won" win message at x 350 y 100
      DrawText(350,  100, "Player 2 Has Won")
      StopDrawing()
      
      ;since someone won set done to 1 to close the game window
      Done  = 1
    EndIf
    
    ;Play the Applause.wav Sound if sound_effects are possible and the player hasn't muted sounds
    If ((Sound_Possible  = 1)And (Mute  = 0))
      PlaySound(0,  0)
    EndIf
    
  EndIf
EndProcedure

Procedure ResetLevel()
  ;reset player 1's paddle sprite position
  Player1\X_Position  = 300
  
  ;reset player 2's paddle sprite position
  Player2\X_Position  = 300
  
  ;reset the ball sprite's x position
  Ball\X_Position  = 240
  
  ;reset the ball sprite's y position
  ball\Y_Position  = 320
  
  ;reset the ball x velocity
  Ball\X_Velocity  = (Random(-3) - Random(3))
  
  ;if the balls x velocity is 0 make it equal To 3
  If Ball\X_Velocity = 0
  Ball\X_Velocity = 3
  EndIf
  
  ;reset the ball y velocity
  Ball\Y_Velocity  = (Random(-3) - Random(3))
  
  ;if the balls x velocity is 0 make it equal To 3
  If Ball\Y_Velocity = 0
  Ball\Y_Velocity = 3
  EndIf
EndProcedure

Procedure ResetScores()
  ;we're starting a new game so reset player 1's score to 0
  Player1\Score  = 0
  
  ;we're starting a new game so reset player 2's score to 0
  Player2\Score  = 0
EndProcedure

Procedure TitleScreen()
  ;- Window Constants
  ;
  Enumeration
  #MainMenu
  EndEnumeration
  
  ;- Gadget Constants
  ;
  Enumeration
  #OnePlayerGame
  #TwoPlayerGame
  #MountAComeback
  #TimeAttack
  #Quit
  EndEnumeration
  
  ;if the window was opened successfully
  If OpenWindow(#MainMenu, 216, 0, 209, 216, #PB_Window_SystemMenu  | #PB_Window_TitleBar  | #PB_Window_ScreenCentered , "Pong Master - Main Menu")
    
    ;create gadget list
    If CreateGadgetList(WindowID(#MainMenu))
      ButtonGadget(#OnePlayerGame,  10, 10, 180, 30, "1 Pplayer Game")
      ButtonGadget(#TwoPlayerGame,  10, 50, 180, 30, "2 Player Game")
      ButtonGadget(#MountAComeback,  10, 90, 180, 30, "Mount-A-Comeback")
      ButtonGadget(#TimeAttack,  10, 130, 180, 30, "Time Attack")
      ButtonGadget(#Quit,  10, 170, 180, 30, "Quit")
      
    EndIf
  EndIf
  
  Repeat ; Start of the event loop
    
    DemoTimer()
    
    Event  = WaitWindowEvent(); This line waits until an event is received from Windows
    
    WindowID  = EventWindow(); The Window where the event is generated, can be used in the gadget procedures
    
    GadgetID  = EventGadget(); Is it a gadget event?
    
    EventType  = EventType(); The event type
    
    ;You can place code here, and use the result as parameters for the procedures
    
    If Event  = #PB_Event_Gadget
      
      ;if the One Player Button was pressed goto OnePlayerGame Procedure
      If GadgetID  = #OnePlayerGame
        OnePlayerGame()
        
        ;if the Two Player Button was pressed goto TwoPlayerGame Procedure
      ElseIf GadgetID  = #TwoPlayerGame
        TwoPlayerGame()
        
        ;if the Mount-A-Comeback Button was pressed goto MountAComeback Procedure
      ElseIf GadgetID  = #MountAComeback
        MountAComebackGame()
        
        ;if the Time Attack Button was pressed goto TimeAttack Procedure
      ElseIf GadgetID  = #TimeAttack
        TimeAttackGame()
        ;if the Quit Button was pressed quit
      ElseIf GadgetID  = #Quit
        End
      EndIf
      
    EndIf
    
  Until Event  = #PB_Event_CloseWindow ; End of the event loop
EndProcedure

Procedure TimeAttackGame()
  ;if a window was successfully opened
  If OpenWindow(0, 0, 0, 480, 640, #PB_Window_SystemMenu, "Pong Master v0.1 - One Player")
    
    ; Now, open a 640*480 - 16 bits (65000 colours) screen
    OpenWindowedScreen(WindowID(0),  0, 0, 480, 640, 0, 1, 1)
    
    ;Load the Sprites into Memory
    LoadSprites()
    
    ;Load the Sound Effects into Memory
    LoadSounds()
    
    ;ResetTheScores
    ResetScores()
    
    ;Position the Sprites
    PositionObjects()
    
    ;set done to 0
    Done  = 0
    
    Timer  = 120
    
    ;start the game loop
    Repeat
      
      ;Process Window Events
      Event  = WindowEvent()
      
      ;if the user tries to close the window then go to the title screen
      If Event  = #PB_Event_CloseWindow
        Done  = 1
      EndIf
      
      ;Delay the game 1 millisecand
      Delay(1)
      
      ;Game Interface Procedures
      CheckForMute()
      CheckForPause()
      BallBouncing()
      TimeAttackMoveBall()
      Player1Movement()
      ComputerAI()
      Timer()
      TimeAttackPrintScore()
      CDPlayer()
      
      ; Inverse the buffers (the back become the front (visible)... And we can do the rendering on the back)
      
      FlipBuffers()
      
      ;increase the loop check by 1
      loopcounter  + 1
      ;if the game has looped 50 times then delay for 1 second
      If loopcounter  > =dynamic_delay_set
        Delay(1)
        loopcounter  = 0
      EndIf
      
    Until KeyboardPushed(#PB_Key_Escape)Or Done  = 1
    
    ;the player just won so wait a few secs so the player can see the win message
    Delay(1000)
    
    ;The game is finished so remove all the sprites and sounds from memory
    CleanUp()
    
    ;close the window
    CloseWindow(0)
    
    ;close the current screen
    CloseScreen()
    
    ;and reopen the titlescreen
    TitleScreen()
    
  Else
    MessageRequester("Error",  "Can't open a 480*640 - 16 bit screen !", 0)
  EndIf
  
  ;remove all the sprites And sounds from memory
  CleanUp()
EndProcedure

Procedure TimeAttackPrintScore()
  ;If the time has not run out print the current score
  If Timer  <  > 0
    If StartDrawing(ScreenOutput())
      
      ;draw the placeholder "Score" at x 400 y 100
      DrawText(400,  100, "Score:")
      
      ;draw player 1's score at x 420 y 100
      DrawText(450,  100, Str(Player1\Score))
      
      ;draw the placeholder "Time:" at x 400 y 120
      DrawText(400,  120, "Time:")
      
      ;draw the cutrrent time limit at x 420 y 120
      DrawText(440,  120, Str(Timer))
      
      StopDrawing()
    EndIf
  EndIf
  
  If Timer  = 0
    
    If StartDrawing(ScreenOutput())
      
      ;draw the placeholder "Score" at x 400 y 100
      DrawText(400,  100, "Score:")
      
      ;draw player 1's score at x 420 y 100
      DrawText(420,  100, Str(Player1\Score))
      
      ;draw the placeholder "Time:" at x 400 y 120
      DrawText(400,  120, "Time:")
      
      ;draw the "Time's Up" message at x 420 y 120
      DrawText(420,  120, "Times Up")
      
      StopDrawing()
    EndIf
  EndIf
  
EndProcedure

Procedure TimeAttackMoveBall()
  Ball\X_Position  = Ball\X_Position  + Ball\X_Velocity
  Ball\Y_Position  = Ball\Y_Position  + ball\Y_Velocity
  
  ;if the ball gets past player 2's paddle player 1 scores a point
  If Ball\Y_Position  < 10
    Player1\Score  + 1
    
    ;and reset all the sprites position
    ResetLevel()
  EndIf
  
  ;if the ball gets past player 1's paddle player 2 scores a point
  If Ball\Y_Position  > 615
    Player1\Score  - 1
    ;and reset all the sprites position
    ResetLevel()
  EndIf
  
  ;if the ball collides with player 1's paddle, bounce the ball off it
  If SpriteCollision(3, Ball\X_Position, Ball\Y_Position, 1, Player1\X_Position, 610)
    Ball\X_Position  = Player1\X_Position  + 10
    Ball\Y_Velocity  =  - Ball\Y_Velocity
    
    ;increase the ball's x velocity
    If Ball\X_Velocity  < 0
      Ball\X_Velocity  - 1
    Else
      Ball\X_Velocity  + 1
    EndIf
    
    ;increase the ball's y velocity
    If Ball\Y_Velocity  < 0
      Ball\Y_Velocity  - 1
    Else
      Ball\Y_Velocity  + 1
    EndIf
    
    ;Play the Boink1.wav Sound if sound_effects are possible and the player hasn't muted sounds
    If ((Sound_Possible  = 1)And (Mute  = 0))
      PlaySound(1,  0)
    EndIf
    
  EndIf
  
  
  ;if the ball collides with player 2's paddle, bounce the ball off it
  If SpriteCollision(3, Ball\X_Position, Ball\Y_Position, 2, Player2\X_Position, 15)
    Ball\X_Position  = Player2\X_position  + 10
    Ball\Y_Velocity  =  - Ball\Y_velocity
    
    ;increase the ball's x velocity
    If Ball\X_Velocity  < 0
      Ball\X_Velocity  - 1
    Else
      Ball\X_Velocity  + 1
    EndIf
    
    ;increase the ball's y velocity
    If Ball\Y_Velocity  < 0
      Ball\Y_Velocity  - 1
    Else
      Ball\Y_Velocity  + 1
    EndIf
    
    ;Play the Boink2.wav Sound if sound_effects are possible and the player hasn't muted sounds
    If ((Sound_Possible  = 1)And (Mute  = 0))
      PlaySound(2,  0)
    EndIf
    
  EndIf
EndProcedure

Procedure Timer()
  If ElapsedMilliseconds() - lastRunMs  > 1000
    lastRunMs  = ElapsedMilliseconds()
    Timer  - 1
    
    ;If the Users Time Attack Timer Has Expired
    If Timer  = 0
      Done  = 1
    
    ;Play the Explosion.wav Sound if sound_effects are possible and the player hasn't muted sounds
    If ((Sound_Possible  = 1)And (Mute  = 0))
      PlaySound(3,  0)
    EndIf
    
    EndIf
  
  EndIf
EndProcedure

Procedure TwoPlayerGame()
  ;If the window was opened successfully
  If OpenWindow(0, 0, 0, 480, 640, #PB_Window_SystemMenu, "Pong Master v0.1 - Two Player")
    
    ; Now, open a 640*480 - 16 bits (65000 colours) screen
    OpenWindowedScreen(WindowID(0),  0, 0, 480, 640, 0, 1, 1)
    
    ;Load the Sprites into Memory
    LoadSprites()
    
    ;Load the Sound Effects into Memory
    LoadSounds()
    
    ;Reset The Scores
    ResetScores()
    
    ;Position the Sprites
    PositionObjects()
    
    ;Set Done to 0
    Done  = 0
    
    ;start the game loop
    Repeat
      
      ;Process Window Events
      Event  = WindowEvent()
      
      ;if the user tries to close the window then go to the title screen
      If Event  = #PB_Event_CloseWindow
        Done  = 1
      EndIf
      
      ;Delay the game 1 millisecand
      Delay(1)
      
      ;Game Interface Procedures
      CheckForMute()
      CheckForPause()
      BallBouncing()
      MoveBall()
      Player1Movement()
      Player2Movement()
      PrintScores()
      CDPlayer()
      
      ; Inverse the buffers (the back become the front (visible)... And we can do the rendering on the back)
      
      FlipBuffers()
      
      ;increase the loop counter by 1
      loopcounter  + 1
      ;if the game has looped 50 times then delay for 1 second
      If loopcounter  > =dynamic_delay_set
        Delay(1)
        loopcounter  = 0
      EndIf
      
    Until KeyboardPushed(#PB_Key_Escape)Or Done  = 1
    
    ;the player just won so wait a few secs so the player can see the win message
    Delay(1000)
    
    ;The game is finished so remove all the sprites and sounds from memory
    CleanUp()
    
    ;and close the window
    CloseWindow(0)
    
    ;close the current screen
    CloseScreen()
    
    ;and reopen the title screen
    TitleScreen()
    
  Else
    
    ;otherwise let the user know the window couldn't be opened
    MessageRequester("Error",  "Can't open a 480*640 - 16 bit screen !", 0)
  EndIf
  
  ;remove all the sprites And sounds from memory
  CleanUp()
EndProcedure
;ExecutableFormat=Windows
;Executable=C:\Projects\Pong Master - Pure BASIC\Pong Master.exe
;EOF
Dark Mars Software
User
User
Posts: 68
Joined: Sat Jun 19, 2004 3:34 am
Location: USA
Contact:

Post by Dark Mars Software »

Anyone wanna help me out?
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

this should solve the problem Nr. 1 :

Code: Select all

WaitWindowEvent(1000)
and this solves problem Nr. 2 :

Code: Select all

  If KeyboardPushed(#PB_Key_Escape)
    Done = 1 
  EndIf 
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

but maybe this could be a bug in PB4B6 :?: can someone else confirm this :wink:

Code: Select all

InitKeyboard()
InitSprite()

Procedure test()
  If OpenWindow(0, 0, 0, 480, 640, "Pong Master v0.1 - Demo Mode",#PB_Window_SystemMenu)
  EndIf
  
  OpenWindowedScreen(WindowID(0),  0, 0, 480, 640, 0, 1, 1)
  
  done=0
  
  Repeat
   WindowEvent()
   Delay(100)
   ExamineKeyboard()
   done=KeyboardPushed(#PB_Key_All) ; push any key and you will see
   Debug done
  Until done=1
EndProcedure

Repeat
  test()
ForEver
Dark Mars Software
User
User
Posts: 68
Joined: Sat Jun 19, 2004 3:34 am
Location: USA
Contact:

Post by Dark Mars Software »

Thanks but, i want my demo game to end if any key input is recieved. Most likely this is a bug in PureBASIC Beta 5.
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

then go on and post it in the bugs section :twisted:
Dark Mars Software
User
User
Posts: 68
Joined: Sat Jun 19, 2004 3:34 am
Location: USA
Contact:

Post by Dark Mars Software »

Here is the results of my debug window:

Code: Select all

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Dark Mars Software
User
User
Posts: 68
Joined: Sat Jun 19, 2004 3:34 am
Location: USA
Contact:

Post by Dark Mars Software »

Posted in Bug Forum.
Post Reply