; ; PongBox_v2: ; ; A basic virtual 3-D environment with a ball bouncing in the z-direction. ; ; To Do: Ball physics. ; ; Mark Dow 4/18/08, v1 ; Mark Dow 5/5/08, v2 ; ; There are no restrictions on the use of this program code. It's free in every sense. AppTitle "PongBox" Graphics3D( 1000, 600, 8, 2 ) SetBuffer BackBuffer() light = CreateLight() Global redlight = 255 Global bluelight = 255 Global greenlight = 255 LightColor( light, redlight, greenlight , bluelight ) camera = CreateCamera() CameraViewport( camera, 0, 0, 1000, 600 ) ;CameraFogMode ( camera, 1 ) CameraZoom ( camera, 1 ) nDistanceBackWall = 45 nDistanceSideWall = 13 nDistanceTBWall = 9 ; Create back side enBackSide = CreateCube() ScaleMesh ( enBackSide, 12, 12, 0 ) PositionEntity( enBackSide, 0, -3, nDistanceBackWall ) ; Create left side enLeftSide = CreateCube( enBackSide ) ScaleMesh ( enLeftSide, 0, 15, 25 ) PositionEntity( enLeftSide, -nDistanceSideWall, 0, 0 ) ; Create right side enRightSide = CreateCube( enBackSide ) ScaleMesh ( enRightSide, 0, 15, 25 ) PositionEntity( enRightSide, nDistanceSideWall , 0, 0 ) ; Create top side enTopSide = CreateCube( enBackSide ) ScaleMesh ( enTopSide, 20, 0, 25 ) PositionEntity( enTopSide, 0, nDistanceTBWall, 0 ) ; Create bottom side enBotSide = CreateCube( enBackSide ) ScaleMesh ( enBotSide, 20, 0, 25 ) PositionEntity( enBotSide, 0, -nDistanceTBWall, 0 ) ; Create racket enRacket = CreateCube( enBackSide ) ScaleMesh ( enRacket , 1, 1, 0 ) PositionEntity( enRacket , 0, 0, 3*nDistanceBackWall/4 ) ;enBall = LoadMesh( "hearse.3ds", enBackSide ) ;ScaleMesh( enBall, .005, .005, .005 ) enBall = CreateSphere( 12, enBackSide ) PositionEntity( enBall, 0, 0, 15 ) ScaleMesh( enBall, 1, 1, 1 ) ; Paint the top and bottom walls imBackground = LoadTexture( "swamp1.bmp" ); brushBackground = CreateBrush( 255, 255, 255) BrushTexture( brushBackground, imBackground ) PaintMesh( enTopSide, brushBackground ) PaintMesh( enBotSide, brushBackground ) ;BrushShininess( brushBackground , .8 ) ; Sound effects sndBack = LoadSound( "cat_1.wav" ); sndBack2 = LoadSound( "tiger_1.wav" ); ; Paint the ball txBall = LoadTexture( "USA.jpg" ); brushBall = CreateBrush( 255, 255, 255) BrushTexture( brushBall, txBall ) PaintMesh( enBall, brushBall ) ; Paint the background txBack = LoadTexture( "catface.jpg" ); brushBack = CreateBrush( 255, 255, 255) BrushTexture( brushBack, txBack ) PaintMesh( enBackSide, brushBack ) ; Paint the sides txSide = LoadTexture( "mazewall.jpg" ); brushSide = CreateBrush( 255, 255, 255) BrushTexture( brushSide, txSide ) PaintMesh( enRightSide, brushSide ) PaintMesh( enLeftSide, brushSide ) ; Paint the racket txRacket = LoadTexture( "USA.jpg" ); brushRacket = CreateBrush( 255, 255, 255) BrushTexture( brushRacket, txRacket ) BrushAlpha( brushRacket, .01 ) PaintMesh( enRacket, brushRacket) Global flBallPositionX# = 0 Global flBallPositionY# = 0 Global flBallPositionZ# = 15 Global flBallSpeed_Z# = .2 Global flBallSpeed_X# = 0 Global flBallSpeed_Y# = 0 TurnEntity( enBackSide, 0, 180, 0 ) ; Main loop .Main_loop While Not KeyHit(1) ; Press ESC key to exit main loop ; Move ball ; PositionEntity( enBall, -13+.025*MouseX(), 7-.020*MouseY(), flBallPositionZ# ) ;Stop ;Delay( 250 ) ;Stop If flBallPositionZ# <= 1 ; ball is past back wall flBallSpeed_Z# = -1*flBallSpeed_Z# ; flBallSpeed_Z# = flBallSpeed_Z# + Rnd(0, 10)/12.0 * flBallSpeed_Z#/Abs(flBallSpeed_Z#) ; flBallSpeed_Y# = flBallSpeed_Y# + Rnd(0, 10)/8 * flBallSpeed_Y#/Abs(flBallSpeed_Y#) If flBallPositionY# > nDistanceTBWall/2 PlaySound(sndBack) Else PlaySound(sndBack2) EndIf EndIf If flBallPositionZ# >= 30 ; ball is past front wall flBallSpeed_Z# = -1*flBallSpeed_Z# flBallSpeed_X# = Rand(-10, 10)/8.0 EndIf If Abs( flBallSpeed_X# ) > 1 flBallSpeed_X# = .8 * flBallSpeed_X# EndIf If Abs( flBallSpeed_Y# ) > 1 flBallSpeed_Y# = .8 * flBallSpeed_Y# EndIf If Abs( flBallSpeed_Z# ) > 1 flBallSpeed_Z# = .8 * flBallSpeed_Z# EndIf If flBallPositionX# >= nDistanceSideWall - 1 ; ball is past right wall flBallSpeed_X# = -1*flBallSpeed_X# EndIf If flBallPositionX# <= -nDistanceSideWall + 1 ; ball is past front wall flBallSpeed_X# = -1*flBallSpeed_X# EndIf If flBallPositionY# >= nDistanceTBWall - 1 ; ball is past right wall flBallSpeed_Y# = -1*flBallSpeed_Y# EndIf If flBallPositionY# <= -nDistanceTBWall + 1 ; ball is past front wall flBallSpeed_Y# = -1*flBallSpeed_Y# EndIf flBallPositionX# = flBallPositionX# - flBallSpeed_X# flBallPositionY# = flBallPositionY# - flBallSpeed_Y# flBallPositionZ# = flBallPositionZ# - flBallSpeed_Z# PositionEntity( enBall, flBallPositionX#, flBallPositionY#, flBallPositionZ# ) BrushAlpha( brushRacket, 1 - flBallPositionZ#/nDistanceBackWall ) PaintMesh( enRacket, brushRacket) ; move racket PositionEntity( enRacket, +13 -.025*MouseX(), 7-.020*MouseY() + 2, 3*nDistanceBackWall/4 ) ; Rotate on arrow keypress If KeyDown( 200 ) Then MoveEntity( enBall, 0, .05, 0 ) MoveEntity( enBackSide, 0, .05, 0 ) EndIf If KeyDown( 203 ) Then TurnEntity( enBall, 0, 3.0, 0 ) TurnEntity( enBackSide, 0, 3.0, 0 ) EndIf If KeyDown( 205 ) Then TurnEntity( enBall, 0, 0, -3.0 ) TurnEntity( enBackSide, 0, 0, -3.0 ) EndIf UpdateWorld RenderWorld Flip Wend ; end of main loop End ; end program