Page 1 of 2

BreakOut example

Posted: Fri Apr 25, 2014 3:19 pm
by Comtois
[Space] = Start game

Code: Select all

IncludeFile #PB_Compiler_Home + "Examples/3D/Screen3DRequester.pb"

#COL_Sphere   = 1 << 6
#COL_Wall     = 1 << 7
#COL_Brique   = 1 << 8
#COL_BriqueT  = 1 << 9 ; Brique touchée
#COL_Raquette = 1 << 10

#SphereCollidesWith = #COL_WALL | #COL_Brique | #COL_Raquette
#WallCollidesWith = #COL_Sphere | #COL_BriqueT
#BriqueCollidesWith = #COL_Sphere
#BriqueTCollidesWith = #COL_Raquette
#RaquetteCollidesWith = #COL_Sphere | #COL_BriqueT

#CameraSpeed = 1

Structure Brique
  no.i
  Time.i
EndStructure  

Define.f KeyX, KeyY, MouseX, MouseY
Define.i Start, Score 
NewList BriquesTouchees.Brique()

Macro CreateCadre(x, y, z, sx, sy, sz, m, r, f)
  Cadre = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), x, y, z)
  ScaleEntity(Cadre, sx, sy, sz)
  EntityPhysicBody(Cadre, #PB_Entity_BoxBody, m, r, f)
  SetEntityCollisionFilter(Cadre, #COL_Wall, #WallCollidesWith) 
EndMacro

Macro SupprimeBriquesTouchees
  ForEach BriquesTouchees()
    FreeEntity(BriquesTouchees()\no)
    DeleteElement(BriquesTouchees())
  Next 
EndMacro

Macro Briques
  n = 0
  For j=0 To 3
    For i = 0 To 10
      n + 1
      Brique = CreateEntity(n, MeshID(1), MaterialID(0), 20 - i * 4, 80 - j * 4, 0)
      ScaleEntity(n, 1.8, 1.8, 1.8)
      EntityPhysicBody(n, #PB_Entity_BoxBody, 0, 1, 0)
      SetEntityCollisionFilter(n, #COL_Brique, #BriqueCollidesWith) 
    Next
  Next
  
  NbBriques = n
  
EndMacro

Macro init
  SupprimeBriquesTouchees
  MoveEntity(Sphere, 0, 50, 0, #PB_Absolute)
  MoveEntity(Raquette, 0, 40, 0, #PB_Absolute)
  Start = 0
EndMacro  

If InitEngine3D(3)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/fonts", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;WorldShadows(#PB_Shadow_Modulative)
    WorldGravity(0)
    
    ;-Materials
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    
    GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
    GetScriptMaterial(2, "Color/Blue")
    GetScriptMaterial(3, "Scene/GroundBlend")
    GetScriptMaterial(4, "Color/Yellow")
    MaterialBlendingMode(4, #PB_Material_AlphaBlend)
    SetMaterialColor(4, #PB_Material_DiffuseColor, RGBA(255, 255, 0, 155))
    GetScriptMaterial(5, "Color/Red")
    GetScriptMaterial(6, "Color/Green")
    
    ;-Mesh
    CreateCube(1, 1.0)
    CreateSphere(2, 1, 30, 30)
    CreateCylinder(3, 0.5, 2) 
    
    CreateText3D(0, "123")
    Text3DColor(0, RGBA(255, 255, 0, 255))
    Text3DAlignment(0, #PB_Text3D_Left | #PB_Text3D_Bottom)
    
    CreateNode(0, 50, 92, 0)
    AttachNodeObject(0, Text3DID(0))
    ScaleText3D(0, 5, 5, 1)
    
    ;-Sphere
    Sphere = CreateEntity(#PB_Any, MeshID(2), MaterialID(4), 0, 41.5, 0)
    EntityPhysicBody(Sphere, #PB_Entity_SphereBody, 1, 1, 0)
    EntityLinearFactor(Sphere, 1, 1, 0)
    SetEntityCollisionFilter(Sphere, #COL_Sphere, #SphereCollidesWith) 
    
    ;-Cadre
    CreateCadre(  0,  0, 0, 50, 1,  4, 0, 1.0, 0)
    CreateCadre(  0, 90, 0, 50, 1,  4, 0, 1.0, 0)
    CreateCadre(-25, 45, 0,  1, 90, 4, 0, 1.0, 0)
    CreateCadre( 25, 45, 0,  1, 90, 4, 0, 1.0, 0) 
    
    ;-Briques
    Briques
    
    ;-Raquette
    Raquette = CreateEntity(#PB_Any, MeshID(1), MaterialID(5), 0, 40, 0)
    ScaleEntity(Raquette, 6, 1, 4)
    EntityPhysicBody(Raquette, #PB_Entity_BoxBody, 0, 1, 0)
    SetEntityCollisionFilter(Raquette, #COL_Raquette, #RaquetteCollidesWith) 
    
    ;-Camera
    CreateCamera(0, 0,  0, 100, 100)
    MoveCamera(0, 0, 30, -47, #PB_Absolute)
    CameraLookAt(0, 0,  60, 0)
    
    ;-Skybox
    SkyBox("desert07.jpg")
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 100, 800, -500)
    AmbientColor(RGB(0, 0, 0))
    
    Init
    
    Repeat
      Screen3DEvents()
      
      ExamineMouse()
      
      If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Left) And EntityX(Raquette) < 21.5
          MoveEntity(Raquette, EntityX(Raquette)+0.5, EntityY(Raquette), EntityZ(Raquette),#PB_Absolute)
        ElseIf KeyboardPushed(#PB_Key_Right) And EntityX(Raquette) > -21.5
          MoveEntity(Raquette, EntityX(Raquette)-0.5, EntityY(Raquette), EntityZ(Raquette),#PB_Absolute)
        EndIf 
        
        If KeyboardReleased(#PB_Key_Space) And Start = 0
          ApplyEntityImpulse(Sphere,  Random(40)-20, -70, 0)
          Start = 1
        EndIf     
        
      EndIf
      
      If Abs(GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityY)) < 3 And GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityX) <> 0
        ApplyEntityImpulse(Sphere,  0, GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityY) * 3, 0)
      EndIf
      
      If Abs(GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityX)) < 3 And GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityY) <> 0
        ApplyEntityImpulse(Sphere, GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityX) * 3, 0, 0)
      EndIf  
      
      SetEntityAttribute(Sphere, #PB_Entity_ForceVelocity, 60)
      
      If ExamineWorldCollisions(#False)
        
        While NextWorldCollision()
          
          If SecondWorldCollisionEntity() <= n
            
            x.f = EntityX(SecondWorldCollisionEntity())
            y.f = EntityY(SecondWorldCollisionEntity())
            z.f = EntityZ(SecondWorldCollisionEntity())
            
            AddElement(BriquesTouchees())
            BriquesTouchees()\no = CreateEntity(#PB_Any, MeshID(3), MaterialID(6), x, y, z)
            ;ScaleEntity(BriquesTouchees()\no, 1.8, 1.8, 1.8)
            EntityPhysicBody(BriquesTouchees()\no, #PB_Entity_CylinderBody, 1, 1, 0)
            EntityLinearFactor(BriquesTouchees()\no, 1, 1, 0)
            SetEntityCollisionFilter(BriquesTouchees()\no, #COL_BriqueT, #BriqueTCollidesWith) 
            BriquesTouchees()\time = ElapsedMilliseconds()
            
            ; x.f = GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityX)
            ; y.f = GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityY)
            ; z.f = GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityZ)
            ApplyEntityImpulse(BriquesTouchees()\no, 0,-15,0)
            FreeEntity(SecondWorldCollisionEntity())
            NbBriques - 1
          ElseIf (FirstWorldCollisionEntity() = Raquette And SecondWorldCollisionEntity()<>Sphere) Or 
                 (FirstWorldCollisionEntity() <> Sphere And SecondWorldCollisionEntity() = Raquette)
            
            ForEach BriquesTouchees()
              If BriquesTouchees()\no = SecondWorldCollisionEntity() Or BriquesTouchees()\no = FirstWorldCollisionEntity()
                FreeEntity(BriquesTouchees()\no)
                DeleteElement(BriquesTouchees())
                Score + 1
                Break
              EndIf  
              
            Next
          EndIf
        Wend
        
      EndIf
      
      ForEach BriquesTouchees()
        SetEntityAttribute(BriquesTouchees()\no, #PB_Entity_ForceVelocity, 15)
        RotateEntity(BriquesTouchees()\no,1,1,1, #PB_Relative)
        If EntityY(BriquesTouchees()\no) < 35 Or (ElapsedMilliseconds() - BriquesTouchees()\Time > 60000)
          FreeEntity(BriquesTouchees()\no)
          DeleteElement(BriquesTouchees())
        EndIf  
        
      Next
      
      If EntityY(Sphere) < 35
        Init
      EndIf
      
      If NbBriques <= 0
        Score  = 0
        Briques
        init
      EndIf  
      
      Text3DCaption(0, Str(Score))
      
      RenderWorld(10)
      
      Screen3DStats()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Re: BreakOut example

Posted: Fri Apr 25, 2014 3:56 pm
by IdeasVacuum
Nice! :mrgreen:

Re: BreakOut example

Posted: Sat Apr 26, 2014 4:36 am
by applePi
thanks for the demo. a possible variations on the game is like that in the old dos game http://www.robots.ox.ac.uk/~north/Paranoid.html (can run on winXP), the Raquette (racket) will be taller or shorter depends on touching a specific falling bricks, some bricks are killers and some are good bricks.

Re: BreakOut example

Posted: Sun Apr 27, 2014 3:22 pm
by box_80
Always glad to see examples posted. I learn a lot from them. :D

Re: BreakOut example

Posted: Sun Apr 27, 2014 7:35 pm
by davido
@Comtois ,Thank you for the example.
Works well on Mac Book Pro.
Crashes badly on Windows 7-64 for some reason. Does anyone else have problems on Windows 7?

Re: BreakOut example

Posted: Sun Apr 27, 2014 8:15 pm
by MightyMAC
Nice example!

No problems here on Windows 7 x64.

Re: BreakOut example

Posted: Mon Apr 28, 2014 12:26 am
by TassyJim
PB 5.22 x86 running on Windows 64 bit.
Crashes at line 160 'RenderWorld(10)' with invalid memory access.

Jim

Re: BreakOut example

Posted: Mon Apr 28, 2014 7:15 am
by Comtois
TassyJim wrote:PB 5.22 x86 running on Windows 64 bit.
Crashes at line 160 'RenderWorld(10)' with invalid memory access.

Jim
Try to comment this line

Code: Select all

WorldShadows(#PB_Shadow_Modulative)

Re: BreakOut example

Posted: Mon Apr 28, 2014 8:18 am
by TassyJim
Comtois wrote: Try to comment this line

Code: Select all

WorldShadows(#PB_Shadow_Modulative)
That fixed it!

Jim

Re: BreakOut example

Posted: Mon Apr 28, 2014 5:42 pm
by DK_PETER
Really nice example, Comtois. ;-)
Thank you.

Re: BreakOut example

Posted: Mon Apr 28, 2014 9:13 pm
by davido
@TassyJim, Thank you for finding the error.

Unfortunately I couldn't as it produced a complete freeze and all I could do was re-boot.

@Comtois, Thank you for the fix. Works great, now. :D

Re: BreakOut example

Posted: Tue Apr 29, 2014 6:30 pm
by Samuel
Very cool example. It brings back a lot of good memories.

Re: BreakOut example

Posted: Wed Jun 04, 2014 9:26 pm
by Comtois
Updated for 5.30 beta 2. Example use SetEntityCollisionFilter()

Re: BreakOut example

Posted: Thu Jun 05, 2014 10:01 am
by Fig
That's a pity, you should keep the 2 versions because i don't want to update to beta just to test your code :?

Re: BreakOut example

Posted: Thu Jun 05, 2014 12:47 pm
by applePi
i have found the older version works on 5.22.
i hope there is no noticeable changes while i experimenting with it

Code: Select all

IncludeFile #PB_Compiler_Home + "Examples/3D/Screen3DRequester.pb"

Macro CreateCadre(x, y, z, sx, sy, sz, m, r, f)
  Cadre = CreateEntity(#PB_Any, MeshID(1), MaterialID(2), x, y, z)
  ScaleEntity(Cadre, sx, sy, sz)
  EntityPhysicBody(Cadre, #PB_Entity_BoxBody, m, r, f)
EndMacro

Structure Brique
  no.i
  x.f
  y.f
  z.f
EndStructure  

#CameraSpeed = 1

Define.f KeyX, KeyY, MouseX, MouseY
Define.i Start 
NewList BriquesTouchees.Brique()

If InitEngine3D()
  
  
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
    
    WorldShadows(#PB_Shadow_Modulative)
    WorldGravity(0)
    
    ;-Materials
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
    GetScriptMaterial(2, "Color/Blue")
    GetScriptMaterial(3, "Scene/GroundBlend")
    
    ;-Mesh
    CreateCube(1, 1.0)
    CreateSphere(2, 1.2, 30, 30)
    
    
    ;-Entity
    Sphere = CreateEntity(#PB_Any, MeshID(2), MaterialID(1), 0, 50, 0)
    EntityPhysicBody(Sphere, #PB_Entity_SphereBody, 1, 1, 0)
    EntityLinearFactor(Sphere, 1, 1, 0)
    
    ; Cadre
    CreateCadre(  0,  0, 0, 50, 1,  4, 0, 1.0, 0)
    CreateCadre(  0, 90, 0, 50, 1,  4, 0, 1.0, 0)
    CreateCadre(-25, 45, 0,  1, 90, 4, 0, 1.0, 0)
    CreateCadre( 25, 45, 0,  1, 90, 4, 0, 1.0, 0) 
    
    
    ;Briques
    For j=0 To 3
      For i = 0 To 10
        n + 1
        Brique = CreateEntity(n, MeshID(1), MaterialID(0), 20 - i * 4, 80 - j * 4, 0)
        ScaleEntity(n, 2, 2, 2)
        EntityPhysicBody(n, #PB_Entity_BoxBody, 0, 1, 0)
      Next
    Next
    
    ;Raquette
    Raquette = CreateEntity(#PB_Any, MeshID(1), MaterialID(1), 0, 40, 0)
    ScaleEntity(Raquette, 20, 1, 2)
    EntityPhysicBody(Raquette, #PB_Entity_BoxBody, 0, 1, 0)
    
    ;-Camera
    CreateCamera(0, 0,  0, 100, 100)
    MoveCamera(0, 0, 30, -47, #PB_Absolute)
    CameraLookAt(0, 0,  60, 0)
    
    ;-Skybox
    SkyBox("desert07.jpg")
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 100, 800, -500)
    AmbientColor(RGB(20, 20, 20))
    WorldGravity(-20)
    Repeat
      Screen3DEvents()
      
      ExamineMouse()
      
      If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Left) And EntityX(Raquette) < 22
          MoveEntity(Raquette, EntityX(Raquette)+0.5, EntityY(Raquette), EntityZ(Raquette),#PB_Absolute)
        ElseIf KeyboardPushed(#PB_Key_Right) And EntityX(Raquette) > -22
          MoveEntity(Raquette, EntityX(Raquette)-0.5, EntityY(Raquette), EntityZ(Raquette),#PB_Absolute)
        EndIf 
        
        If KeyboardReleased(#PB_Key_Space) And Start = 0
          ApplyEntityImpulse(Sphere,  Random(20)-10, -60, 0)
          Start = 1
        EndIf     
        
      EndIf
      
      If Abs(GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityY)) < 3 And GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityX) <> 0
        ApplyEntityImpulse(Sphere,  0, GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityY) * 3, 0)
      EndIf
      
      If Abs(GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityX)) < 3 And GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityY) <> 0
        ApplyEntityImpulse(Sphere, GetEntityAttribute(Sphere, #PB_Entity_LinearVelocityX) * 3, 0, 0)
      EndIf  
      
      SetEntityAttribute(Sphere, #PB_Entity_MaxVelocity, 60)
      
      
      If ExamineWorldCollisions(#False)
        
        While NextWorldCollision()
          
          If SecondWorldCollisionEntity() <= n
            
            x.f = EntityX(SecondWorldCollisionEntity())
            y.f = EntityY(SecondWorldCollisionEntity())
            z.f = EntityZ(SecondWorldCollisionEntity())
            
            AddElement(BriquesTouchees())
            BriquesTouchees()\no = CreateEntity(#PB_Any, MeshID(1), MaterialID(0), x, y, z)
            ScaleEntity(BriquesTouchees()\no, 2, 2, 2)
            BriquesTouchees()\x = (Random(200)-100)/100.0
            BriquesTouchees()\y = (Random(200)-100)/100.0
            BriquesTouchees()\z = (Random(200)-100)/100.0
            
            FreeEntity(SecondWorldCollisionEntity())
            
          EndIf
        Wend
        
      EndIf
      
      ForEach BriquesTouchees()
        RotateEntity(BriquesTouchees()\no, BriquesTouchees()\x, BriquesTouchees()\y, BriquesTouchees()\z, #PB_Relative)
        MoveEntity(BriquesTouchees()\no, 0, -0.3, 0, #PB_Relative)
        
        If EntityY(BriquesTouchees()\no) < 5
          FreeEntity(BriquesTouchees()\no)
          DeleteElement(BriquesTouchees())
        EndIf  
        
      Next
      
      If EntityY(Sphere) < 35
        MoveEntity(Sphere, 0, 50, 0, #PB_Absolute)
        Start = 0
      EndIf
      
      RenderWorld(10)
      Screen3DStats()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End