Page 2 of 2

Re: Real time in games

Posted: Tue Jul 03, 2012 5:23 pm
by J@ckWhiteIII
Complete code now, weird Shared variable, some unnecessary variables.

Code: Select all

EnableExplicit

Enumeration
  #Win
  #Sound0
  #Sound1
  #SpriteBorder
  #SpriteBat
  #SpriteBat1
  #SpriteBall
  #SpriteBorder1
EndEnumeration

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

Global we.i
Global Score1.i
Global Score2.i
Global zieg.s = "C:\Program Files\ProgMaster\Pong\"
Global circlex.i
Global circley.i
Global movex.i
Global movey.i
Global PlayerTwo.i
Global pseudoy.i
Global Block.i
Global StartTime.i
Global pseudomovex.i
Global pseudomovey.i
Global Blocky.i
Global Timerlol.i

Score1 = 0
Score2 = 0
circlex = 320
circley = Random (480)
movex = 2
movey = 2
PlayerTwo = 2
Block = 0

Structure player
  x.i
  y.i
EndStructure

UsePNGImageDecoder ()

Global Dim Player.player(1)

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

Player(1)\x = 635
Player(1)\y = 200

Procedure Game ()
  Shared Blocky
  ClearScreen (0)
  StartDrawing (ScreenOutput ())
  DrawText (20,20,Str (Score1),RGB(255,255,255),0)
  DrawText (500,20,Str (Score2),RGB(255,255,255),0)
  DrawText (380,20,Str ((ElapsedMilliseconds () - StartTime) / 1000),RGB(255,255,255),0)
  If PlayerTwo = -2
    DrawText (300,20,"Player 2",RGB(255,255,255),0)
  EndIf
  StopDrawing ()
  
  DisplayTransparentSprite (#SpriteBall,circlex,circley)
  DisplayTransparentSprite (#SpriteBat,Player(0)\x,Player(0)\y)
  DisplayTransparentSprite (#SpriteBat1,Player(1)\x,Player(1)\y)
  DisplayTransparentSprite (#SpriteBorder,0,0)
  DisplayTransparentSprite (#SpriteBorder1,0,475)
  
  circlex + movex
  circley + movey
  
  If SpritePixelCollision (#SpriteBat,Player(0)\x,Player(0)\y,#SpriteBall,circlex,circley) Or SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBall,circlex,circley)
    movex * -1
    If Block < 1
      PlaySound (#Sound1)
    EndIf
    Block = 30
  EndIf
  
  If SpritePixelCollision (#SpriteBall,circlex,circley,#SpriteBorder,0,0)
    movey * -1
    PlaySound (#Sound1)
  ElseIf SpritePixelCollision (#SpriteBall,circlex,circley,#SpriteBorder1,0,475)
    movey * -1
    PlaySound (#Sound1)
  EndIf
  
  If KeyboardPushed (#PB_Key_Up)
    Player(1)\y - 3
  ElseIf KeyboardPushed (#PB_Key_Down)
    Player(1)\y + 3
  EndIf
  
  If KeyboardPushed (#PB_Key_F2) And Block < 1
    PlayerTwo * -1
    Block = 30
  EndIf
  
  If Block > 0
    Block - 1
  EndIf
  
  
  If PlayerTwo = 2
    If KeyboardPushed (#PB_Key_W)
      Player(0)\y - 3
    ElseIf KeyboardPushed (#PB_Key_S)
      Player(0)\y + 3
    EndIf
    If SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBorder1,0,475) 
      Player(1)\y - 3
    EndIf
    If SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBorder,0,0) 
      Player(1)\y + 3
    EndIf
  Else
    If movex < 0
      pseudoy = circley - 30
      If Player(0)\y <> pseudoy
        If Player(0)\y < pseudoy
          Player(0)\y + 2
        Else
          Player(0)\y - 2 
        EndIf
      EndIf
    EndIf    
  EndIf

  If SpritePixelCollision (#SpriteBat,Player(0)\x,Player(0)\y,#SpriteBorder,0,0)
    Player(0)\y + 2
  EndIf
  If SpritePixelCollision (#SpriteBat,Player(0)\x,Player(0)\y,#SpriteBorder1,0,475)
    Player(0)\y - 2
  EndIf
  If SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBorder,0,0)
    Player(1)\y + 3
  EndIf
  If SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBorder1,0,475)
    Player(1)\y - 3
  EndIf
  
  If circlex < 0
    StartTime = ElapsedMilliseconds() ; record start time
    PlaySound (#Sound0)
    circlex = 320
    circley = Random (475)
    Score2 + 1
  EndIf
  
  
  
  
  If circlex > 635
    StartTime = ElapsedMilliseconds() ; record start time
    PlaySound (0)
    circlex = 320
    circley = Random (480)
  EndIf
    
EndProcedure


OpenWindow (#Win,20,30,640,480,"Pong",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
OpenWindowedScreen (WindowID (#Win),0,0,WindowWidth (#Win),WindowHeight (#Win),0,0,0)

LoadSprite (#SpriteBat,zieg + "bat.png")
CopySprite (#SpriteBat,#SpriteBat1)
LoadSprite (#SpriteBall,zieg + "ball.png")
LoadSprite (#SpriteBorder,zieg + "Border.png")
CopySprite (#SpriteBorder,#SpriteBorder1)
LoadSound (#Sound0,zieg + "Explosion.wav")
LoadSound (#Sound1,zieg + "Lazer.wav")

Procedure Loop ()

Shared StartTime.i

Repeat
  we = WindowEvent ()
  ExamineKeyboard ()
  Game()
  Select we
    Case #PB_Event_CloseWindow
      End
  EndSelect
  FlipBuffers ()
Until KeyboardPushed (1)
EndProcedure

Loop ()

Re: Real time in games

Posted: Tue Jul 03, 2012 5:52 pm
by IdeasVacuum
The folder zieg.s should not be hard-coded like that because the Program Files folder name is different, depending on the OS and whether it is 32 or 64bits. You have got a Player 0 a Player 1 and a Player 2 - I suggest you just have Player 1 and Player 2, the code is then easier to understand. Global Dim Player.player(2). What does your Sprite 'SpriteBorder' look like? and it's Size? Can't test the code without it......

Re: Real time in games

Posted: Tue Jul 03, 2012 5:58 pm
by J@ckWhiteIII
Gonna include SysInfo ^^

Border: (w=640,h=5)

Re: Real time in games

Posted: Tue Jul 03, 2012 6:16 pm
by IdeasVacuum
.... looks and works great in a Window :D

Code: Select all

EnableExplicit
UsePNGImageDecoder()

Enumeration
  #Win
  #Sound0
  #Sound1
  #SpriteBorder
  #SpriteBat
  #SpriteBat1
  #SpriteBall
  #SpriteBorder1
EndEnumeration

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

Global we.i
Global Score1.i = 0
Global Score2.i = 0
Global zieg.s = "C:\Program Files\ProgMaster\Pong\"
Global circlex.i = 320
Global circley.i = Random(480)
Global movex.i = 2
Global movey.i = 2
Global PlayerTwo.i = 2
Global pseudoy.i
Global Block.i = 0
Global StartTime.i
Global pseudomovex.i
Global pseudomovey.i
Global Blocky.i
Global Timerlol.i

Structure player
  x.i
  y.i
EndStructure

Global Dim Player.player(1)

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

Player(1)\x = 635
Player(1)\y = 200

OpenWindow (#Win,20,30,640,480,"Pong",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
OpenWindowedScreen (WindowID (#Win),0,0,WindowWidth (#Win),WindowHeight (#Win),0,0,0)

LoadSprite(#SpriteBat,zieg + "bat.png")
CopySprite(#SpriteBat,#SpriteBat1)
LoadSprite(#SpriteBall,zieg + "ball.png")
LoadSprite(#SpriteBorder,zieg + "Border.png")
CopySprite(#SpriteBorder,#SpriteBorder1)
LoadSound(#Sound0,zieg + "Explosion.wav")
LoadSound(#Sound1,zieg + "Lazer.wav")

Procedure Game ()
  Shared Blocky
  ClearScreen (0)
  StartDrawing (ScreenOutput ())
  DrawText (20,20,Str (Score1),RGB(255,255,255),0)
  DrawText (500,20,Str (Score2),RGB(255,255,255),0)
  DrawText (380,20,Str ((ElapsedMilliseconds () - StartTime) / 1000),RGB(255,255,255),0)
  If PlayerTwo = -2
    DrawText (300,20,"Player 2",RGB(255,255,255),0)
  EndIf
  StopDrawing ()
 
  DisplayTransparentSprite (#SpriteBall,circlex,circley)
  DisplayTransparentSprite (#SpriteBat,Player(0)\x,Player(0)\y)
  DisplayTransparentSprite (#SpriteBat1,Player(1)\x,Player(1)\y)
  DisplayTransparentSprite (#SpriteBorder,0,0)
  DisplayTransparentSprite (#SpriteBorder1,0,475)
 
  circlex + movex
  circley + movey
 
  If SpritePixelCollision(#SpriteBat,Player(0)\x,Player(0)\y,#SpriteBall,circlex,circley) Or SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBall,circlex,circley)
    movex * -1
    If Block < 1
      PlaySound(#Sound1)
    EndIf
    Block = 30
  EndIf
 
  If SpritePixelCollision (#SpriteBall,circlex,circley,#SpriteBorder,0,0)
    movey * -1
    PlaySound(#Sound1)
  ElseIf SpritePixelCollision (#SpriteBall,circlex,circley,#SpriteBorder1,0,475)
    movey * -1
    PlaySound(#Sound1)
  EndIf
 
  If KeyboardPushed (#PB_Key_Up)
    Player(1)\y - 3
  ElseIf KeyboardPushed (#PB_Key_Down)
    Player(1)\y + 3
  EndIf
 
  If KeyboardPushed (#PB_Key_F2) And Block < 1
    PlayerTwo * -1
    Block = 30
  EndIf
 
  If Block > 0
    Block - 1
  EndIf
 
 
  If PlayerTwo = 2
    If KeyboardPushed (#PB_Key_W)
      Player(0)\y - 3
    ElseIf KeyboardPushed (#PB_Key_S)
      Player(0)\y + 3
    EndIf
    If SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBorder1,0,475)
      Player(1)\y - 3
    EndIf
    If SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBorder,0,0)
      Player(1)\y + 3
    EndIf
  Else
    If movex < 0
      pseudoy = circley - 30
      If Player(0)\y <> pseudoy
        If Player(0)\y < pseudoy
          Player(0)\y + 2
        Else
          Player(0)\y - 2
        EndIf
      EndIf
    EndIf   
  EndIf

  If SpritePixelCollision (#SpriteBat,Player(0)\x,Player(0)\y,#SpriteBorder,0,0)
    Player(0)\y + 2
  EndIf
  If SpritePixelCollision (#SpriteBat,Player(0)\x,Player(0)\y,#SpriteBorder1,0,475)
    Player(0)\y - 2
  EndIf
  If SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBorder,0,0)
    Player(1)\y + 3
  EndIf
  If SpritePixelCollision (#SpriteBat1,Player(1)\x,Player(1)\y,#SpriteBorder1,0,475)
    Player(1)\y - 3
  EndIf
 
  If circlex < 0
    StartTime = ElapsedMilliseconds() ; record start time
    PlaySound (#Sound0)
    circlex = 320
    circley = Random (475)
    Score2 + 1
  EndIf
 
  If circlex > 635
    StartTime = ElapsedMilliseconds() ; record start time
    PlaySound(#Sound0)
    circlex = 320
    circley = Random (480)
  EndIf
   
EndProcedure

Procedure Loop()
;--------------

Shared StartTime.i

Repeat
  we = WindowEvent()
  ExamineKeyboard()
  Game()
  Select we
    Case #PB_Event_CloseWindow
      End
  EndSelect
  FlipBuffers()
Until KeyboardPushed(1)
EndProcedure

Loop()

Re: Real time in games

Posted: Tue Jul 03, 2012 6:37 pm
by IdeasVacuum
Take a look at these guys: Free Sound Effects Files

Re: Real time in games

Posted: Wed Jul 04, 2012 1:50 pm
by J@ckWhiteIII
Seems to be a great website! Thanks for that tip :D

BOOOOOOOM the CountDown works now, too. made a little mistake...:

Code: Select all

Procedure BallReset ()
  If StartTime
    If (ElapsedMilliseconds () - StartTime) / 1000 > 3
      PlaySound (#Sound1)
      StartTime = 0
      circlex = 320
      circley = Random (475)
      Blocky = 0
    EndIf
  EndIf
EndProcedure

If circlex < 0 And Blocky < 1
    StartTime = ElapsedMilliseconds() ; record start time
    PlaySound (#Sound0)
    Score2 + 1
    If Blocky < 1
      Blocky = 1
    EndIf
  EndIf 
  
  If circlex > 635 And Blocky < 1
    StartTime = ElapsedMilliseconds() ; record start time
    PlaySound (#Sound0)
    If Blocky < 1
      Blocky = 1
    EndIf
  EndIf
  
  If Blocky = 1
    BallReset ()
  EndIf  
BallReset () = CountDown

^works perfectly! Earlier, circlex and circley were put to the center of the screen before CountDown was called. This means, that CountDown was only called once..because there was no "Blocky" variable. Logic is cool xD

Re: Real time in games

Posted: Wed Jul 04, 2012 2:15 pm
by J@ckWhiteIII

Code: Select all

If movex < 0
  pseudoy = circley - 30
  If Player(0)\y <> pseudoy
    If Player(0)\y < pseudoy
      Player(0)\y + 3
    Else
      Player(0)\y - 3 
    EndIf
  EndIf
EndIf
Does anyone know why this makes the "computer bat" stop-go? it's like moving up and down a tiny bit now (I only replaced Player(0)\y + 2 with Player(0)\y + 3) when it tries to get to circley.
This only happens when the cmputer bat is moving upwards.

Re: Real time in games

Posted: Wed Jul 04, 2012 4:41 pm
by kenmo
Not sure what you mean, but maybe try this?

Code: Select all

If movex < 0
  pseudoy = circley - 30
  If Player(0)\y <> pseudoy
    If Abs(Player(0)\y - pseudoy) > 3
      If Player(0)\y < pseudoy
        Player(0)\y + 3
      Else
        Player(0)\y - 3
      EndIf
    Else
      ; If Player 0 is already within 3 units of pseudoy,
      ; move directly to pseudoy, instead of repeatedly
      ; moving forward/backwards past pseudoy and back again
      Player(0)\y = pseudoy
    EndIf
  EndIf
EndIf

Re: Real time in games

Posted: Thu Jul 05, 2012 3:02 pm
by J@ckWhiteIII
ooooooh yeah, I see what was happening. learnt something new now :D Thanks :D

Re: Real time in games

Posted: Sat Jul 07, 2012 10:27 pm
by J@ckWhiteIII
Problem: If the ball speed is increased, the computer bat will be faster, too. This would mean you cannot beat the computer.