Breakout clone help

Blitz3D Forums/Blitz3D Beginners Area/Breakout clone help

Yahfree(Posted 2007) [#1]
Hey, i have a question and may have more as i go on so i made this thread. For my breakout clone i'm working on.

First of all i'm really new at programming my only other language is HTML, so bare with me :).


Anyways, i'm working on a breakout clone to teach me basics of game programming, I'm trying to figure out at the moment how to make a pillar go buh bye when the ball hits i'v tried to make a veriable that has to be true to draw a block, and if the pillar hits then the veriable is false for that block. but i also made it when it hits the pillars it gives me points, so even after it hits if the ball pass's through that area it gives me a point.

heres my code, also i'm trying to figure out how to make the ball bounce back ect:

AppTitle "Breakout by Yahfree","Sure you want to quit?"

Graphics 1680,1050,16,1
SetBuffer BackBuffer()

;Tunes for the background
chnbackground=PlayMusic("Music&SFX\Scorpions - Rock You Like A Hurricane.mp3") 

;Global stuff can be accessed anywhere if put here
Global ESC_KEY=1, UP_ARR=200, LEFT_ARR=203, RIGHT_ARR=205, DOWN_ARR=208, SPAC_BAR=57, Q=24, CTR=157
Global ms
Global player_x=800,player_y=900,ball_x=840,ball_y=880,power_x=0,power_y=0
Global move=0, pldelay=MilliSecs()
Global player, power, ball, mouse, score=0

;Pillars
;SeedRnd MilliSecs()
Type pillar
Field X
Field Y
Field r
Field b
Field g
Field hp
Field pillamount=109
End Type

For tempy=200 To 500 Step 30
For tempx=325 To 1350 Step 100
  pillars.pillar=New pillar
  pillars\X=tempx
  pillars\Y=tempy
  pillars\r=Rand(100,200)
  pillars\g=Rand(100,200)
  pillars\b=Rand(100,200)
  pillars\hp=1
Next
 Next

;@!@!@!@@@!@!@!@!@@@@@@@@@@@@@!@!@!@!@!@!@!@!@!
;@!@!@@@@@@!!!!MAIN GAMELOOP CODE!!!!!@!@!@!@!@
;@!@!@!@!@!@!@!@!@!@!@!@!@@@@@@@@@@!!!!@@@!!!@!
While Not KeyHit(ESC_KEY)

;Whipes the memory storage clean every 1 millisec keeps it running nice and smooth
    tmrscreen=MilliSecs()
    If MilliSecs() > ms+1 Then
    Cls
   End If

;Says that this = this... in this case the word "mouse_y" = mouseY() the code for the mouse
     mouse_y=MouseY()
     mouse_x=MouseX()
     pr=Rand(0,255)
     pb=Rand(0,255)
     pg=Rand(0,255)


    ;Player controls 2 of each left and right to move the ball if move=0
    Keycontrols()


    ;Tells that if move=1\2 is activated somehow then that function is also activated
    If move=1 Then ballmovement()
    If move=2 Then ballmovement2()

    ;Draws the MouseDot
    Oval mouse_x,mouse_y,1,1

    ;Draws the ball
    Oval ball_x,ball_y,20,20
   
    ;Draws the player controled object
    Rect player_x,player_y,100,20

    ;Draws the "Power" block and some of its functions E.G if reachs a point it resets
    Color pr,pb,pg
    Rect PL,power_y,60,38
    power_y = power_y+ 10
    If power_y = 1050 Then power_y = 0
    If power_y =0 Then PL=Rand(0,1580)
    
    ;Color switched back to white
    Color 255,255,255
  
    ;Draws the pillars using types
   pillars.pillar=First pillar
    If pillars\hp=1 Then Color pillars\r,pillars\b,pillars\g:Rect pillars\X,pillars\Y,100,30,1:Color 0,0,0:Rect pillars\X,pillars\Y,100,30,0
    For temp=1 To 109
    pillars=After pillars 
    If pillars\hp=1 Then Color pillars\r,pillars\b,pillars\g:Rect pillars\X,pillars\Y,100,30,1:Color 0,0,0:Rect pillars\X,pillars\Y,100,30,0
   Next

    ;Switch the color back to white for the text
    Color 255,255,255

    If score=15000 Then
    Text 800,500,"YOU WIN!!"
    score=0
   End If
  


    ;Text and some maths
    Text 50,50,"Players X: "+player_x+" Players Y: "+player_y
    Text 50,65,"Ball X: "+ball_x+" Ball Y: "+ball_y
    Text 50,80,"Powerblock X:"+PL+" Powerblock Y: "+power_y
    Text 50,95,"MouseDot X: "+mouse_x+" MouseDot Y: "+mouse_y
    Text PL+7,power_y,"Power"
    Text 800,0,"Score: "+score

    ;Collision codes
    ;Power block -> player
    If RectsOverlap(PL,power_y,60,38,player_x,player_y,100,20) And ball_x=player_x+40 And move=0 Then
    move=2
   End If

  ;Ball -> Pillars
  For p.pillar=Each pillar
   If RectsOverlap(p\X,p\Y,100,30,ball_x,ball_y,15,15) Then
   score=score+1
   End If
  Next

;Flip all the actions into the front buffer
 Flip
    ;tmrscreen=MilliSecs()
Wend

;This function is activated from the "Move=" code if the = value is 1 this is activated
Function ballmovement()
    If move=2 Then ball_y = ball_y + -20
    ball_y = ball_y + -10
    If ball_y=0 Then ball_y=880 ball_x=player_x+40 move=0
End Function

;This function is activated from the "Move=" code if the = value is 2 this is activated
Function ballmovement2()
    ball_y = ball_y + -20
    If ball_y=0 Then ball_y=880 ball_x=player_x+40 move=0
End Function

Function Keycontrols()
    If KeyDown(LEFT_ARR) And player_x>0 Then
    player_x = player_x -10
   End If
    If KeyDown(LEFT_ARR) And move=0 And ball_x>40 Then
    ball_x = ball_x + -10
   End If
    If KeyDown(RIGHT_ARR) And player_x<1580 Then
    player_x = player_x + 10
   End If
    If KeyDown(RIGHT_ARR) And move=0 And ball_x<1620 Then
    ball_x = ball_x + 10
   End If
    If KeyDown(SPAC_BAR) Then
    move=1   
   End If
End Function



b32(Posted 2007) [#2]
To remove the blocks when the ball hits them, you should first change the part "Draws the pillars using types", using "For Each" instead of "First" and "After":
;Draws the pillars using types
For pillars.pillar=Each pillar
If pillars\hp=1 Then Color pillars\r,pillars\b,pillars\g:Rect pillars\X,pillars\Y,100,30,1:Color 0,0,0:Rect pillars\X,pillars\Y,100,30,0
Next


Then, when the collision occurs, at the "collision codes" part, add "delete p", like this:
 ;Ball -> Pillars
  For p.pillar=Each pillar
   If RectsOverlap(p\X,p\Y,100,30,ball_x,ball_y,15,15) Then
   score=score+1
   Delete p
   End If
  Next

To make the ball bounce, maybe this helps:
Graphics 800, 600, 0, 2
SetBuffer BackBuffer()

bal_xdir = 1
bal_ydir = 1

Repeat

Cls

balx = balx + bal_xdir*5
baly = baly + bal_ydir*5

If baly < 0 Then bal_ydir = 1
If baly > 590 Then bal_ydir = -1
If balx < 0 Then bal_xdir = 1
If balx > 790 Then bal_xdir = -1

Oval balx, baly, 10, 10

Flip

Until KeyHit(1)



Yahfree(Posted 2007) [#3]
Alright the Delete p thing works wonders, but i still cant figure out how to get the ball to bounce, i tried working in your code, this is what it looks like now notice the change in the function ball movement:

AppTitle "Breakout by Yahfree","Sure you want to quit?"

Graphics 1680,1050,16,1
SetBuffer BackBuffer()

;Tunes for the background
chnbackground=PlayMusic("Music&SFX\Scorpions - Rock You Like A Hurricane.mp3") 

;Global stuff can be accessed anywhere if put here
Global ESC_KEY=1, UP_ARR=200, LEFT_ARR=203, RIGHT_ARR=205, DOWN_ARR=208, SPAC_BAR=57, Q=24, CTR=157
Global ms
Global player_x=800,player_y=900,ball_x=840,ball_y=880,power_x=0,power_y=0
Global move=0, pldelay=MilliSecs()
Global player, power, ball, mouse, score=0
Global bal_xdir = 1
Global bal_ydir = 1

;Pillars
;SeedRnd MilliSecs()
Type pillar
Field X
Field Y
Field r
Field b
Field g
Field hp
Field pillamount=109
End Type

For tempy=200 To 500 Step 30
For tempx=325 To 1350 Step 100
  pillars.pillar=New pillar
  pillars\X=tempx
  pillars\Y=tempy
  pillars\r=Rand(100,200)
  pillars\g=Rand(100,200)
  pillars\b=Rand(100,200)
  pillars\hp=1
Next
 Next



;@!@!@!@@@!@!@!@!@@@@@@@@@@@@@!@!@!@!@!@!@!@!@!
;@!@!@@@@@@!!!!MAIN GAMELOOP CODE!!!!!@!@!@!@!@
;@!@!@!@!@!@!@!@!@!@!@!@!@@@@@@@@@@!!!!@@@!!!@!
While Not KeyHit(ESC_KEY)

;Whipes the memory storage clean every 1 millisec keeps it running nice and smooth
    tmrscreen=MilliSecs()
    If MilliSecs() > ms+1 Then
    Cls
   End If

;Says that this = this... in this case the word "mouse_y" = mouseY() the code for the mouse
     mouse_y=MouseY()
     mouse_x=MouseX()
     pr=Rand(0,255)
     pb=Rand(0,255)
     pg=Rand(0,255)


    ;Player controls 2 of each left and right to move the ball if move=0
    Keycontrols()


    ;Tells that if move=1\2 is activated somehow then that function is also activated
    If move=1 Then ballmovement()
    If move=2 Then ballmovement2()

    ;Draws the MouseDot
    Oval mouse_x,mouse_y,1,1

    ;Draws the ball
    Oval ball_x,ball_y,20,20
   
    ;Draws the player controled object
    Rect player_x,player_y,100,20

    ;Draws the "Power" block and some of its functions E.G if reachs a point it resets
    Color pr,pb,pg
    Rect PL,power_y,60,38
    power_y = power_y+ 10
    If power_y = 1050 Then power_y = 0
    If power_y =0 Then PL=Rand(0,1580)
    
    ;Color switched back to white
    Color 255,255,255
  
    ;Draws the pillars using types
   For pillars.pillar=Each pillar
   If pillars\hp=1 Then Color pillars\r,pillars\b,pillars\g:Rect pillars\X,pillars\Y,100,30,1:Color 0,0,0:Rect pillars\X,pillars\Y,100,30,0
Next


    ;Switch the color back to white for the text
    Color 255,255,255

    If score=15000 Then
    Text 800,500,"YOU WIN!!"
    score=0
   End If
  


    ;Text and some maths
    Text 50,50,"Players X: "+player_x+" Players Y: "+player_y
    Text 50,65,"Ball X: "+ball_x+" Ball Y: "+ball_y
    Text 50,80,"Powerblock X:"+PL+" Powerblock Y: "+power_y
    Text 50,95,"MouseDot X: "+mouse_x+" MouseDot Y: "+mouse_y
    Text PL+7,power_y,"Power"
    Text 800,0,"Score: "+score

    ;Collision codes
    ;Power block -> player
    If RectsOverlap(PL,power_y,60,38,player_x,player_y,100,20) And ball_x=player_x+40 And move=0 Then
    move=2
   End If

  ;Ball -> Pillars
  For p.pillar=Each pillar
   If RectsOverlap(p\X,p\Y,100,30,ball_x,ball_y,15,15) Then
   score=score+1
   Delete p
   End If
  Next

;Flip all the actions into the front buffer
 Flip

Wend

;This function is activated from the "Move=" code if the = value is 1 this is activated
Function ballmovement()
    If move=2 Then ball_y = ball_y + -20
    ball_y = ball_y + -10
    ;If ball_y=0 Then ball_y=880 ball_x=player_x+40 move=0

balx = balx + bal_xdir*5
baly = baly + bal_ydir*5

If ball_y < 0 Then bal_ydir = 1
If ball_y > 1040 Then bal_ydir = -1
If ball_x < 0 Then bal_xdir = 1
If ball_x > 1670 Then bal_xdir = -1


End Function

;This function is activated from the "Move=" code if the = value is 2 this is activated
Function ballmovement2()
    ball_y = ball_y + -20
    If ball_y=0 Then ball_y=880 ball_x=player_x+40 move=0
End Function

Function Keycontrols()
    If KeyDown(LEFT_ARR) And player_x>0 Then
    player_x = player_x -10
   End If
    If KeyDown(LEFT_ARR) And move=0 And ball_x>40 Then
    ball_x = ball_x + -10
   End If
    If KeyDown(RIGHT_ARR) And player_x<1580 Then
    player_x = player_x + 10
   End If
    If KeyDown(RIGHT_ARR) And move=0 And ball_x<1620 Then
    ball_x = ball_x + 10
   End If
    If KeyDown(SPAC_BAR) Then
    move=1   
   End If
End Function


And thanks for the help


b32(Posted 2007) [#4]
It is just a typo, in the function ballmovement(), change "balx" to "ball_x" and comment out the original upwards movement:
;This function is activated from the "Move=" code if the = value is 1 this is activated
Function ballmovement()
    If move=2 Then ball_y = ball_y + -20
;    ball_y = ball_y + -10
    ;If ball_y=0 Then ball_y=880 ball_x=player_x+40 move=0

ball_x = ball_x + bal_xdir*5
ball_y = ball_y + bal_ydir*5

If ball_y < 0 Then bal_ydir = 1
If ball_y > 1040 Then bal_ydir = -1
If ball_x < 0 Then bal_xdir = 1
If ball_x > 1670 Then bal_xdir = -1


End Function



Yahfree(Posted 2007) [#5]
Alright, first of all thanks for all the Help b32.


I think i got it to work after a few deletings and a few editings, the only problem is when the space bar is pressed the ball launches the wrong way from what i'v learned its just a matter of changing a veriable, but i cant seem to find what one.

I'll brain storm a bit, see if i cant beat you to the answer, Thanks again for the help. Heres my code at the moment:
AppTitle "Breakout by Yahfree","Sure you want to quit?"

Graphics 1680,1050,16,1
SetBuffer BackBuffer()

;Tunes for the background
chnbackground=PlayMusic("Music&SFX\Scorpions - Rock You Like A Hurricane.mp3") 

;Global stuff can be accessed anywhere if put here
Global ESC_KEY=1, UP_ARR=200, LEFT_ARR=203, RIGHT_ARR=205, DOWN_ARR=208, SPAC_BAR=57, Q=24, CTR=157
Global ms
Global player_x=800,player_y=900,ball_x=840,ball_y=880,power_x=0,power_y=0
Global move=0, pldelay=MilliSecs()
Global player, power, ball, mouse, score=0
Global bal_xdir = 1
Global bal_ydir = 1

;Pillars
;SeedRnd MilliSecs()
Type pillar
Field X
Field Y
Field r
Field b
Field g
Field hp
Field pillamount=109
End Type

For tempy=200 To 500 Step 30
For tempx=325 To 1350 Step 100
  pillars.pillar=New pillar
  pillars\X=tempx
  pillars\Y=tempy
  pillars\r=Rand(100,200)
  pillars\g=Rand(100,200)
  pillars\b=Rand(100,200)
  pillars\hp=1
Next
 Next

bal_xdir = 1
bal_ydir = 1

;@!@!@!@@@!@!@!@!@@@@@@@@@@@@@!@!@!@!@!@!@!@!@!
;@!@!@@@@@@!!!!MAIN GAMELOOP CODE!!!!!@!@!@!@!@
;@!@!@!@!@!@!@!@!@!@!@!@!@@@@@@@@@@!!!!@@@!!!@!
While Not KeyHit(ESC_KEY)

;Whipes the memory storage clean every 1 millisec keeps it running nice and smooth
    tmrscreen=MilliSecs()
    If MilliSecs() > ms+1 Then
    Cls
   End If


;Says that this = this... in this case the word "mouse_y" = mouseY() the code for the mouse
     mouse_y=MouseY()
     mouse_x=MouseX()
     pr=Rand(0,255)
     pb=Rand(0,255)
     pg=Rand(0,255)


    ;Player controls 2 of each left and right to move the ball if move=0
    Keycontrols()


    ;Tells that if move=1\2 is activated somehow then that function is also activated
    If move=1 Then ballmovement()


    ;Draws the MouseDot
    Oval mouse_x,mouse_y,1,1

    ;Draws the ball
    Oval ball_x,ball_y,20,20
   
    ;Draws the player controled object
    Rect player_x,player_y,100,20

    ;Draws the "Power" block and some of its functions E.G if reachs a point it resets
    Color pr,pb,pg
    Rect PL,power_y,60,38
    power_y = power_y+ 10
    If power_y = 1050 Then power_y = 0
    If power_y =0 Then PL=Rand(0,1580)
    
    ;Color switched back to white
    Color 255,255,255
  
    ;Draws the pillars using types
   For pillars.pillar=Each pillar
   If pillars\hp=1 Then Color pillars\r,pillars\b,pillars\g:Rect pillars\X,pillars\Y,100,30,1:Color 0,0,0:Rect pillars\X,pillars\Y,100,30,0
Next


    ;Switch the color back to white for the text
    Color 255,255,255

    If score=15000 Then
    Text 800,500,"YOU WIN!!"
    score=0
   End If


  


    ;Text and some maths
    Text 50,50,"Players X: "+player_x+" Players Y: "+player_y
    Text 50,65,"Ball X: "+ball_x+" Ball Y: "+ball_y
    Text 50,80,"Powerblock X:"+PL+" Powerblock Y: "+power_y
    Text 50,95,"MouseDot X: "+mouse_x+" MouseDot Y: "+mouse_y
    Text PL+7,power_y,"Power"
    Text 800,0,"Score: "+score

    ;Collision codes
    ;Power block -> player
    If RectsOverlap(PL,power_y,60,38,player_x,player_y,100,20) And ball_x=player_x+40 And move=0 Then
    move=2
   End If

  ;Ball -> Pillars
  For p.pillar=Each pillar
   If RectsOverlap(p\X,p\Y,100,30,ball_x,ball_y,15,15) Then
   score=score+1
   Delete p
   End If
  Next

;Flip all the actions into the front buffer
 Flip

Wend

;This function is activated from the "Move=" code if the = value is 1 this is activated
Function ballmovement()

ball_x = ball_x + bal_xdir*5
ball_y = ball_y + bal_ydir*5

If ball_y < 0 Then bal_ydir = 1
If ball_y > 1040 Then bal_ydir = -1
If ball_x < 0 Then bal_xdir = 1
If ball_x > 1670 Then bal_xdir = -1

End Function




Function Keycontrols()
    If KeyDown(LEFT_ARR) And player_x>0 Then
    player_x = player_x -10
   End If
    If KeyDown(LEFT_ARR) And move=0 And ball_x>40 Then
    ball_x = ball_x + -10
   End If
    If KeyDown(RIGHT_ARR) And player_x<1580 Then
    player_x = player_x + 10
   End If
    If KeyDown(RIGHT_ARR) And move=0 And ball_x<1620 Then
    ball_x = ball_x + 10
   End If
    If KeyDown(SPAC_BAR) Then
    move=1   
   End If
End Function



Yahfree(Posted 2007) [#6]
Cool! i got the bounce off the padle to work, but it still fires off the wrong way, i made it fire upwards rather then downwards but it fires off in a 45degree angle

Also gotta try to figure out how to make it bounce off the pillars, i tried to make it bounce off when it collided, but for example if the ball makes it to the top and flys downward to the pillars it keeps going, also screws up if it hits the pillars from the side, works fine if it hits from the bottom though.


Heres my code:

AppTitle "Breakout by Yahfree","Sure you want to quit?"

Graphics 1680,1050,16,1
SetBuffer BackBuffer()

;Tunes for the background
chnbackground=PlayMusic("Music&SFX\Scorpions - Rock You Like A Hurricane.mp3") 

;Global stuff can be accessed anywhere if put here
Global ESC_KEY=1, UP_ARR=200, LEFT_ARR=203, RIGHT_ARR=205, DOWN_ARR=208, SPAC_BAR=57, Q=24, CTR=157
Global ms
Global player_x=800,player_y=900,ball_x=840,ball_y=880,power_x=0,power_y=0
Global move=0, pldelay=MilliSecs()
Global player, power, ball, mouse, score=0
Global bal_xdir = 1
Global bal_ydir = 1

;Pillars
;SeedRnd MilliSecs()
Type pillar
Field X
Field Y
Field r
Field b
Field g
Field hp
Field pillamount=109
End Type

For tempy=200 To 500 Step 30
For tempx=325 To 1350 Step 100
  pillars.pillar=New pillar
  pillars\X=tempx
  pillars\Y=tempy
  pillars\r=Rand(100,200)
  pillars\g=Rand(100,200)
  pillars\b=Rand(100,200)
  pillars\hp=1
Next
 Next

bal_xdir = -1
bal_ydir = -1

;@!@!@!@@@!@!@!@!@@@@@@@@@@@@@!@!@!@!@!@!@!@!@!
;@!@!@@@@@@!!!!MAIN GAMELOOP CODE!!!!!@!@!@!@!@
;@!@!@!@!@!@!@!@!@!@!@!@!@@@@@@@@@@!!!!@@@!!!@!
While Not KeyHit(ESC_KEY)

;Whipes the memory storage clean every 1 millisec keeps it running nice and smooth
    tmrscreen=MilliSecs()
    If MilliSecs() > ms+1 Then
    Cls
   End If


;Says that this = this... in this case the word "mouse_y" = mouseY() the code for the mouse
     mouse_y=MouseY()
     mouse_x=MouseX()
     pr=Rand(0,255)
     pb=Rand(0,255)
     pg=Rand(0,255)


    ;Player controls 2 of each left and right to move the ball if move=0
    Keycontrols()


    ;Tells that if move=1\2 is activated somehow then that function is also activated
    If move=1 Then ballmovement()


    ;Draws the MouseDot
    Oval mouse_x,mouse_y,1,1

    ;Draws the ball
    Oval ball_x,ball_y,20,20
   
    ;Draws the player controled object
    Rect player_x,player_y,100,20

    ;Draws the "Power" block and some of its functions E.G if reachs a point it resets
    Color pr,pb,pg
    Rect PL,power_y,60,38
    power_y = power_y+ 10
    If power_y = 1050 Then power_y = 0
    If power_y =0 Then PL=Rand(0,1580)
    
    ;Color switched back to white
    Color 255,255,255
  
    ;Draws the pillars using types
   For pillars.pillar=Each pillar
   If pillars\hp=1 Then Color pillars\r,pillars\b,pillars\g:Rect pillars\X,pillars\Y,100,30,1:Color 0,0,0:Rect pillars\X,pillars\Y,100,30,0
Next


    ;Switch the color back to white for the text
    Color 255,255,255

    If score=15000 Then
    Text 800,500,"YOU WIN!!"
    score=0
   End If


  


    ;Text and some maths
    Text 50,50,"Players X: "+player_x+" Players Y: "+player_y
    Text 50,65,"Ball X: "+ball_x+" Ball Y: "+ball_y
    Text 50,80,"Powerblock X:"+PL+" Powerblock Y: "+power_y
    Text 50,95,"MouseDot X: "+mouse_x+" MouseDot Y: "+mouse_y
    Text PL+7,power_y,"Power"
    Text 800,0,"Score: "+score

    ;Collision codes
    ;Power block -> player
    ;If RectsOverlap(PL,power_y,60,38,player_x,player_y,100,20) And ball_x=player_x+40 And move=0 Then
    ;move=2
   ;End If

  ;Ball -> Pillars
  For p.pillar=Each pillar
   If RectsOverlap(p\X,p\Y,100,30,ball_x,ball_y,15,15) Then
   score=score+1
   Delete p
   End If
  Next

;Flip all the actions into the front buffer
 Flip

Wend

;This function is activated from the "Move=" code if the = value is 1 this is activated
Function ballmovement()

ball_x = ball_x + bal_xdir*5
ball_y = ball_y + bal_ydir*5

If ball_y < 0 Then bal_ydir = 1
If ball_y > 1040 Then move=0
If ball_x < 0 Then bal_xdir = 1
If ball_x > 1670 Then bal_xdir = -1

If RectsOverlap(ball_x,ball_y,15,15,player_x,player_y,100,20) Then bal_ydir = -1

If move=0 Then
ball_y=player_y+-20
ball_x=player_x+40
End If

End Function




Function Keycontrols()
    If KeyDown(LEFT_ARR) And player_x>0 Then
    player_x = player_x -10
   End If
    If KeyDown(LEFT_ARR) And move=0 And ball_x>40 Then
    ball_x = ball_x + -10
   End If
    If KeyDown(RIGHT_ARR) And player_x<1580 Then
    player_x = player_x + 10
   End If
    If KeyDown(RIGHT_ARR) And move=0 And ball_x<1620 Then
    ball_x = ball_x + 10
   End If
    If KeyDown(SPAC_BAR) Then
    move=1   
   End If
End Function



b32(Posted 2007) [#7]
It looks like it's coming along quite nicely :)


Yahfree(Posted 2007) [#8]
Heres a question: How do you rotate a image in realtime, i tried if keydown blah then rotate the image it slows it down and gets all screwed up :\


Stevie G(Posted 2007) [#9]
You'd need to rotate the image and store the new versions before the main loop. As you've found out it's dog slow in realtime and not intended for that purpose.

If you change your initial velocity setting to this ...
Global bal_xdir = 0
Global bal_ydir = -1

and get rid of this further down the code
bal_xdir = -1
bal_ydir = -1


The ball will fire straight upwards when space is intially pressed.

Stevie.

p.s. You may want to reconsider reducing the graphics resolution as alot of people won't be able to run the app. On my lsd monitor max is 1280 x 960.


Yahfree(Posted 2007) [#10]
Cool, thanks, is there a code that fits it to the screen (full screen) that its being played on?


Yahfree(Posted 2007) [#11]
Alright new problem.

Because the ball shoots upwards (correctly)

And the padle doesnt change the angle of the ball once it hits, the ball just goes up and down ... now, if i look at a real breakout game when you move the padle and the ball hits the edge of it, it flys off at a angle not just upwards.

How do i get this effect?


Subirenihil(Posted 2007) [#12]
You don't rotate in realtime in 2d. Your options are to either pre-rotate the image (creating a series of images each rotated a different way) or you can use 3d to mimic 2d.

Here is one way to pre-rotate:
;Preparation
Const ROTATIONS=32
Const ROTATION_AMOUNT=360/rotations

Global rotatedimage[ROTATIONS]

For angle=0 to ROTATIONS-1
	rotatedimage[angle]=CopyImage(<image to be rotated>)
	RotateImage rotatedimage[angle],angle#*ROTATION_AMOUNT
Next

;Actual use this assumes <angle> is >= 0

DrawImage rotatedimage[(Floor#(( <angle> /ROTATION_AMOUNT) + .5) Mod ROTATIONS)]

You may ask why use an array of images instead of multiple frames?
Two reasons:
1) Conserve memory. Rotated images get larger in comparison to the original the more they are rotated.
2) Allows an animated image to be rotated without any extra hassle


b32(Posted 2007) [#13]
For checking if a resolution exists, use GfxModeExists().
I suppose, when the ball hits the paddle, you can alter the x-speed depending on the distance between the centre of the ball and the centre of the paddle:
newspeedx = (balx - paddlex)


Yahfree(Posted 2007) [#14]
Sorry b32, i understand the conscept but i cant figure out how to make that into code that would work.


Bare with me i'm very new to game programing in general.

so, somthing with a veriable that tells to change the x pos. of the ball when it hits the paddle... would i put your code "newspeedx = (balx - paddlex)" on the collision with the ball and the padle?


Yahfree(Posted 2007) [#15]
Lol, i tried to do this:

newspeedx = (ball_x - player_x)

;Blah blah the rest of the movement function

If RectsOverlap(ball_x,ball_y,15,15,player_x,player_y,100,20) Then bal_ydir = -1 bal_xdir = newspeedx



notice the:
bal_xdir = newspeedx


after the collision with the paddle, but when it hits it flys off at a million miles per hour to the right. And returns to normal speed when it hits the wall and bounces


Yahfree(Posted 2007) [#16]
Does it have somthing to do with the handle on the image? or am i putting the code in wrong :\


b32(Posted 2007) [#17]
No, the idea is allright, shouldn't there be an extra : between the two commands after the If ? Better is to use an EndIf block.
Okay, so we are talking about the part after the rectsoverlap() that checks for the collision between the paddle and the ball. That is correct.
The goal is to change the angle of the ball, depending on where it hits the paddle.
I think it is important that you understand how the velocities work that Stevie explained first.
There are two variables: bal_xdir and bal_ydir.
The one you need for this problem is bal_xdir. It defines the sideways movement of the ball.
If "bal_xdir" is negative, the ball goes to the left. If it is positive, it goes to the right. The higher its absoluate value is, the more speed the ball has.
-2 = left/fast
-1 = left/slow
+1 = right/slow
+2 = right/fast
If this value is too high (too low), the ball goes too fast. Best is to check the limits for this yourself.
So, when the ball bounces back from the paddle, you need to assign "bal_xdir" with a new value.
Then you need to determine where the ball hits the paddle.
That can be done by substracting the ball's x position from the paddle's x position. If the outcome of this substraction is negative, the ball is on the left of the paddle. If the substraction is positive, the ball is on the right.
To get a proper 'touch' with this value, use:
Color 255,255,255
Text 0, 0, (ball_x + 7) - (player_x + 50)

Right before calling "Flip"
I'm adding 7 to ball_x and 50 to player_x, because you need the centre of both the ball and the paddle. That is one problem with what you were trying.
Next it is a matter of getting this value converted into a usable velocity.


Yahfree(Posted 2007) [#18]
Alright, i'll mess around with that. whats a good way to make that into a x velocity ?


Yahfree(Posted 2007) [#19]
OH! i see its kinda the x distance from the center of the ball and the center of the padle... hmm. And i have to somehow turn this into a useable veriable for the ball's X angle..? all so confusing :)


Yahfree(Posted 2007) [#20]
Alright i'm getting closer!! i just gotta turn these numbers into a small scale version so they don't bounce off at a million miles per hour..

Function ballmovement()

newspeedx = (ball_x+7 - player_x+50)

ball_x = ball_x + bal_xdir*5
ball_y = ball_y + bal_ydir*5

If ball_y < 0 Then bal_ydir = 1
If ball_y > 1040 Then move=0 lifes=lifes-1
If ball_x < 0 Then bal_xdir = 1
If ball_x > 1670 Then bal_xdir = -1

If RectsOverlap(ball_x,ball_y,15,15,player_x,player_y,100,20) Then bal_ydir = -1 bal_xdir = newspeedx

If move=0 Then
ball_y=player_y+-20
ball_x=player_x+40
End If

End Function


The question is; how? lol


Yahfree(Posted 2007) [#21]
Hmm i did

newspeedx = (ball_x+7 - player_x+50)*10


that made it 10 times faster, now how to make it 10 times slower...

i tried "-*10" that didnt work.


Yahfree(Posted 2007) [#22]
SO CLOSE i can taste that ball smacking off the paddle the right way, anyways, its reading the center of the paddle wrong, because the numbers say that the right edge is the center, i know because i halted the movment of the ball with the paddle and i moved the padle around while the ball was holding still, and the left edge is "0.0" and the right edge is "2.0" how do i fix this? so the center is "0.0" and the edges are "-1.0" for the left and "1.0"for the right?


heres my code if it helps btw i figured out the numbers to make it whatever less times i have to put a decimal

Function ballmovement()

newspeedx = (ball_x+7 - player_x+100)*.01

ball_x = ball_x + bal_xdir*5
ball_y = ball_y + bal_ydir*5

If ball_y < 0 Then bal_ydir = 1
If ball_y > 1040 Then move=0 lifes=lifes-1
If ball_x < 0 Then bal_xdir = 1
If ball_x > 1670 Then bal_xdir = -1

If RectsOverlap(ball_x,ball_y,15,15,player_x,player_y,100,20) Then bal_ydir = -1 bal_xdir = newspeedx

If move=0 Then
ball_y=player_y+-20
ball_x=player_x+40
End If

End Function


And the display code was also altered to match the real code:
Color 255,255,255
Text 0, 0,(ball_x+7 - player_x+100)*.01



Stevie G(Posted 2007) [#23]
To center the bat use the midhandle command detailed here after loading.

http://www.blitzbasic.com/b3ddocs/command.php?name=MidHandle&ref=2d_cat


Yahfree(Posted 2007) [#24]
Oh so it seems i have to make my images real images, well i was gonna have to do that anyways as i planned on after its done replacing all the basic images with real images to make it look a lot better


Yahfree(Posted 2007) [#25]
Hmm how would i make the pillars a real image? i'm still learning types, so any help would be wonderful


Yahfree(Posted 2007) [#26]
Alright, i don't know what i'm doing wrong, but its not working, the ball isnt flying off right only up and down:

first off i changed most of my images into real images and auto midhandled 'em and drew em and also got rid of the player_x+50 thing and just made it player_x because the midhandle allows that Everthing looks in working order when the ball is centered the text display says its 0.0 and each edge is 1.0 and -1.0:

The drawing codes changes the ball and the player into real images alowing midhandles:
;creates the empty containers to draw on using the buffer
AutoMidHandle True
player=CreateImage(100,20)
ball=CreateImage(20,20)
life1=CreateImage(20,20)
life2=CreateImage(20,20)
life3=CreateImage(20,20)

SetBuffer ImageBuffer(player)
Rect 0,0,100,20

SetBuffer ImageBuffer(ball)
Oval 0,0,20,20


SetBuffer BackBuffer()


shows how i drew the images onto the screen:
    ;Draws the MouseDot
    Oval mouse_x,mouse_y,1,1

    ;Draws the ball
    DrawImage ball,ball_x,ball_y
    
    ;Draws the life's depending on the lifes veriable
    If lifes>1 Then
    Oval 1411,1005,20,20
  End If
    If lifes>2 Then
    Oval 1433,1005,20,20
  End If
    If lifes>3 Then
    Oval 1455,1005,20,20
  End If
  
    ;Draws the player controled object
    DrawImage player,player_x,player_y

    ;Draws the "Power" block and some of its functions E.G if reachs a point it resets
    Color pr,pb,pg
    Rect PL,power_y,60,38
    power_y = power_y+ 10
    If power_y = 1050 Then power_y = 0
    If power_y =0 Then PL=Rand(0,1580)
    
    ;Color switched back to white
    Color 255,255,255
  
    ;Draws the pillars using types
   For pillars.pillar=Each pillar
   If pillars\hp=1 Then Color pillars\r,pillars\b,pillars\g:Rect pillars\X,pillars\Y,100,30,1:Color 0,0,0:Rect pillars\X,pillars\Y,100,30,0
Next


Shows how i did the ball movement function:
Function ballmovement()

newspeedx = (ball_x - player_x)*.01

ball_x = ball_x + bal_xdir*5
ball_y = ball_y + bal_ydir*5

If ball_y < 0 Then bal_ydir = 1
If ball_y > 1040 Then move=0 lifes=lifes-1
If ball_x < 0 Then bal_xdir = 1
If ball_x > 1670 Then bal_xdir = -1

If ImagesCollide (ball,ball_x,ball_y,0,player,player_x,player_y,0) Then bal_ydir = -1 bal_xdir = newspeedx

If move=0 Then
ball_y=player_y+-20
ball_x=player_x
End If

End Function


And shows that the display matchs the function
Color 255,255,255
Text 0, 0,(ball_x - player_x)*.01


Well thats everything that would have to do with the ball bouncing off the paddle the wrong way(i think)

Any ideas what i did wrong? and if you need more code let me know, also thanks for all the help i'm still learning.


b32(Posted 2007) [#27]
I think it needs a ":" in this line:
If ImagesCollide (ball,ball_x,ball_y,0,player,player_x,player_y,0) Then bal_ydir = -1: bal_xdir = newspeedx


Yahfree(Posted 2007) [#28]
That wasnt it. Still screwed up heres the whole code:

AppTitle "Breakout by Yahfree","Sure you want to quit?"

Graphics GraphicsWidth,GraphicsHeight,16,1
SetBuffer BackBuffer()

;Tunes for the background
chnbackground=PlayMusic("Music&SFX\Scorpions - Rock You Like A Hurricane.mp3") 

;Global stuff can be accessed anywhere if put here
Global ESC_KEY=1, UP_ARR=200, LEFT_ARR=203, RIGHT_ARR=205, DOWN_ARR=208, SPAC_BAR=57, Q=24, CTR=157
Global ms
Global player_x=800,player_y=900,ball_x=player_x,ball_y=880,power_x=0,power_y=0
Global move=0, pldelay=MilliSecs()
Global player, power, ball, mouse, score=0
Global bal_xdir = 0
Global bal_ydir = -1
Global lifes=4

;Pillars
;SeedRnd MilliSecs()
Type pillar
Field X
Field Y
Field r
Field b
Field g
Field hp
Field pillamount=109
End Type

For tempy=200 To 500 Step 30
For tempx=325 To 1350 Step 100
  pillars.pillar=New pillar
  pillars\X=tempx
  pillars\Y=tempy
  pillars\r=Rand(100,200)
  pillars\g=Rand(100,200)
  pillars\b=Rand(100,200)
  pillars\hp=1
Next
 Next

;creates the empty containers to draw on using the buffer
AutoMidHandle True
player=CreateImage(100,20)
ball=CreateImage(20,20)
life1=CreateImage(20,20)
life2=CreateImage(20,20)
life3=CreateImage(20,20)

SetBuffer ImageBuffer(player)
Rect 0,0,100,20

SetBuffer ImageBuffer(ball)
Oval 0,0,20,20


SetBuffer BackBuffer()


;@!@!@!@@@!@!@!@!@@@@@@@@@@@@@!@!@!@!@!@!@!@!@!
;@!@!@@@@@@!!!!MAIN GAMELOOP CODE!!!!!@!@!@!@!@
;@!@!@!@!@!@!@!@!@!@!@!@!@@@@@@@@@@!!!!@@@!!!@!
While Not KeyHit(ESC_KEY)

;Whipes the memory storage clean every 1 millisec keeps it running nice and smooth
    tmrscreen=MilliSecs()
    If MilliSecs() > ms+1 Then
    Cls
   End If


;Says that this = this... in this case the word "mouse_y" = mouseY() the code for the mouse
     mouse_y=MouseY()
     mouse_x=MouseX()
     pr=Rand(0,255)
     pb=Rand(0,255)
     pg=Rand(0,255)


    ;Player controls 2 of each left and right to move the ball if move=0
    Keycontrols()


    ;Tells that if move=1\2 is activated somehow then that function is also activated
    If move=1 Then ballmovement()


    ;Draws the MouseDot
    Oval mouse_x,mouse_y,1,1

    ;Draws the ball
    DrawImage ball,ball_x,ball_y
    
    ;Draws the life's depending on the lifes veriable
    If lifes>1 Then
    Oval 1411,1005,20,20
  End If
    If lifes>2 Then
    Oval 1433,1005,20,20
  End If
    If lifes>3 Then
    Oval 1455,1005,20,20
  End If
  
    ;Draws the player controled object
    DrawImage player,player_x,player_y

    ;Draws the "Power" block and some of its functions E.G if reachs a point it resets
    Color pr,pb,pg
    Rect PL,power_y,60,38
    power_y = power_y+ 10
    If power_y = 1050 Then power_y = 0
    If power_y =0 Then PL=Rand(0,1580)
    
    ;Color switched back to white
    Color 255,255,255
  
    ;Draws the pillars using types
   For pillars.pillar=Each pillar
   If pillars\hp=1 Then Color pillars\r,pillars\b,pillars\g:Rect pillars\X,pillars\Y,100,30,1:Color 0,0,0:Rect pillars\X,pillars\Y,100,30,0
Next


    ;Switch the color back to white for the text
    Color 255,255,255

    If score=15000 Then
    Text 800,500,"YOU WIN!!"
    score=0
   End If



    ;Text and some maths
    Text 50,50,"Players X: "+player_x+" Players Y: "+player_y
    Text 50,65,"Ball X: "+ball_x+" Ball Y: "+ball_y
    Text 50,80,"Powerblock X:"+PL+" Powerblock Y: "+power_y
    Text 50,95,"MouseDot X: "+mouse_x+" MouseDot Y: "+mouse_y
    Text PL+7,power_y,"Power"
    Text 800,0,"Score: "+score

    ;Collision codes

  ;Ball -> Pillars
  For p.pillar=Each pillar
   If RectsOverlap(p\X,p\Y,100,30,ball_x,ball_y,15,15) Then
   score=score+1
   Delete p
   bal_ydir=1
   End If
  Next


;If the lifes go down to 0 then give 3 more lifes
If lifes=0 lifes=4


Color 255,255,255
Text 0, 0,(ball_x - player_x)*.01


;Flip all the actions into the front buffer
 Flip


Wend

;This function is activated from the "Move=" code if the = value is 1 this is activated
Function ballmovement()

newspeedx = (ball_x - player_x)*.01

ball_x = ball_x + bal_xdir*5
ball_y = ball_y + bal_ydir*5

If ball_y < 0 Then bal_ydir = 1
If ball_y > 1040 Then move=0 lifes=lifes-1
If ball_x < 0 Then bal_xdir = 1
If ball_x > 1670 Then bal_xdir = -1

If ImagesCollide (ball,ball_x,ball_y,0,player,player_x,player_y,0) Then bal_ydir = -1: bal_xdir = newspeedx 

If move=0 Then
ball_y=player_y+-20
ball_x=player_x
End If

End Function




Function Keycontrols()
    If KeyDown(LEFT_ARR) And player_x>0 Then
    player_x = player_x -10
   End If
    If KeyDown(LEFT_ARR) And move=0 And ball_x>40 Then
    ball_x = ball_x + -10
   End If
    If KeyDown(RIGHT_ARR) And player_x<1580 Then
    player_x = player_x + 10
   End If
    If KeyDown(RIGHT_ARR) And move=0 And ball_x<1620 Then
    ball_x = ball_x + 10
   End If
    If KeyDown(SPAC_BAR) Then
    move=1   
   End If
End Function



Yahfree(Posted 2007) [#29]
btw how do i make a scroll bar next to the code so its smaller, because this thread could use a bit of cleaning :)


Yahfree(Posted 2007) [#30]
Alright i figured it out: somthing was wrong with the veriable, so i just make the bal_xdir=(ball_x - player_x)

The raw value, would be nice if i could dress it up later but still its good to know it works, anyways on to bigger problems:

i cant seem to figure out how to make it know when the ball hits a different side of the pillar.

This is what i use now to bounce the ball back when it collides:

  For p.pillar=Each pillar
   If RectsOverlap(p\X,p\Y,100,30,ball_x,ball_y,15,15) Then
   score=score+1
   Delete p
   bal_ydir=1
   End If
  Next


Now this works, but it only works if the ball hits it from the bottom, when it hits from the sides or the top it just keeps going.

Any ideas? btw thanks for all the help, if this goes well, this will be my first working game, and i'll credit you guys for helping me in my first steps to programming.


b32(Posted 2007) [#31]
This is what I tried for the paddle collision:
If ImagesCollide (ball,ball_x,ball_y,0,player,player_x,player_y,0) Then 
	bal_ydir = -1
        bal_xdir=(ball_x - player_x)  / 12
End If

Hmm, for bouncing off the pilars, I would use this:
   bal_ydir=-Sgn(p\Y - ball_y)
   bal_xdir=-Sgn(p\X - ball_x)

However, when the ball hits the pilar at the same height, ball_ydir becomes zero and the ball gets stuck there. So then using an If statement would be better.
Ie:
If p\y < ball_y then bal_ydir = -1 else bal_ydir = 1


Yahfree(Posted 2007) [#32]
i cant seem to get eather of them to work, could you show me how you would put them into work in the collisions code? because the first two sort of work but the bal_xdir calculation is messed up some where cause the ball always flys off in one direction, the second two i cant get to work at all...

Thanks for the help though


b32(Posted 2007) [#33]
Here, I used RectsOverlap to determine what side the ball hits the pilar:



Yahfree(Posted 2007) [#34]
Alright thank you for all the help and for putting up with my newbynessness, anyways, i'll let ya know if any more problems arise.


Yahfree


Yahfree(Posted 2007) [#35]
oh heres a question, how do i convert the normal pillars into real image pillars like i did with the ball and the paddle?


Fernhout(Posted 2007) [#36]
Hey guys.
I did like what you are doing here. Greet stuff.
I have a question for you all. Is it possible to bring this toppic to a tutotial part. Because the furter i come the more i think its a good part for helping other people to understand Blitz. And this is a good way.

Please post a massage to the people of this site and ask for it.

Keep this nice work going....


Yahfree(Posted 2007) [#37]
Fine by me. I learned alot i'm making this breakout clone to help me understand the basics of programming, i don't know any other languages only html, so yeah, i plan on this breakout clone being my first game, i'm in the process of replacing some of the basic shapes with images i made through photoshop, then i need to figure out SFX and music, cause i'm sure this rock ya like a hurricane is all copy-righted.

feel free to make a tutorial out of this, all i request is some credits for making the game also i request you credit b32 for all the help along with all the others that helped me taking my first steps into game programing.


b32(Posted 2007) [#38]
Hmm, aren't there any breakout tutorials yet? I thought I seen one a while ago.
With drawing images for the pillars, did you mean:
For p.pillar = each pillar
drawimage pillarimage, p\x, p\y

?


Yahfree(Posted 2007) [#39]
ya already figured it out do you know how to change the EXE icon?


b32(Posted 2007) [#40]
I believe it can be done using Reshacker
http://www.blitzbasic.co.nz/Community/posts.php?topic=55871


Yahfree(Posted 2007) [#41]
interesting, heres a question, you know where i could find some free sound effects?


Fernhout(Posted 2007) [#42]
Look around on the internet. Use google to find some. Thats the same way i did it. There is a lot of sites who offes sound files to use. The only thing they wil ask is that you mentioned, you did get your sound from them. Thats all. Even if you try to find some graphics there is something to find for free.

The last game i did make in another basic program. I did use some sound from the site http://www.soundforfree.org. Its a big site with tons of differend sounds effects and even music. in midi, mp3 or wav.


Fernhout(Posted 2007) [#43]
Sorry for the wrong site. This site is replaced to another site. This link is working. http://www.a1freesoundeffects.com
Here you can find your sound effect you like.
have fun on the site.


Yahfree(Posted 2007) [#44]
cool, i cant seem to find what i'm looking for.... I'm looking for a sound of like bricks crashing, like when the ball hits the pillar, and a nother of a paddle type thing

Any ideas?


Fernhout(Posted 2007) [#45]
The only thing that i can say is try to find it on the internet.


b32(Posted 2007) [#46]
Or grab a microphone and two bricks and record it with a soundrecording program