Hi
I'm new to DBP and i tried to write a game (dont remember the name, it's similar to Pong)
The Idea is that when the Ball (Box) hits a Brick (Box) then it gets deleted and continues to fly around, like if it hits from right and is flying up the Ball goes now right and up because it hit the Bricks right side and so on from every side.
But somehow it sometimes hit the Bricks but gets the wrong direction.
I realy dont know why, i tried to figure it out for an hour but i dont realy get it.
I included the Code and the Program so You can try it out
here the Main code
#include "Functions.dba"
sync on
sync rate 60
autocam off
position camera 18,-40,-75
SET TEXT FONT "verdana"
set Text Size 20
gosub Create_Level
points=0
cubes=36
GameOver=0
#constant playerID=100
#constant ballID=101
#constant mv_right=1
#constant mv_left=2
#constant mv_up=3
#constant mv_down=4
moving_LR=mv_right
moving_UD=mv_up
rem **************************************************************
rem *********************** Main Loop ****************************
rem **************************************************************
do
px=object position x(playerID)
if (GameOver=0)
gosub Player_Control
gosub Collision
endif
if (cubes=0)
set Text Size 100
Text 200,200,"You Win"
set Text Size 20
GameOver=1
endif
gosub Screen_Info
sync
loop
Here the Functions.dba
rem **************************************************************
rem *********************** Create Level *************************
rem **************************************************************
Create_Level:
rem Background
load image "./bg.bmp",1
make object plain 80,100,80
scale object 80,120,120,100
position object 80,18,-40,0
texture object 80,1
rem Create Gamefield
load image "./brix.bmp",2
for I=0 to 5 step 1
for J=1 to 6 step 1
make object box J+I*6,5,5,5
texture object J+I*6,2
position object J+I*6,I*8,-J*8,0
next
next
rem Walls
rem Top
make object box 91,60,1,5
position object 91,20,0,0
SC_SetupObject 91,0,2
rem Left
make object box 92,1,80,5
position object 92,-10,-40,0
SC_SetupObject 92,0,2
rem Right
make object box 93,1,80,5
position object 93,50,-40,0
SC_SetupObject 93,0,2
rem Set Collision for Gamefield
for I=1 to 36 step 1
SC_SetupObject I,0,2
next
rem Create Player
make object box playerID,10,3,5
position object playerID,0,-80,0
SC_SetupObject playerID,0,2
return
rem **************************************************************
rem *********************** Player Control ***********************
rem **************************************************************
Player_Control:
rem Move Player
if (px>-5) then if (leftkey()) then move object left playerID,1
if (px<45) then if (rightkey()) then move object right playerID,1
sc_updateobject playerID
rem Shoot Ball
if (spacekey() && object exist(ballID)=0)
make object box ballID,1,1,1
position object ballID,px,object position y(playerID)+3,0
SC_SetupObject ballID,0,2
endif
return
rem **************************************************************
rem *********************** Collision ****************************
rem **************************************************************
Collision:
rem Check Collision, Update Points
if (object exist(ballID)=1)
if (object position y(ballID)<0 && object position y(ballID)>-90 )
sc_updateobject ballID
col=SC_ObjectCollision (ballID,0)
rem If Ball hits upper Walls
If (col=91)
if (moving_UD=mv_up)
moving_UD=mv_down
move object down ballID,1
else
if (moving_UD=mv_down)
moving_UD=mv_up
move object up ballID,1
endif
endif
endif
rem If Ball hits Left or Right Wall
if (col=92 or col=93)
if (moving_LR=mv_right)
moving_LR=mv_left
move object left ballID,1
else
if (moving_LR=mv_left)
moving_LR=mv_right
move object right ballID,1
endif
endif
endif
rem If Ball hits Player
if (col=playerID)
moving_UD=mv_up
move object up ballID,1
endif
rem If Ball hits Bricks
if (col > 0 && col < 90)
rem Ball Hits Leftside of Brick
position object ballID,object position x (ballID)+1,object position y (ballID),0
sc_updateobject ballID
Ncol=SC_ObjectCollision (ballID,0)
if (Ncol>0) then moving_LR=mv_left
position object ballID,object position x (ballID)-1,object position y (ballID),0
rem Ball Hits Rightside of Brick
position object ballID,object position x (ballID)-1,object position y (ballID),0
sc_updateobject ballID
Ncol=SC_ObjectCollision (ballID,0)
if (Ncol>0) then moving_LR=mv_right
position object ballID,object position x (ballID)+1,object position y (ballID),0
rem Ball Hits Underside of Brick
position object ballID,object position x (ballID),object position y (ballID)+1,0
sc_updateobject ballID
Ncol=SC_ObjectCollision (ballID,0)
if (Ncol>0) then moving_UD=mv_down
position object ballID,object position x (ballID),object position y (ballID)-1,0
rem Ball Hits Upperside of Brick
position object ballID,object position x (ballID),object position y (ballID)-1,0
sc_updateobject ballID
Ncol=SC_ObjectCollision (ballID,0)
if (Ncol>0) then moving_UD=mv_up
position object ballID,object position x (ballID),object position y (ballID)+0.5,0
SC_RemoveObject col
delete object col
points=points+10
dec cubes
endif
rem If Ball isnt hitting
if (col=0)
select moving_LR
case mv_right:
move object right ballID,0.5
endcase
case mv_left:
move object left ballID,0.5
endcase
endselect
select moving_UD
case mv_up:
move object up ballID,0.5
endcase
case mv_down:
move object down ballID,0.5
endcase
endselect
endif
else
delete object ballID
SC_RemoveObject ballID
moving_LR=mv_right
moving_UD=mv_up
endif
endif
return
rem **************************************************************
rem *********************** Screen Info **************************
rem **************************************************************
Screen_Info:
rem Screen Infos
Text 450, 30,"X : " : Text 500, 30,Str$(object position x(playerID))
Text 650, 30,"Y : " : Text 700, 30,Str$(object position Y(playerID))
Text 200, 30,"Points : " : Text 300, 30,Str$(points)
Text 0,0,"FPS : "+str$(screen fps())
Text 0,20,"UD : "+str$(moving_UD)
Text 0,40,"LR : "+str$(moving_LR)
set window title "Brix "+" FPS : "+str$(screen fps())
return