I was playing around about to start a Brick Breaker project and before I start jumping off the deep end with it. I thought about sharing the simple start I am off too....For anyone to learn from...or for people to tell me I could do something differently
// Project: Test Project - Break Buster
// Created: 2015-09-23
// set window properties
SetWindowTitle( "Test Project - Pong" )
SetWindowSize( 1024, 768, 0 )
//Set the Physics Variables//
SetPhysicsScale(0.05)
SetPhysicsGravity(0, 0)
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
//Crate Top Boarder//
TopSprite = CreateSprite(0)
SetSpriteSize(TopSprite,1024,25)
SetSpriteColor(TopSprite,0,0,255,255)
//Create Left Boarder//
LeftSprite = CreateSprite(0)
SetSpriteSize(LeftSprite,25,768)
SetSpriteColor(LeftSprite,0,0,255,255)
//Create Bottom Boarder//
BottomSprite = CreateSprite(0)
SetSpriteSize(BottomSprite,1024,25)
SetSpritePosition(BottomSprite,0,743)
SetSpriteColor(BottomSprite,0,0,255,255)
//Create Right Boarder//
RightSprite = CreateSprite(0)
SetSpriteSize (RightSprite, 25,768)
SetSpritePosition(RightSprite,999,0)
SetSpriteColor(RightSprite,0,0,255,255)
//Create Paddle(Middle)//
Paddle = CreateSprite(0)
SetSpriteSize(Paddle, 50,15)
SetSPritePosition(Paddle,500,700)
SetSpriteColor(Paddle,0,255,0,255)
//Create Paddle(Left)//
Paddle1 = CreateSprite(0)
SetSpriteSize(Paddle1, 25,15)
SetSPritePosition(Paddle1,475,700)
SetSpriteColor(Paddle1,255,0,0,255)
//Create Paddle(right)//
Paddle2 = CreateSprite(0)
SetSpriteSize(Paddle2, 25,15)
SetSPritePosition(Paddle2,550,700)
SetSpriteColor(Paddle2,255,0,0,255)
//Bricks
Global Dim Brick[100]
//Define the ball direction
Global BallDirection = 400
//Create Random Bricks all over the place
For RandomBrick = 1 to 60
Brick[RandomBrick] = CreateSprite(0)
SetSpriteSize(Brick[RandomBrick], 30,15)
SetSPritePosition (Brick[RandomBrick],Random(25,999), Random (25,400))
SetSpriteColor(Brick[RandomBrick], Random(0,255),Random(0,255),Random(0,255),255)
Next RandomBrick
//Create the Ball//
SquareBall = CreateSprite(0)
SetSPriteSize(SquareBall,20,20)
SetSpriteColor(SquareBall,255,255,0,255)
SetSpritePhysicsOn(SquareBall,2)
SetSpritePosition(SquareBall,515,665)
//Check to see if a game is playing//
Playing = 0
do
//Get Paddle X# so we can update the Paddle positions//
PaddleX# = GetSpriteX(Paddle)
Paddle1X# = GetSpriteX(Paddle1)
Paddle2X# = GetSpriteX(Paddle2)
//Get the Middle Paddle Y#//
PaddleY# = GetSpriteY(Paddle)
//Check to see if game is playing....If not wait for Pointer pressed to start the ball movement
If GetPointerPressed() = 1 and Playing = 0
SetSpritePhysicsVelocity(Squareball, -100, -400)
Playing = 1
endif
//If Playing =1 give the paddle movement so the player can hit the ball
If Playing = 1
SetSpritePosition(Paddle,GetPointerX(),PaddleY#)
SetSpritePosition(Paddle1,GetPointerX()-25,PaddleY#)
SetSpritePosition(Paddle2,GetPointerX()+50,PaddleY#)
endif
//Check for Collision with Paddle
If GetSpriteCollision(Paddle,SquareBall) = 1
BallDirection = BallDirection * - 1
SetSpritePhysicsVelocity(Squareball, GetSpritePhysicsVelocityX(SquareBall), BallDirection)
endif
//Check for Collision with Paddle 1 and Add some XVelocity to change BallX Movement// <-----Reason for not using BallDirection as YVelocity Variable is because sometimes the ball is confused when touching 2 sprites at the same time
If GetSpriteCollision(Paddle1,SquareBall) = 1
BallDirection = BallDirection * - 1
SetSpritePhysicsVelocity(Squareball, -100, -400)
endif
//Check For Collision with paddle 2 and add some XVelocity to change BallX Movement// <-----Reason for not using BallDirection as YVelocity Variable is because sometimes the ball is confused when touching 2 sprites at the same time
If GetSpriteCollision(Paddle2,SquareBall) = 1
SetSpritePhysicsVelocity(Squareball, 100, -400)
endif
//Check collision with bricks//
For RandomBrick = 1 to 60
If GetSpriteCollision(Brick[RandomBrick], SquareBall) = 1
BallDirection = BallDirection * - 1
SetSpritePhysicsVelocity(Squareball, GetSpritePhysicsVelocityX(SquareBall), BallDirection)
DeleteSprite (Brick[RandomBrick])
endif
Next RandomBrick
///Check Collisions with all the boarders//
If GetSpriteCollision(Squareball,TopSprite) = 1
BallDirection = BallDirection * - 1
SetSpritePhysicsVelocity(Squareball, GetSpritePhysicsVelocityX(SquareBall), BallDirection)
ElseIf GetSpriteCollision(SquareBall,BottomSprite) = 1
BallDirection = BallDirection * - 1
SetSpritePhysicsVelocity(Squareball, GetSpritePhysicsVelocityX(SquareBall), BallDirection)
ElseIf GetSpriteCollision(SquareBall,RightSprite) = 1
SetSpritePhysicsVelocity(Squareball, GetSpritePhysicsVelocityX(SquareBall) - 100, GetSpritePhysicsVelocityY(SquareBall))
ElseIf GetSpriteCollision (Squareball,LeftSprite) = 1
SetSpritePhysicsVelocity(Squareball, GetSpritePhysicsVelocityX(SquareBall) + 100, GetSpritePhysicsVelocityY(SquareBall))
endif
//Double Check Ball Direction//
If GetSpritePhysicsVelocityY(Squareball) = 400
BallDirection = 400
Elseif GetSpritePhysicsVelocityY(SquareBall) = - 400
BallDirection = -400
endif
Sync()
loop
No Media is required...Give it a test and see what you think.
Feel free to take the code and run with it in any direction your heart desires.
The reason the paddle is made up of 3 sprites is because I wanted to have some control over the Balls X-Velocity Movement.
I tried to manage this using Friction on one paddle but failed
Beta Test Age of Knights:https://play.google.com/apps/testing/com.CrazyProgrammerProductions.my_AgeOfKnights
Download JellyFish Dive:https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_JellyFishSwim