News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

My BreakOut Game

Started by markel422, November 12, 2009, 08:21:07 PM

Previous topic - Next topic

Big C.

hi markel422,

can u more explain the meaning of ItemMax and why want you dim the itemtype again?

What is your game design/logic behind the powerup grab? Normally in games like arkanoid the grab function will grab the ball at the paddle. The player can use this powerup as long as the player catch a new powerup or lost the ball. Is this what your powerup shall do?

Btw it isn't easy to read your code because i miss the recurrent theme. Your game loop is very big and i think there is many code which can perfect implemented in a Psub/Function ;)

Big C.





markel422

Hi Big C. :)

Quotecan u more explain the meaning of ItemMax and why want you dim the itemtype again?

Well with the variable ItemMax, I am able to control how many items can only appear at a time when it is given the means to spawn (as to just fall down). The reason with the redim is technically why I need help on how to approach this code, because of each of the items from before labeled like this on Lines 71-85:

;Dimension for GreenItem
ItemMax=1
Dim GIAppear(ItemMax)
Dim GIposX(ItemMax)
Dim GIposY(ItemMax)
;Dimension for BlueItem
Dim BIAppear(ItemMax)
Dim BIPosX(ItemMax)
Dim BIPosY(ItemMax)
;Dimension for YellowItem
Dim YIAppear(ItemMax)
Dim YIPosX(ItemMax)
Dim YIPosY(ItemMax)


So for me to even try and do this again I would need to find some way in order to still dim the ItemMax while still doing the Array Method.

QuoteWhat is your game design/logic behind the powerup grab? Normally in games like arkanoid the grab function will grab the ball at the paddle. The player can use this powerup as long as the player catch a new powerup or lost the ball. Is this what your powerup shall do?

Yeah that was what I was trying to aim for, but now it is because of these Arrays that I need to Fix up, I cannot fix it right now until I change the bunch of items into one array so that the the code will be more clean.

QuoteBtw it isn't easy to read your code because i miss the recurrent theme. Your game loop is very big and i think there is many code which can perfect implemented in a Psub/Function

As with the Psub/Functions, I am still kinda having trouble on how to properly put all of the code right (when it comes to Functions). I still don't know how to Correctly do that yet, but I will probably attempt it once I am able to get all of this fixed ;).

kevin

#32
  From what i can see, you've convert the Ball1 to an array, but the code still seems to be treating  them differently.  

 So  i've stripped your code down to leave a bouncing ball.  

PlayBASIC Code: [Select]
   Setfps 60

///Normal BallSize
BallSize =8

type tBall
x
y ;The Position for the Balls
speedx
speedy ; The Speed for the Balls
Size ; Size for each of the Balls
Colour
endtype

; create an array with space for 3 balls
Dim Ball(3) as tball

; init the balls starting states
gosub Ball_Restart



; Play game loop
Do

Cls 0

;Handle Ball Movement(rebound off screen edges)
if ball(1).x + ball(1).size > getscreenwidth()
ball(1).x = getscreenwidth() - ball(1).size
ball(1).speedx = ball(1).speedx * -1
endif

if ball(1).x - ball(1).size < 0
ball(1).x = 0 + ball(1).size
ball(1).speedx=ball(1).speedx * -1
endif

if ball(1).y - ball(1).size < 0
ball(1).y = 0 + ball(1).size
ball(1).speedy=ball(1).speedy * -1
endif

if ball(1).y + ball(1).size >GetScreenHeight()
ball(1).y = GetScreenHeight()- ball(1).size
ball(1).speedy=ball(1).speedy * -1
endif

; draw it
circle ball(1).x,Ball(1).y,Ball(1).size,true
; move it for next frame
ball(1).x=ball(1).x+ball(1).Speedx
ball(1).y=ball(1).y+ball(1).Speedy



; draw the screen
Sync
Loop


//=============================================================================
//ROUTINES
//============================================================================

Ball_Restart:

;Create Ball One, in array Index #1
Ball(1).x=400
Ball(1).y=300
Ball(1).speedx=rndrange(2,3)
Ball(1).speedy=rndrange(3,4)
Ball(1).Size=Ballsize
Ball(1).Colour=RGB(0,0,200)

;Create Ball Two, in array Index #2
Ball(2).x=350
Ball(2).y=300
Ball(2).speedx=rndrange(2,3)
Ball(2).speedy=rndrange(3,4)
Ball(2).Size=Ballsize
Ball(2).Colour=RGB(0,0,200)

;Create Ball Three, in array Index #3
ball(3).x = 400
ball(3).y = 300
ball(3).speedx = rndrange(2,3)
ball(3).speedy = rndrange(3,4)
ball(3).size=Ballsize
ball(3).colour=rgb(0,0,200)

return





  The code declares the Ball Objects type  (what properties each ball has), then Dimensions the Ball array  (See DIM, ARRAY BASIC sin the HELP->ABOUT->ARRAYBASIC)   The subroutine then fills in the individual balls.

  Now in the game loop, where processing & drawing ball object #1.   So while the other balls exist (Run in debug mode F7 and view the Ball array), we'll never seen them.   Now while the code does what we've told it, it's not actually


PlayBASIC Code: [Select]
   Setfps 60

///Normal BallSize
BallSize =8

type tBall
x
y ;The Position for the Balls
speedx
speedy ; The Speed for the Balls
Size ; Size for each of the Balls
Colour
endtype

; create an array with space for 3 balls
Dim Ball(3) as tball

; init the balls starting states
gosub Ball_Restart



; Play game loop
Do

Cls 0

; loop through each index (ball) in the ball array and update it's logoc and draw it
For lp=1 to 3

;Handle Ball Movement(rebound off screen edges)
if ball(lp).x + ball(lp).size > getscreenwidth()
ball(lp).x = getscreenwidth() - ball(lp).size
ball(lp).speedx = ball(lp).speedx * -1
endif

if ball(lp).x - ball(lp).size < 0
ball(lp).x = 0 + ball(lp).size
ball(lp).speedx=ball(lp).speedx * -1
endif

if ball(lp).y - ball(lp).size < 0
ball(lp).y = 0 + ball(lp).size
ball(lp).speedy=ball(lp).speedy * -1
endif

if ball(lp).y + ball(lp).size >GetScreenHeight()
ball(lp).y = GetScreenHeight()- ball(lp).size
ball(lp).speedy=ball(lp).speedy * -1
endif

; draw it
circle ball(lp).x,Ball(lp).y,Ball(lp).size,true
; move it for next frame
ball(lp).x=ball(lp).x+ball(lp).Speedx
ball(lp).y=ball(lp).y+ball(lp).Speedy

next lp


; draw the screen
Sync
Loop


//=============================================================================
//ROUTINES
//============================================================================

Ball_Restart:

;Create Ball One, in array Index #1
Ball(1).x=400
Ball(1).y=300
Ball(1).speedx=rndrange(2,3)
Ball(1).speedy=rndrange(3,4)
Ball(1).Size=Ballsize
Ball(1).Colour=RGB(0,0,200)

;Create Ball Two, in array Index #2
Ball(2).x=350
Ball(2).y=300
Ball(2).speedx=rndrange(2,3)
Ball(2).speedy=rndrange(3,4)
Ball(2).Size=Ballsize
Ball(2).Colour=RGB(0,0,200)

;Create Ball Three, in array Index #3
ball(3).x = 400
ball(3).y = 300
ball(3).speedx = rndrange(2,3)
ball(3).speedy = rndrange(3,4)
ball(3).size=Ballsize
ball(3).colour=rgb(0,0,200)

return



  Here we're processing all the objects in BALL array.   While this works, it's clunkys, because you've hard coded what indexs the balls use.

  We can eleminate that by rather than explicitly nominating what Indexs the balls use, we ask PB to find a free one for us.  This will require a slight logic change to the ball processing loop, but that's it.


PlayBASIC Code: [Select]
   Setfps 60

///Normal BallSize
BallSize =8

type tBall
x
y ;The Position for the Balls
speedx
speedy ; The Speed for the Balls
Size ; Size for each of the Balls
Colour
endtype

; create an array with space for 3 balls
Dim Ball(3) as tball

; init the balls starting states
gosub Ball_Init


; Play game loop
Do

Cls 0

; loop through each index in the ball array.

For lp=1 to GetArrayElements(Ball(),1)

; check if this ball exists?, if it does, we update it's logic and draw it
if Ball(lp)

;Handle Ball Movement(rebound off screen edges)
if ball(lp).x + ball(lp).size > getscreenwidth()
ball(lp).x = getscreenwidth() - ball(lp).size
ball(lp).speedx = ball(lp).speedx * -1
endif

if ball(lp).x - ball(lp).size < 0
ball(lp).x = 0 + ball(lp).size
ball(lp).speedx=ball(lp).speedx * -1
endif

if ball(lp).y - ball(lp).size < 0
ball(lp).y = 0 + ball(lp).size
ball(lp).speedy=ball(lp).speedy * -1
endif

if ball(lp).y + ball(lp).size >GetScreenHeight()
ball(lp).y = GetScreenHeight()- ball(lp).size
ball(lp).speedy=ball(lp).speedy * -1
endif

; draw it
circle ball(lp).x,Ball(lp).y,Ball(lp).size,true
; move it for next frame
ball(lp).x=ball(lp).x+ball(lp).Speedx
ball(lp).y=ball(lp).y+ball(lp).Speedy

endif
next lp


if Spacekey()
gosub Add_Ball
FlushKeys
endif



; draw the screen
Sync
Loop


//=============================================================================
//ROUTINES
//============================================================================

Ball_Init:

;Create Ball One, in whatever index is free in BALL()
Index=GetFreeCell(Ball())
Ball(Index).x=400
Ball(Index).y=300
Ball(Index).speedx=rndrange(2,3)
Ball(Index).speedy=rndrange(3,4)
Ball(Index).Size=Ballsize
Ball(Index).Colour=RGB(0,0,200)

;Create Ball Two
Index=GetFreeCell(Ball())
Ball(Index).x=350
Ball(Index).y=300
Ball(Index).speedx=rndrange(2,3)
Ball(Index).speedy=rndrange(3,4)
Ball(Index).Size=Ballsize
Ball(Index).Colour=RGB(0,0,200)

;Create Ball Three
Index=GetFreeCell(Ball())
ball(Index).x = 400
ball(Index).y = 300
ball(Index).speedx = rndrange(2,3)
ball(Index).speedy = rndrange(3,4)
ball(Index).size=Ballsize
ball(Index).colour=rgb(0,0,200)

return




//=============================================================================
//ROUTINES
//============================================================================

Add_Ball:

;Ask PB for a free index within the BALL() array. If none exist, Pb will
; expand the array for us.
Index=GetFreeCell(Ball())
Ball(Index).x=400
Ball(Index).y=300
Ball(Index).speedx=rndrange(-3,3)
Ball(Index).speedy=rndrange(-4,4)
Ball(Index).Size=Ballsize
Ball(Index).Colour=rndrgb()

return






And here's a version with deletion.  This version that allows the user to click a ball to kill it.  Press SPACE to add balls to the scene.

PlayBASIC Code: [Select]
   Setfps 60

///Normal BallSize
BallSize =20

type tBall
x
y ;The Position for the Balls
speedx
speedy ; The Speed for the Balls
Size ; Size for each of the Balls
Colour
endtype

; create an array with space for 3 balls
Dim Ball(3) as tball

; init the balls starting states
gosub Ball_Init


; Play game loop
Do

Cls 0


NumberOfBallsAlive=0

; loop through each index in the ball array.
For lp=1 to GetArrayElements(Ball(),1)

; check if this ball exists?, if it does, we update it's logic and draw it
if Ball(lp)
inc NumberOfBallsAlive

;Handle Ball Movement(rebound off screen edges)
if ball(lp).x + ball(lp).size > getscreenwidth()
ball(lp).x = getscreenwidth() - ball(lp).size
ball(lp).speedx = ball(lp).speedx * -1
endif

if ball(lp).x - ball(lp).size < 0
ball(lp).x = 0 + ball(lp).size
ball(lp).speedx=ball(lp).speedx * -1
endif

if ball(lp).y - ball(lp).size < 0
ball(lp).y = 0 + ball(lp).size
ball(lp).speedy=ball(lp).speedy * -1
endif

if ball(lp).y + ball(lp).size >GetScreenHeight()
ball(lp).y = GetScreenHeight()- ball(lp).size
ball(lp).speedy=ball(lp).speedy * -1
endif

; draw it
circle ball(lp).x,Ball(lp).y,Ball(lp).size,true


; move it for next frame
ball(lp).x=ball(lp).x+ball(lp).Speedx
ball(lp).y=ball(lp).y+ball(lp).Speedy


; check if the player is clicking the ball, to kill it
if MouseButton()=1

; check if the user is clicking on ball
if CirclesIntersect(MouseX(),Mousey(),5,ball(lp).x,Ball(lp).y,Ball(lp).size)=true
; if it is then we kill this ball.

ball(lp) = NULL

endif
endif


endif
next lp


if Spacekey()
gosub Add_Ball
FlushKeys
endif

; print the number of balls that are alive
print NumberOfBallsAlive

; draw the screen
Sync
Loop


//=============================================================================
//ROUTINES
//============================================================================

Ball_Init:

;Create Ball One, in whatever index is free in BALL()
Index=GetFreeCell(Ball())
Ball(Index).x=200
Ball(Index).y=300
Ball(Index).speedx=rndrange(2,3)
Ball(Index).speedy=rndrange(3,4)
Ball(Index).Size=Ballsize
Ball(Index).Colour=RGB(0,0,200)

;Create Ball Two
Index=GetFreeCell(Ball())
Ball(Index).x=350
Ball(Index).y=300
Ball(Index).speedx=rndrange(2,3)
Ball(Index).speedy=rndrange(3,4)
Ball(Index).Size=Ballsize
Ball(Index).Colour=RGB(0,0,200)

;Create Ball Three
Index=GetFreeCell(Ball())
ball(Index).x = 400
ball(Index).y = 300
ball(Index).speedx = rndrange(2,3)
ball(Index).speedy = rndrange(3,4)
ball(Index).size=Ballsize
ball(Index).colour=rgb(0,0,200)

return




//=============================================================================
//ROUTINES
//============================================================================

Add_Ball:

;Ask PB for a free index within the BALL() array. If none exist, Pb will
; expand the array for us.
Index=GetFreeCell(Ball())
Ball(Index).x=400
Ball(Index).y=300
Ball(Index).speedx=rndrange(-3,3)
Ball(Index).speedy=rndrange(-4,4)
Ball(Index).Size=Ballsize
Ball(Index).Colour=rndrgb()
Login required to view complete source code




Rather than repeat myself.  See Character Life Cycles
 

markel422

Aahhhh...Ok, So that's how "GetFreeCell" and "GetArrayElements" work. When the Index variable represents any New Array number given, GetFreeCell automatically finds any Array space that is available and even if it can't, it still creates a new space thus Forcefully expanding the Array, while GetArrayElements checks to see how many spaces were Dimmed from the start and even when it is expanded. When I was first shown this before, I didn't understand it due to me not understanding the functionality of "Functions" at the time (which was the many various passings of variables from a Function to the Main Loop). But now after working with the Arrays all the time, I now see how important those two codes REALLY are and how I can use it in my Game. :)

Although I'm kinda having trouble understanding the Cos, Sin, and Tan logic. Is Cos what draws "Left & Right" of Degrees, Sin draws "Up & Down" of Degrees, What is Tan? Diagonal???

kevin

#34
cos, sin, tan

Cos & Sin are the useful ones



Bellow is a quick revision of the example above, except this one show a contents of the array on the side of the screen.  it shows the size of the array, and what cells are currently in use (have objects in them).   It draws a lines between to the cell and the object on screen to give you some ideas of what index in the array is what object on screen.     It also show empty cells, so you see what's actually happening.

  Space bar to add objects,  click on them with the mouse to delete them.






Setfps 60

///Normal BallSize
BallSize =20

type tBall
x
y ;The Position for the Balls
speedx
speedy ; The Speed for the Balls
Size ; Size for each of the Balls
  Colour
endtype

; create an array with space for 3 balls
Dim Ball(3) as tball

; init the balls starting states
gosub Ball_Init

 
; Play game loop
Do

Cls 0


NumberOfBallsAlive=0

; loop through each index in the ball array.
For lp=1 to GetArrayElements(Ball(),1)

; check if this ball exists?, if it does, we update it's logic and draw it  
if Ball(lp)
inc NumberOfBallsAlive
       
;Handle Ball Movement(rebound off screen edges)
if ball(lp).x + ball(lp).size > getscreenwidth()
 ball(lp).x = getscreenwidth() - ball(lp).size
 ball(lp).speedx = ball(lp).speedx * -1
endif

if ball(lp).x - ball(lp).size < 0
ball(lp).x = 0 + ball(lp).size
ball(lp).speedx=ball(lp).speedx * -1    
  endif

if ball(lp).y - ball(lp).size < 0
  ball(lp).y = 0 + ball(lp).size
ball(lp).speedy=ball(lp).speedy * -1
  endif
 
if ball(lp).y + ball(lp).size >GetScreenHeight()
ball(lp).y = GetScreenHeight()- ball(lp).size
ball(lp).speedy=ball(lp).speedy * -1
  endif

; draw it
circle ball(lp).x,Ball(lp).y,Ball(lp).size,true


; move it for next frame
ball(lp).x=ball(lp).x+ball(lp).Speedx
ball(lp).y=ball(lp).y+ball(lp).Speedy


; check if the player is clicking the ball, to kill it
if MouseButton()=1

; check if the user is clicking on ball
if CirclesIntersect(MouseX(),Mousey(),5,ball(lp).x,Ball(lp).y,Ball(lp).size)=true
; if it is then we kill this ball.

ball(lp) = NULL

endif
endif


endif
next lp


if Spacekey()
gosub Add_Ball
FlushKeys
endif

; print the number of balls that are alive
print "NumberOf Living Objects:"+Str$(NumberOfBallsAlive)


DrawBallArray(0,50)


; draw the screen
Sync
Loop


//=============================================================================
//ROUTINES
//============================================================================

Ball_Init:

;Create Ball One, in whatever index is free in BALL()
Index=GetFreeCell(Ball())
  Ball(Index).x=200
  Ball(Index).y=300
  Ball(Index).speedx=rndrange(2,3)
  Ball(Index).speedy=rndrange(3,4)
  Ball(Index).Size=Ballsize
  Ball(Index).Colour=RGB(0,0,200)
 
;Create Ball Two
Index=GetFreeCell(Ball())
  Ball(Index).x=350
  Ball(Index).y=300
  Ball(Index).speedx=rndrange(2,3)
  Ball(Index).speedy=rndrange(3,4)
  Ball(Index).Size=Ballsize
  Ball(Index).Colour=RGB(0,0,200)

;Create Ball Three
Index=GetFreeCell(Ball())
  ball(Index).x = 400
  ball(Index).y = 300
  ball(Index).speedx = rndrange(2,3)
  ball(Index).speedy = rndrange(3,4)
  ball(Index).size=Ballsize
  ball(Index).colour=rgb(0,0,200)

return




//=============================================================================
//ROUTINES
//============================================================================

Add_Ball:

;Ask PB for a free index within the BALL() array.  If none exist, Pb will
; expand the array for us.
Index=GetFreeCell(Ball())
  Ball(Index).x=400
  Ball(Index).y=300
  Ball(Index).speedx=rndrange(-3,3)
  Ball(Index).speedy=rndrange(-4,4)
  Ball(Index).Size=Ballsize
  Ball(Index).Colour=rndrgb()

return


; This functions draws a graphicsl representation of what is going on in the Ball() array

Function DrawBallArray(Xpos,Ypos)

; ask Pb how big this array currently is
Size=getArrayElements(Ball(),1)

CellWidth=100
CellHeight=GetTextHeight("Y")+5

Height=CellHeight*(Size)

c1=rgb(255,0,250)
c2=rgb(55,10,50)


ink rgb(0,255,0)
Text Xpos,Ypos,"Array Size="+Str$(Size)
Ypos=Ypos+CellHeight
Shadebox xpos,ypos,xpos+CellWidth,Ypos+Height,c1,c2,c2,c1

PadX=5
X=Xpos+PadX
Y=Ypos

for lp=1 to Size
if Ball(lp)
ink rgb(0,255,0)
text X,Y,"#"+Digits$(lp,2)

x2=Xpos+CellWidth
y2=Y+CellHeight/2
linec x2,y2,Ball(lp).x,Ball(lp).y,rgb(0,0,255)


else
Boxc x,y,x+CellWidth-PadX,Y+Cellheight,true,rgb(100,0,0)
ink rgb(255,0,0)
text X,Y,"#EMPTY"
endif

Y=Y+Cellheight
next
ink rgb(255,255,255)

EndFunction





markel422

I am completely remodifying my whole code in order for me to start from scratch to put my Array codes in. So far after changing alot things, it is going quite well, even though there were a few slow downs. Now instead of my items spawning when a Player reach a certain amount of "PlayerScore", these items will now only spawn when a random ranged number reach a certain number. But there's a problem with even using this system...the items now keep respawning in different locations everytime the Rnd number equals true, is there anyway to turn this Rnd Range System OFF while an item spawn is happening or to at least keep it from interfering with the item spawns?

Please Note: (I Put the Rnd Number Range Low on Purpose (Lines 154-157) in order to see if this strange thing is still happening.)

Here's the remodifed Code:

openscreen 800,600,32,1
setfps 60
IWidth=8
IHeight=8
Width=90
Height=15
PlayerSize=Width
playerx=355
playery=550
Ballsize=8
PlayerNormalState=0
PlayerScore=0
sw=getscreenwidth()
sh=getscreenheight()
Green=0
Blue=0
Constant Normal=7
Constant Shrunk=8

type tBall
x,y
speedx
speedy
size
colour
endtype
Dim Ball(1) as tBall
Dim bl(160)

type tItem
Appear
PosX
PosY
endtype
Dim Item(2) as tItem
ItemCount=0
ItemMax=1
Appear=0
gosub Init_Ball


Do
Cls 0
   PlayerState=Normal
   playerx=MouseX()
   
      NumberOfBalls=0
   for lp=1 to GetArrayElements(Ball(),1)
   
    ;To see if the Balls exist
    if ball(lp)
   
   
    inc NumberOfBalls
    x = ball(lp).x+ball(lp).speedx
    y = ball(lp).y+ball(lp).speedy
    radius=ball(lp).size
    colour=ball(lp).colour
   
    if x-radius < 0
       x = 0+radius
       ball(lp).speedx=ball(lp).speedx*-1
       endif
   
    if x+radius > sw
    x = sw-radius
    ball(lp).speedx=ball(lp).speedx*-1
       endif
   
    if y-radius < 0
    y = 0+radius
    ball(lp).speedy=ball(lp).speedy*-1
    endif
               
NumberOfBlocksLeft=0
BA =0
for blocky = 100 to 280 step 20
for blockx = 1 to 751 step 50

; check if block exists
    if bl(ba)=0
; draw block
        if blocky>99 and blocky<140 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,0,0)
        if blocky>139 and blocky<180 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,100,0)
        if blocky>179 and blocky<220 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(100,180,0)
        if blocky>219 and blocky<260 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(0,180,100)
        if blocky>259 and blocky<300 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(0,100,160)

; check if the ball might be hitting this block
          if PointInBox (x,y,blockx,blocky,blockx+48,blocky+18)
          bl(ba)=1
          ball(lp).speedy=ball(lp).speedy * -1
          PlayerScore=PlayerScore+50
               endif
             
; bump our counter that represents the number of blocks that are still alive
inc NumberOfBlocksLeft
    endif

      Inc BA
      next
   next
       
         
       
       
        If PlayerGotShrunk=1
      If Playernormalstate=0
          PlayerState=Shrunk
           Endif
        Endif
       
        If PlayerState=Shrunk
           PlayerNormalState=timer()+10000 ;Add 10 second timer
     PlayerSize=width/2 ;Shrink Player Half it's Width
        Endif
     
      If Timer()>PlayerNormalState ;Did timer pass 10 seconds? If did then...
        PlayerNormalState = 0 ;Player is Back to Normal
        PlayerGotShrunk=0
        PlayerSize=Width ;Player will Come to back to it's origial width
        PlayerState=Normal
        Endif 
           
            if Playerx < 0 then Playerx = 0 + PLayerSize
            if Playerx+PlayerSize > sw then Playerx = sw - PlayerSize
   
            if PointinBox (x,y,playerx,playery,playerx+PlayerSize,Playery+Height)
            ball(lp).speedy=ball(lp).speedy*-1
            endif
   
            boxc playerx,playery,playerx+PlayerSize,playery+Height,1,RGB(180,0,0)
    circlec x,y,radius,1,colour
   
    ball(lp).x=x
    ball(lp).y=y
   
       Endif;End of "Ball(obj)"....
   
   if ball(lp).y > sh
    ball(lp)=NULL
   endif
   
      next lp  
     
//BALL LOOP ENDS HERE----------------------------------------------------------------
       
         if Rnd(50)=5
          Green=1
         endif
         if Rnd(60)=6
          Blue=1
         endif
         
         if Green=1
          gosub GreenItemSpawn
            Green=2
         endif
         if Green=2
          gosub GreenItem
         endif
         
         If Blue=1
          gosub BlueItemSpawn
            Blue=2
         endif
         if Blue=2
          gosub BlueItem
         endif
         
  if NumberOfBalls=0
    gosub Init_Ball
      endif
  if spacekey()=true
     gosub Add_Ball
  endif
  print "Number Of Balls: "+Str$(NumberOfBalls)
  print "ItemCount: "+Str$(ItemCount)
  print "PlayerScore: "+Str$(PlayerScore)
  print "PlayerNormalState: "+Str$(playernormalstate)
  print "PlayerState: "+Str$(PlayerState)
  print "Green: "+Str$(Green)
  print "BLue: "+Str$(Blue)
Sync
Loop
//Ball Variable Arrays---------------------------------------------------------
Init_Ball:
   Index=GetFreeCell(ball())
   ball(Index).x=400
   ball(Index).y=300
   ball(Index).speedx=rndrange(3,4)
   ball(Index).speedy=rndrange(4,5)
   ball(Index).size=ballsize
   ball(Index).colour=RGB(0,0,200)
Return

Add_Ball:
   Index=GetFreeCell(ball())
   ball(Index).x=400
   ball(Index).y=300
   ball(Index).speedx=rndrange(3,4)
   ball(Index).speedy=rndrange(4,5)
   ball(Index).size=ballsize
   ball(Index).colour=RGB(0,0,200)
   
   Index=GetFreeCell(ball())
   ball(Index).x=350
   ball(Index).y=300
   ball(Index).speedx=rndrange(3,4)
   ball(Index).speedy=rndrange(4,5)
   ball(Index).size=ballsize
   ball(Index).colour=RGB(0,0,200)
Return
//End of Ball Variable Arrays-----------------------------------------------------
GreenItemSpawn:
For I=0 to ItemMax
   Item(1).PosX=rndrange(20,780)
   Item(1).PosY=300
Next I
Return
BlueItemSpawn:
For I=0 to ItemMax
   Item(2).PosX=rndrange(20,780)
   Item(2).PosY=300
Next I
Return
GreenItem:
for obj= 1 to ItemMax
if Item(1)

   Boxc Item(1).PosX,Item(1).PosY,Item(1).PosX+IWidth,Item(1).PosY+IHeight,1,RGB(20,200,0)
   Item(1).PosY=Item(1).PosY+3
   Inc ItemCount
   
   If ItemCount > 0 then ItemCount=ItemMax
   
   If PointinBox(item(1).PosX,item(1).PosY,playerx,playery,playerx+playersize,playery+height)
    Item(1)=NULL
    PlayerGotShrunk=1
    Dec ItemCount
    Green=0
      endif
      If Item(1).PosY > sh
    Dec ItemCount
    Green=0
      endif
      if Green=0
      Item(1)=NULL
      Exit
      endif
      Endif
     
      print "GreenItemY: "+Str$(Item(1).PosY)
   Exit
   next obj
Return

BlueItem:
for obj= 1 to ItemMax
if Item(2)

   Boxc Item(2).PosX,Item(2).PosY,Item(2).PosX+IWidth,Item(2).PosY+IHeight,1,RGB(0,0,200)
   Item(2).PosY=Item(2).PosY+3
   Inc ItemCount
   
   If ItemCount > 0 then ItemCount=ItemMax
   
   If PointinBox(item(2).PosX,item(2).PosY,playerx,playery,playerx+playersize,playery+height)
    Item(2)=NULL
    PlayerGotShrunk=1
    Dec ItemCount
    Blue=0
      endif
      If Item(2).PosY > sh
    item(2)=NULL
    Dec ItemCount
    Blue=0
      endif
      if Blue = 0
      Item(2)=NULL
      Exit
      endif
      Endif
     
      print "BlueItemY: "+Str$(Item(2).PosY)
   Exit
   next obj
Return

kevin

#36
Quoteam completely remodifying my whole code in order for me to start from scratch to put my Array codes in. So far after changing alot things, it is going quite well, even though there were a few slow downs.

 Well, we've actually covered this problem previously..  See post



QuoteNow instead of my items spawning when a Player reach a certain amount of "PlayerScore", these items will now only spawn when a random ranged number reach a certain number. But there's a problem with even using this system...the items now keep respawning in different locations everytime the Rnd number equals true, is there anyway to turn this Rnd Range System OFF while an item spawn is happening or to at least keep it from interfering with the item spawns?

    ok so you're talking about this right ?



        if Rnd(50)=5
        Green=1
        endif
       
        if Green=1
        gosub GreenItemSpawn
              Green=2
        endif
        if Green=2
        gosub GreenItem
        endif


   
      ok, so one of the problems with the Items is in triggering.  There are others, but we'll get to those.

      Lets follow the logic of this code through and see what it's doing.

       In the first IF/ENDIF you're comparing with a Rnd(50)=5.  Sooner or later this is going to be true and PB will run the code inside the IF/ENDIF,  therefore setting the 'Green' variable to 1.  

      So when PB falls though to the next IF/ENDIF, Green will indeed equal 1, and PB will run the code inside this IF/ENDIF.   Which calls the 'init'  sub routine to initialize the green item and set the Green Variable to 2.    So PB won't call this init routine again on the next loop.    

     When PB hits the 3rd IF/ENDIF the Green variable will now equal 2, so the GreenItem subroutine is called..

     What's happening, is the logic is structured to always try and trigger a new Green item when the rnd() equals 5 .  So we need to restructure the code, so that when a green item is triggered (the first if/endif condition is true),  another one can't be triggered until the 'Green' variable is reset to zero again (assuming the when the green item leaves the screen or is collected by the player,  that the Green variable is reset to zero)

      This can be done in a few ways..



        ; check if Green=0 and RND(50)=5  ..  So both conditions need to be met, for PB to run the code inside this IF/ENDIF  
        if Green=0 and Rnd(50)=5
        Green=1
        endif
       
        if Green=1
        gosub GreenItemSpawn
              Green=2
        endif
        if Green=2
        gosub GreenItem
        endif



or




        ; check if Green=0
        if Green=0
           ; if RND(50)=5  ..
            if Rnd(50)=5
               ; So both conditions need to be met, for PB to run the code inside the inner IF/ENDIF  
        Green=1
            endif
        endif
       
        if Green=1
        gosub GreenItemSpawn
              Green=2
        endif
        if Green=2
        gosub GreenItem
        endif


      While these will both work, but we can clean this up a bit more by removing the middle IF/ENDIF statement, it's not needed.




        ; check if Green=0 and RND(50)=5  ..  So both conditions need to be met, for PB to run the code inside this IF/ENDIF  
        if Green=0 and Rnd(50)=5
        gosub GreenItemSpawn
        Green=2
        endif

        if Green=2
        gosub GreenItem
        endif





Big C.

#37
Hi Markel422,

Kevin gives you the main hint and some solution... I use them to modify your code a little bit  :P

So, examine it carefully to understand... I do changes to your gosub routines Green- and BlueItemSpawn ==> make them more flexible to use if more than 2 powerups will have to init, spawn  and use by player. I renamed it to Init_Item_Spawn. The same thing I have done with the routine Green/BlueSpawn ==> also put them in one flexible routine named Spawn_Item.

Question: I don't understand the using of the variable ItemMax because in the moment you have two Items to init and spawn. Have a look at

QuoteType tItem
   Appear
   PosX
   PosY
   RGB_Item_Color
EndType
Dim Item(2) As tItem  <== Here you dimmed 2 Items of tItem ==> this means the Max of Items is 2 (in fact Item(0) is Nr.3 ;-)) for init and spawn.

To demonstrate the flexibility of the new routines I initiate a third PowerUp by using Item(0) ;-)

BTW: 1. I have limit the fps to 30... so you can better watch what happened  ;) 2. This modified code isn't optimized because I think you are in a learning phase... But I will give you a Question to think about: What are you making if you want to implement a Background image?

greetings Big C.

modified code:
PlayBASIC Code: [Select]
OpenScreen 800,600,32,1
;SetFPS 60
SetFPS 30
IWidth=8
IHeight=8
Width=90
Height=15
PlayerSize=Width
playerx=355
playery=550
Ballsize=8
PlayerNormalState=0
PlayerScore=0
sw=GetScreenWidth()
sh=GetScreenHeight()
Green=0
Blue=0
Constant Normal=7
Constant Shrunk=8

Type tBall
x,y
speedx
speedy
size
colour
EndType
Dim Ball(1) As tBall
Dim bl(160)

Type tItem
Appear
PosX
PosY
RGB_Item_Color
EndType
Dim Item(2) As tItem

ItemCount=0
ItemMax=1
Appear=0
Gosub Init_Ball


Do
Cls 0
PlayerState=Normal
playerx=MouseX()

NumberOfBalls=0
For lp=1 To GetArrayElements(Ball(),1)

;To see if the Balls exist
If ball(lp)


Inc NumberOfBalls
x = ball(lp).x+ball(lp).speedx
y = ball(lp).y+ball(lp).speedy
radius=ball(lp).size
colour=ball(lp).colour

If x-radius < 0
x = 0+radius
ball(lp).speedx=ball(lp).speedx*-1
EndIf

If x+radius > sw
x = sw-radius
ball(lp).speedx=ball(lp).speedx*-1
EndIf

If y-radius < 0
y = 0+radius
ball(lp).speedy=ball(lp).speedy*-1
EndIf

NumberOfBlocksLeft=0
BA =0
For blocky = 100 To 280 Step 20
For blockx = 1 To 751 Step 50

; check if block exists
If bl(ba)=0
; draw block
If blocky>99 And blocky<140 Then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,0,0)
If blocky>139 And blocky<180 Then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,100,0)
If blocky>179 And blocky<220 Then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(100,180,0)
If blocky>219 And blocky<260 Then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(0,180,100)
If blocky>259 And blocky<300 Then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(0,100,160)

; check if the ball might be hitting this block
If PointInBox (x,y,blockx,blocky,blockx+48,blocky+18)
bl(ba)=1
ball(lp).speedy=ball(lp).speedy * -1
PlayerScore=PlayerScore+50
EndIf

; bump our counter that represents the number of blocks that are still alive
Inc NumberOfBlocksLeft
EndIf

Inc BA
Next
Next




If PlayerGotShrunk=1
If Playernormalstate=0
PlayerState=Shrunk
EndIf
EndIf

If PlayerState=Shrunk
PlayerNormalState=Timer()+10000 ;Add 10 second timer
PlayerSize=width/2 ;Shrink Player Half it's Width
EndIf

If Timer()>PlayerNormalState ;Did timer pass 10 seconds? If did then...
PlayerNormalState = 0 ;Player is Back to Normal
PlayerGotShrunk=0
PlayerSize=Width ;Player will Come to back to it's origial width
PlayerState=Normal
EndIf

If Playerx < 0 Then Playerx = 0 + PLayerSize
If Playerx+PlayerSize > sw Then Playerx = sw - PlayerSize

If PointInBox (x,y,playerx,playery,playerx+PlayerSize,Playery+Height)
ball(lp).speedy=ball(lp).speedy*-1
EndIf

BoxC playerx,playery,playerx+PlayerSize,playery+Height,1,RGB(180,0,0)
CircleC x,y,radius,1,colour

ball(lp).x=x
ball(lp).y=y

EndIf;End of "Ball(obj)"....

If ball(lp).y > sh
ball(lp)=null
EndIf

Next lp

//BALL LOOP ENDS HERE----------------------------------------------------------------
Login required to view complete source code



markel422

Hi Big C. and Thanks ;)

I think I understand what this code you gave me does. You First created a variable which is (ItemSpawn) to represent the Rnd Range System, so it can be used for a Select/Case Function that is coming up.

Select ItemSpawn
       
        Case 5
        ItemGreen = 5
        If Item(1).Appear = 0
        ItemNr = 1
        Gosub Init_Item_Spawn
        EndIF
        ContCase
      Case 10
      if PlayerGotBall=0
      ItemBlue = 10
      If Item(2).Appear = 0
        ItemNr = 2
        Gosub Init_Item_Spawn
        EndIF
        endif
       
        ;here we use Item(0) to spawn powerup 3 ;)
        Case 15
      ItemRed = 15
      If Item(0).Appear = 0
        ItemNr = 0
        Gosub Init_Item_Spawn
        EndIF
        ContCase
        EndSelect


If ItemSpawn "case 5" which is (If Rnd(100)=5) then use my type variable Appear to if it only equals 0 then get ready to display the items. Setting the ItemNr=5 is to give the next code which is the "gosub Init_Item_Spawn" the number code to which color to display if the number is the same as the next Case Function for its case is "Green" and the same goes for the next two cases. Now looking at the Select/Case Init_Item_Spawn itself:

Init_Item_Spawn:
;For I=0 To ItemMax
    If Item(ItemNr).Appear = 0
    Item(ItemNr).Appear = 1
    Select ItemNr
    Case 1
    Item(ItemNr).RGB_Item_Color = RGB(20,200,0) ; Green
    Case 2
    Item(ItemNr).RGB_Item_Color = RGB(0,0,200) ; Blue
    ;color for Item 3
    Case 0
    Item(ItemNr).RGB_Item_Color = RGB(255,0,0) ; red
    EndSelect
   
    Item(ItemNr).PosX=RndRange(20,780)
    Item(ItemNr).PosY=300
    EndIf
;Next I
Return


It just gives the color for each of the items along with setting up the positioning spawns for each of them. Next, after when the "ItemSpawn Select/Case" is done, I will find another gosub "Spawn_Item":

Spawn_Item:
;For obj= 1 To ItemMax
For ItemLP = 0 to 2
If Item(ItemLP).Appear = 1

    BoxC Item(ItemLP).PosX,Item(ItemLP).PosY,Item(ItemLP).PosX+IWidth,Item(ItemLP).PosY+IHeight,1,Item(ItemLP).RGB_Item_Color
    Item(ItemLP).PosY=Item(ItemLP).PosY+3
    ;Inc ItemCount
   
    ;If ItemCount > 0 Then ItemCount=ItemMax
   
    If PointInBox(item(ItemLP).PosX,item(ItemLP).PosY,playerx,playery,playerx+playersize,playery+height)
    Item(ItemLP)=0
   
    Select ItemLP
    Case 1
    ;Init PowerUp "Shrunk"
    PlayerGotShrunk=1
    ItemGreen = 0 ; testcode
    Case 2
    ;Init PowerUp "Multiball"
    PlayerGotBall=1
    ItemBlue = 0 ; testcode
    Case 0
    ;Init PowerUp "??????"
    ItemRed = 0; testcode
    Endselect
    ExitFor
    ;Dec ItemCount
    ;Green=0
      EndIf
     
      If Item(ItemLP).PosY > sh
    ;Dec ItemCount
    Item(ItemLP)=0
    If ItemLP = 1
    ItemGreen =0
    ElseIf ItemLP = 2
    ItemBlue = 0
    Else
    ItemRed = 0
    EndIf
    ExitFor
      EndIf
     
      ;If Item(ItemLP).Appear=0
      ; Item(ItemLP)=null
      ; Exit
      ;EndIf
      EndIf
     
      Print "Item_" +Str$(ItemLP)+"_Y: "+Str$(Item(ItemLP).PosY)
  ;Exit
  Next ItemLP
   ;Next obj
Return


What this is doing is the same Routine Functions I had before, except it is all done in one Routine. After creating a For/Loop variable "ItemLP" to represent 0 to 2 (Which means that it can show all three items)and creating a Item(ItemLP)=0 to represent if it touches the player or the screen, it will delete. You then created a Select/Case for ItemLP that if any of these numbers are given to the gosub, it will finally show the items going down the screen until each either hits the player or the screen and also setting up the Powers for the Items. :)

I've worked on the code you gave me alittle and tried to put in the "MultiBall" function, but am apparently having trouble preventing the blue item to respawn after when a Power is happening but it doesn't matter.;)

QuoteQuestion: I don't understand the using of the variable ItemMax because in the moment you have two Items to init and spawn.

The ItemMax variable isn't controlling how many "Item Types" it is to have, it is to controlling how many items can appear on screen for each of my items like with one of my codes found in my GreenItem and BlueItem Routine:

Note:"This was originally for it to be used back when I had the "GetFreeCell" and "GetArrayElements" Functions".

If ItemCount > 0 then ItemCount=ItemMax

If I don't put that code in there it will either not show anything on screen or (in some other case), Spawn Items LIKE CRAZY! If I wanted the ItemMax to represent the Item Types itself I could of put the code I think like this or something.

Dim Item(ItemMax) as tBall

Me setting up my For/Loop like this:

for obj= 1 to ItemMax

I control how many items can show at a time when they are spawning, which in this case is only 1. But now I don't need that variable anymore :), since I now again hard-coded my items to a specific number to only spawn once.

Quote2. This modified code isn't optimized because I think you are in a learning phase...

Huh???

QuoteWhat are you making if you want to implement a Background image?

I don't understand that Question... :'(







UPDATE ON GAME!

Ok, now I am currently having a problem with the Level Functioning which is "GameState=Win" for some reason when I put:

If Spacekey()=true then GameState=Win

It Works but if I put this:

If NumberOfBlocksLeft=0 then GameState=Win

It also works but for some reason it counts the Level Twice, pausing the game for two 4 seconds of the message "HOORAY YOU WIN!!". I thought it was because PB somehow thinks that after when that if statement is executed, it does it again because of how "NumberOfBlocksLeft=0" and then redraws it and executes it again. I tried to fix this by doing this to get away from the Blocks variable:

If NumberOfBlocksLeft=0
            State=1
         Endif
         If State=1
            GameState=Win
            State=0
         Endif


But to no success, I even tried putting the code on top, bottom, and even in the inside of the ball loop "for lp".

Heres the Original Game Code:

openscreen 800,600,32,1
setfps 60
loadfont "Arial",1,24,0
IWidth=8
IHeight=8
Width=90
Height=15
PlayerSize=Width
playerx=355
playery=550
Ballsize=8
PlayerNormalState=0
PlayerScore=0
PlayerLives=3
Level=1
sw=getscreenwidth()
sh=getscreenheight()
Green=0
Blue=0

//GAMESTATES
Constant StartGame = 1
Constant Playing   = 2
Constant Win = 3
Constant Gameover  = 4
Constant LevelUp = 5
Constant LostLife = 6

//PLAYERSTATES
Constant Normal=7
Constant Shrunk=8
Constant MultiBall=9

type tBall
x,y
speedx
speedy
size
colour
endtype
Dim Ball(1) as tBall
Dim bl(160)

type tItem
Appear
PosX
PosY
endtype
Dim Item(2) as tItem
ItemCount=0
ItemMax=1
Appear=0
Won=0
Dim Power$(4)
Power$(1)="Normal"
Power$(2)="Shrunk"
Power$(3)="MultiBall"
Power$(4)="Shrunk & MultiBall"

gosub Init_Ball

Menu()
GameState=Playing

Do
Cls 0

   PlayerState=Normal
   playerx=MouseX()
   
      NumberOfBalls=0
   for lp=1 to GetArrayElements(Ball(),1)
   
    ;To see if the Balls exist
    if ball(lp)
   
   
    inc NumberOfBalls
    x = ball(lp).x+ball(lp).speedx
    y = ball(lp).y+ball(lp).speedy
    radius=ball(lp).size
    colour=ball(lp).colour
   
    if x-radius < 0
       x = 0+radius
       ball(lp).speedx=ball(lp).speedx*-1
       endif
   
    if x+radius > sw
    x = sw-radius
    ball(lp).speedx=ball(lp).speedx*-1
       endif
   
    if y-radius < 0
    y = 0+radius
    ball(lp).speedy=ball(lp).speedy*-1
    endif
               
   NumberOfBlocksLeft=0
BA =0
for blocky = 100 to 280 step 20
for blockx = 1 to 751 step 50

; check if block exists
    if bl(ba)=0
; draw block
        if blocky>99 and blocky<140 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,0,0)
        if blocky>139 and blocky<180 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,100,0)
        if blocky>179 and blocky<220 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(100,180,0)
        if blocky>219 and blocky<260 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(0,180,100)
        if blocky>259 and blocky<300 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(0,100,160)

; check if the ball might be hitting this block
          if PointInBox (x,y,blockx,blocky,blockx+48,blocky+18)
          bl(ba)=1
          ball(lp).speedy=ball(lp).speedy * -1
          PlayerScore=PlayerScore+50
               endif
             
; bump our counter that represents the number of blocks that are still alive
inc NumberOfBlocksLeft
    endif

      Inc BA
      next
   next
       
       
       
        If PlayerGotShrunk=1
      If Playernormalstate=0
          PlayerState=Shrunk
           Endif
        Endif
       
        If PlayerState=Shrunk
           PlayerNormalState=timer()+10000 ;Add 10 second timer
     PlayerSize=width/2 ;Shrink Player Half it's Width
     DisplayMode=1
        Endif
     
      If Timer()>PlayerNormalState ;Did timer pass 10 seconds? If did then...
        PlayerNormalState = 0 ;Player is Back to Normal
        PlayerGotShrunk=0
        PlayerSize=Width ;Player will Come to back to it's origial width
        PlayerState=Normal
        DisplayMode=0
        Endif 
           
            if Playerx < 0 then Playerx = 0 + PLayerSize
            if Playerx+PlayerSize > sw then Playerx = sw - PlayerSize
   
            if PointinBox (x,y,playerx,playery,playerx+PlayerSize,Playery+Height)
            ball(lp).speedy=ball(lp).speedy*-1
            endif
   
            boxc playerx,playery,playerx+PlayerSize,playery+Height,1,RGB(180,0,0)
    circlec x,y,radius,1,colour
   
    ball(lp).x=x
    ball(lp).y=y
   
     
       Endif;End of "Ball(obj)"....
   if GameState=Win then ball(lp)=NULL
   if ball(lp).y > sh
    ball(lp)=NULL
   endif
 
      next lp  
     
//BALL LOOP ENDS HERE----------------------------------------------------------------
       
         ; check if Green=0 and RND(50)=5  ..  So both conditions need to be met, for PB to run the code inside this IF/ENDIF 
         if Green=0 and Rnd(50)=5 and PlayerGotShrunk=0
          gosub GreenItemSpawn
          Green=1
         endif
         if Blue=0 and Rnd(60)=6 and NumberOfBalls=1
          gosub BlueItemSpawn
          Blue=1
         endif
         
         if Green=1
          gosub GreenItem
         endif
         if Blue=1
          gosub BlueItem
         endif
     
     if PlayerGotBall=1
    PlayerState=MultiBall
     gosub Add_Balls
     endif
     if NumberOfBalls>1
        DisplayMode=2
     endif
  if NumberOfBalls=1
     DisplayMode=0
    PlayerState=Normal
    PlayerGotBall=0
  endif   
  if NumberOfBalls=0
     GameState=LostLife
     endif
     if PlayerState=Shrunk and PlayerState=MultiBall
        DisplayMode=3
     endif
     ; check if the number of blocks left is zero. If it is, then all the blocks have been be killed
     ; so we can
 
       
     If GameState=LostLife

  text 310,getscreenheight()/2,"Player Has Missed Ball!"
  sync
  for lp=1 to 3
  wait 1000
  next
        DisplayMode=0
        gosub Init_Ball
       
        PlayerSize=Width
        ; take a life away from player
  Dec PlayerLives
       
; if they've no more lives, then this is game over
if PlayerLives=0
GameState = GameOver
else
; Else, set the game back to PLAYING, to continue playing the game
GameState=Playing
endif
   Endif
 
   ; check if the game state is set to WIN.  If so, the player must have cleared the level
If GameState=Win
text 310,280,"HOORAY YOU WIN!"
sync
for lp=1 to 4
wait 1000
      next lp
      DisplayMode=0
GameState=LevelUp

Endif

   If GameState=LevelUp
   Inc Level
; Init all the game character variables and array to start a new level..
; a Sub routine would be
      PlayerState=Normal
      PlayerSize=Width
         gosub Init_Ball

; re-dim the Blocks (alive?) array. This will destory the old one and create a new clear one in it's place
; this will reset the blocks
Dim bl(160)

; set the game state playing.. 
GameState=Playing
   Endif
   if NumberOfBlocksLeft=150
    State=1
   endif
   if State=1
      GameState=Win
      State=0
   endif
; The Game has ended, so lets end the program
   if GameState = GameOver
    cls 0
    End
   Endif
   
//Display Messages-------------------------------------------------------------
text 345,10,"PlayerScore: "+Str$(PlayerScore)
text 350,40,"PlayerLives: "+Str$(PlayerLives)
text 650,25,"Level: "+str$(Level)
If DisplayMode=0
      text 20,20,"Power: "+(Power$(1))
      Endif

   If DisplayMode=1
    ink RGB(0,255,0)
      text 20,20,"Power: "+(Power$(2))
    ink RGB(255,255,255)
      Endif
 
   If DisplayMode=2
      text 20,20,"Power: "+(Power$(3))
      Endif
   If DisplayMode=3
    text 20,20,"Power: "+(Power$(4))
   Endif
Sync
Loop

//FUNCTIONS--------------------------------------------------------------------
Function Menu()
repeat
cls 0
centertext 395,275,"Let's Play BreakOut!"
centertext 395,305,"Press the Left Button To Play!"
sync
until LeftMouseButton()=true
EndFunction

//Ball Variable Arrays---------------------------------------------------------
Init_Ball:
   Index=GetFreeCell(ball())
   ball(Index).x=400
   ball(Index).y=300
   ball(Index).speedx=rndrange(3,4)
   ball(Index).speedy=rndrange(4,5)
   ball(Index).size=ballsize
   ball(Index).colour=RGB(0,0,200)
Return

Add_Balls:
   Index=GetFreeCell(ball())
   ball(Index).x=400
   ball(Index).y=300
   ball(Index).speedx=rndrange(3,4)
   ball(Index).speedy=rndrange(4,5)
   ball(Index).size=ballsize
   ball(Index).colour=RGB(0,0,200)
   
   Index=GetFreeCell(ball())
   ball(Index).x=350
   ball(Index).y=300
   ball(Index).speedx=rndrange(3,4)
   ball(Index).speedy=rndrange(4,5)
   ball(Index).size=ballsize
   ball(Index).colour=RGB(0,0,200)
Return
//End of Ball Variable Arrays-----------------------------------------------------
GreenItemSpawn:
For I=0 to ItemMax
   Item(1).PosX=rndrange(20,780)
   Item(1).PosY=300
Next I
Return
BlueItemSpawn:
For I=0 to ItemMax
   Item(2).PosX=rndrange(20,780)
   Item(2).PosY=300
Next I
Return
GreenItem:
if Item(1)

   Boxc Item(1).PosX,Item(1).PosY,Item(1).PosX+IWidth,Item(1).PosY+IHeight,1,RGB(20,200,0)
   Item(1).PosY=Item(1).PosY+3
   Inc ItemCount
   
   
   
   If PointinBox(item(1).PosX,item(1).PosY,playerx,playery,playerx+playersize,playery+height)
    Item(1)=NULL
    PlayerGotShrunk=1
    Dec ItemCount
    Green=0
      endif
      If Item(1).PosY > sh
      item(1)=NULL
    Dec ItemCount
    Green=0
      endif
      if Green=0
      Item(1)=NULL
     
      endif
      Endif
     
      print "GreenItemY: "+Str$(Item(1).PosY)
   
Return

BlueItem:
for obj= 1 to ItemMax
if Item(2)

   Boxc Item(2).PosX,Item(2).PosY,Item(2).PosX+IWidth,Item(2).PosY+IHeight,1,RGB(0,0,200)
   Item(2).PosY=Item(2).PosY+3
   Inc ItemCount
   
   If ItemCount > 0 then ItemCount=ItemMax
   
   If PointinBox(item(2).PosX,item(2).PosY,playerx,playery,playerx+playersize,playery+height)
    Item(2)=NULL
    PlayerGotBall=1
    Dec ItemCount
    Blue=0
      endif
      If Item(2).PosY > sh
    item(2)=NULL
    Dec ItemCount
    Blue=0
      endif
      if Blue = 0
      Item(2)=NULL
      Exit
      endif
      Endif
     
      print "BlueItemY: "+Str$(Item(2).PosY)
   Exit
   next obj
Return


I also seem to notice that when it comes to me entering code for like if GameState=LostLife and put a variable to play if it does happen like "If NumberOfBalls=0 then GameState=LostLife" and I put this code on top of that GameState function, it doesn't work, how ever if I put it understand the function it will work. There are also functions that needs to go in number order from highest to lowest for some reason like:

if PlayerGotBall=1
    PlayerState=MultiBall
     gosub Add_Balls
     endif
     if NumberOfBalls>1
        DisplayMode=2
     endif
  if NumberOfBalls=1
     DisplayMode=0
    PlayerState=Normal
    PlayerGotBall=0
  endif   
  if NumberOfBalls=0
     GameState=LostLife
     endif


Because if I try to switch any of them around from the order in which they are, they don't seem to work, any idea why?

kevin

#39
 Can you explain why this is set out in two comparisons ?  

PlayBASIC Code: [Select]
   if NumberOfBlocksLeft=150
State=1
endif
if State=1
GameState=Win
State=0
endif




 The state variable and secondary compare seem redundant..  So we can represent the same action using.

PlayBASIC Code: [Select]
   if NumberOfBlocksLeft=150
GameState=Win
endif



 So when the blocks hit the certain limit, we flip the GameState variable to whatever condition we want.  

 If the player wins a level, then depending upon what you want, you'd normally reset all the game variables/arrays to their starting values and go again.    So what's probably happening is that something isn't be reset correctly between levels.    


PlayBASIC Code: [Select]
openscreen 800,600,32,1
setfps 60
loadfont "Arial",1,24,0
IWidth=8
IHeight=8
Width=90
Height=15
PlayerSize=Width
playerx=355
playery=550
Ballsize=8
PlayerNormalState=0
PlayerScore=0
PlayerLives=3
Level=1
sw=getscreenwidth()
sh=getscreenheight()
Green=0
Blue=0

//GAMESTATES
Constant StartGame = 1
Constant Playing = 2
Constant Win = 3
Constant Gameover = 4
Constant LevelUp = 5
Constant LostLife = 6

//PLAYERSTATES
Constant Normal=7
Constant Shrunk=8
Constant MultiBall=9

type tBall
x,y
speedx
speedy
size
colour
endtype
Dim Ball(1) as tBall
Dim bl(160)

type tItem
Appear
PosX
PosY
endtype
Dim Item(2) as tItem
ItemCount=0
ItemMax=1
Appear=0
Won=0
Dim Power$(4)
Power$(1)="Normal"
Power$(2)="Shrunk"
Power$(3)="MultiBall"
Power$(4)="Shrunk & MultiBall"

gosub Init_Ball

Menu()
GameState=Playing

Do
Cls 0

PlayerState=Normal
playerx=MouseX()

NumberOfBalls=0
for lp=1 to GetArrayElements(Ball(),1)

;To see if the Balls exist
if ball(lp)


inc NumberOfBalls
x = ball(lp).x+ball(lp).speedx
y = ball(lp).y+ball(lp).speedy
radius=ball(lp).size
colour=ball(lp).colour

if x-radius < 0
x = 0+radius
ball(lp).speedx=ball(lp).speedx*-1
endif

if x+radius > sw
x = sw-radius
ball(lp).speedx=ball(lp).speedx*-1
endif

if y-radius < 0
y = 0+radius
ball(lp).speedy=ball(lp).speedy*-1
endif

NumberOfBlocksLeft=0
BA =0
for blocky = 100 to 280 step 20
for blockx = 1 to 751 step 50

; check if block exists
if bl(ba)=0
; draw block
if blocky>99 and blocky<140 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,0,0)
if blocky>139 and blocky<180 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,100,0)
if blocky>179 and blocky<220 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(100,180,0)
if blocky>219 and blocky<260 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(0,180,100)
if blocky>259 and blocky<300 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(0,100,160)

; check if the ball might be hitting this block
if PointInBox (x,y,blockx,blocky,blockx+48,blocky+18)
bl(ba)=1
ball(lp).speedy=ball(lp).speedy * -1
PlayerScore=PlayerScore+50
endif

; bump our counter that represents the number of blocks that are still alive
inc NumberOfBlocksLeft
endif

Inc BA
next
next



If PlayerGotShrunk=1
If Playernormalstate=0
PlayerState=Shrunk
Endif
Endif

If PlayerState=Shrunk
PlayerNormalState=timer()+10000 ;Add 10 second timer
PlayerSize=width/2 ;Shrink Player Half it's Width
DisplayMode=1
Endif

If Timer()>PlayerNormalState ;Did timer pass 10 seconds? If did then...
PlayerNormalState = 0 ;Player is Back to Normal
PlayerGotShrunk=0
PlayerSize=Width ;Player will Come to back to it's origial width
PlayerState=Normal
DisplayMode=0
Endif

Login required to view complete source code



    In this version, the Level is reset (mostly, the Blocks and Balls are at least) so the next level starts a fresh.     Normally, this type of initialization done in a subroutine/function.  

    So you could wrap up a sub routine that init's the level and just call it when ever you need to restart the level...  less code, less chance of errors creeping in.




markel422

UPDATE!


-Sound has been added! :D ;D

-Grab Function has been remodified!

-Speed Increase been added for Ball! ;)

-Level Changes Color after each Level Completion.

-Ability to gain Extra Life!

-Display Power Labels changed to Color.



The Good News:

This Game is becoming more closer to completion than I originally had in mind. In fact, I over did everything in this game than what was originally planned and am very pleased on how much things were added here.

As far as the Sound, I am more surprised that I was able to pull this off. All Sound was made by me using a program know as "SFXR" with this, I was able to make 8-bit sounds that made sense of what is happening on screen. Even as far as the Power-Ups and Extra Life. ;P

Displays are now given color so that to represent how good or bad it is to have more Power-Ups going on.

When you now score a certain amount of points, you are automatically given an Extra Life; also added Scoring.

Another Incredible thing in my accomplishment was that I was able to make the ball increase in speed each time it successfully touches the player paddle, giving this game extra challenge!  8)

I am very glad of my progress with this, even as far as my knowledge of programming. I feel like I have leveled up a few levels due to me getting "Programming EXP. Points". lol ;) I just now figured out that when creating variables, that they can only control one thing and one thing only and that if I wanted to make another thing within that variable then I need another variable to represent what I want to happen within that variable.

The Bad News:

There are still a few things that I wish to add in this game but kept failing, which was to add an already active ball inside of the blocks (as if it is trapped) and can only break the other blocks if it set free breaking the specific block areas in the screen.

There are also a few Bugs in this game that has been haunting me since I started making this and they are:

-The Mouse Glitch: If you make your mouse travel to far to the right of the screen, for some reason the ball will no longer bounce off of your paddle.

-Player & Ball Collsion Glitch: When the ball hits either the left side or the right side of the paddle, it will cause the computer the calculate that it is still hitting the paddle up & down inside of the paddle really fast.

Code is In File Below...
|
|
|
|
\/