So I decided to do a Breakout game for practice.
At first I went with what I knew, moving everything using X and Y coordinates, but the problem here is that the ball will always follow the same path. The only challenge for the player is to catch the ball. I remember playing Breakout games where you could affect the angle of the ball depending how and where it hit on the paddle, so I tried to use physics.
// Project: Blockout
// Created: 2016-09-26
// set window properties
SetWindowTitle( "Blockout" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
ballXPos as integer
ballYPos as integer
ballXSpeed as integer
ballYSpeed as integer
paddleCollisionFlag as integer
Dim blockXPos[5] as integer
Dim blockYPos[5] as integer
ballXPos = 300
ballYPos = 500
ballXSpeed = 100
ballYSpeed = 100
paddleCollisionFlag = 1
LoadImage(1, "paddle.png")
LoadImage(2, "ball.png")
LoadImage(3, "block.png")
CreateSprite(1, 1) //Paddle
CreateSprite(2, 2) //Ball
SetSpritePosition(2, ballXPos, ballYPos)
For x = 11 to 15 //Creates blocks
CreateSprite(x, 3) //Blocks
blockXPos[x - 10] = (70 + (140 * (x -10)))
blockYPos[x - 10] = 120
SetSpritePosition(x, blockXPos[x - 10], blockYPos[x - 10])
Next x
do
`Inc ballXPos, ballXSpeed
`Inc ballYPos, ballYSpeed
SetSpritePosition(1, GetPointerX(), 700)
SetSpritePosition(2, ballXPos, ballYPos)
If ((ballXPos + GetSpriteWidth(2)) >= GetScreenBoundsRight()) //Detects collision between ball and window borders/Detects collision between ball and Right Window Border.
ballXSpeed = (ballXSpeed * -1)
paddleCollisionFlag = 1
ElseIf ((ballYPos + GetSpriteHeight(2)) >= GetScreenBoundsBottom()) //Detecs collision between ball and Bottom Window Border.
ballYSpeed = (ballYSpeed * -1)
paddleCollisionFlag = 1
ElseIf (ballXPos <= GetScreenBoundsLeft()) //Detects collision between ball and Left Window Border.
ballXSpeed = (ballXSpeed * -1)
paddleCollisionFlag = 1
ElseIf (ballYPos <= GetScreenBoundsTop()) //Detects collision between ball and Top Window Border.
ballYSpeed = (ballYSpeed * -1)
paddleCollisionFlag = 1
Endif
If paddleCollisionFlag = 1 //Detects collision between ball and paddle.
If GetSpriteCollision(1, 2) = 1
If (ballYPos + GetSpriteHeight(2)) > 710
If ((ballXPos + GetSpriteWidth(2)) >= (GetPointerX() + 10))
ballXSpeed = ballXSpeed * -1
paddleCollisionFlag = 0
Endif
Else
ballYSpeed = ballYSpeed * -1
paddleCollisionFlag = 0
Endif
Endif
Endif
For x = 11 to 15 //Collision between ball and blocks.
If GetSpriteCollision(2, x) = 1
DeleteSprite(x)
If (ballYPos < blockYPos[x - 10])
ballYSpeed = (ballYSpeed * -1)
ElseIf (ballYPos > (blockYPos[x - 10] + GetSpriteHeight(x)))
ballYSpeed = (ballYSpeed * -1)
ElseIf (ballXPos < blockXPos[x - 10])
ballXSpeed = (ballXSpeed * -1)
ElseIf (ballXPos > (blockXPos[x - 10] + GetSpriteWidth(x)))
ballXSpeed = (ballXSpeed * -1)
Endif
Endif
Next x
Sync()
loop
I am still figuring them out but so far the problems are that often times the ball will roll on the paddle rather than bounce off and it refuses to bounce off the walls anymore.
// Project: Blockout
// Created: 2016-09-26
// set window properties
SetWindowTitle( "Blockout" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
ballXPos as integer
ballYPos as integer
ballXSpeed as integer
ballYSpeed as integer
paddleCollisionFlag as integer
Dim blockXPos[5] as integer
Dim blockYPos[5] as integer
ballXPos = 300
ballYPos = 500
ballXSpeed = 100
ballYSpeed = 100
paddleCollisionFlag = 1
LoadImage(1, "paddle.png")
LoadImage(2, "ball.png")
LoadImage(3, "block.png")
CreateSprite(1, 1) //Paddle
CreateSprite(2, 2) //Ball
SetSpritePosition(2, ballXPos, ballYPos)
SetSpritePhysicsOn(1, 1)
SetSpritePhysicsOn(2, 2)
For x = 11 to 15 //Creates blocks
CreateSprite(x, 3) //Blocks
blockXPos[x - 10] = (70 + (140 * (x -10)))
blockYPos[x - 10] = 120
SetSpritePosition(x, blockXPos[x - 10], blockYPos[x - 10])
SetSpritePhysicsOn(x, 1)
Next x
SetSpriteShape(1, 2)
SetSpriteShape(2, 1)
SetSpritePhysicsVelocity(2, 100, 100)
SetSpritePhysicsForce(2, 310, 510, 1000, 1000)
SetSpritePhysicsRestitution(2, 1)
SetSpritePhysicsMass(2, 1000)
SetPhysicsGravity(0, 0)
CreatePhysicsForce(300, 500, 1000, 0, 100, 0)
do
SetSpritePosition(1, GetPointerX(), 700)
SetSpritePhysicsVelocity(2, ballXSpeed, ballYSpeed)
`If GetPointerPressed() = 1
` ballXSpeed = ballXSpeed * -1
`Endif
`If GetSpriteCollision(1, 2) = 1
`SetSpritePhysicsImpulse(1, GetPointerX(), GetPointerY(), 100, 100)
`Endif
If ((ballXPos + GetSpriteWidth(2)) >= GetScreenBoundsRight()) //Detects collision between ball and window borders/Detects collision between ball and Right Window Border.
ballXSpeed = (ballXSpeed * -1)
paddleCollisionFlag = 1
ElseIf ((ballYPos + GetSpriteHeight(2)) >= GetScreenBoundsBottom()) //Detecs collision between ball and Bottom Window Border.
ballYSpeed = (ballYSpeed * -1)
paddleCollisionFlag = 1
ElseIf (ballXPos <= GetScreenBoundsLeft()) //Detects collision between ball and Left Window Border.
ballXSpeed = (ballXSpeed * -1)
paddleCollisionFlag = 1
ElseIf (ballYPos <= GetScreenBoundsTop()) //Detects collision between ball and Top Window Border.
ballYSpeed = (ballYSpeed * -1)
paddleCollisionFlag = 1
Endif
If paddleCollisionFlag = 1 //Collision between paddle and ball.
If GetSpriteCollision(1, 2) = 1
ballYSpeed = (ballYSpeed * -1)
paddleCollisionFlag = 0
Endif
Endif
Sync()
loop
If anyone could assist me in getting the ball to bounce correctly, help in controlling the velocity, and explaining these things, I would very much appreciate it.
-gameangel147