It is currently Fri May 24, 2013 1:19 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 25 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Real time in games
PostPosted: Tue Jul 03, 2012 5:23 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri May 25, 2012 7:39 pm
Posts: 183
Complete code now, weird Shared variable, some unnecessary variables.

Code:
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 ()


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Tue Jul 03, 2012 5:52 pm 
Offline
Addict
Addict

Joined: Fri Oct 23, 2009 2:33 am
Posts: 2863
Location: Wales, UK
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......

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Tue Jul 03, 2012 5:58 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri May 25, 2012 7:39 pm
Posts: 183
Gonna include SysInfo ^^

Border: (w=640,h=5)


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Tue Jul 03, 2012 6:16 pm 
Offline
Addict
Addict

Joined: Fri Oct 23, 2009 2:33 am
Posts: 2863
Location: Wales, UK
.... looks and works great in a Window :D
Code:
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()

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Tue Jul 03, 2012 6:37 pm 
Offline
Addict
Addict

Joined: Fri Oct 23, 2009 2:33 am
Posts: 2863
Location: Wales, UK
Take a look at these guys: Free Sound Effects Files

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Wed Jul 04, 2012 1:50 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri May 25, 2012 7:39 pm
Posts: 183
Seems to be a great website! Thanks for that tip :D

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

Code:
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


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Wed Jul 04, 2012 2:15 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri May 25, 2012 7:39 pm
Posts: 183
Code:
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.


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Wed Jul 04, 2012 4:41 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 932
Location: New York
Not sure what you mean, but maybe try this?

Code:
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


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Thu Jul 05, 2012 3:02 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri May 25, 2012 7:39 pm
Posts: 183
ooooooh yeah, I see what was happening. learnt something new now :D Thanks :D


Top
 Profile  
 
 Post subject: Re: Real time in games
PostPosted: Sat Jul 07, 2012 10:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri May 25, 2012 7:39 pm
Posts: 183
Problem: If the ball speed is increased, the computer bat will be faster, too. This would mean you cannot beat the computer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 25 posts ]  Go to page Previous  1, 2

All times are UTC + 1 hour


Who is online

Users browsing this forum: Crusiatus Black and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye