Main Menu

My BreakOut Game

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

Previous topic - Next topic

micky4fun

Hi markel422

;Create Blocks
for blocky = 100 to 280 step 20
  for blockx = 1 to 751 step 50
     if bl(ba)=0
           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)
     endif
           if PointInBox (ballx,bally,blockx,blocky,blockx+48,blocky+18) and bl(ba)=0
            bl(ba)=1
            ballspeedy=ballspeedy * -1
            PlayerScore=PlayerScore+50
               endif
       Inc BA
      next
   next


one method of changing colour of blokes is in above code , which is at line 100 of your program , there's proberly a better way , but this give you an idea

as for the sounds , try
http://www.soundsnap.com
you get 5 free downloads a month

mick :)

markel422

#16
Here is an update of the Game so far.

I've fixed the item spawning from the previous screen and have changed the color of the Item itself. Now you will notice a green box falls down every once in a while to the screen and if the Player were to get that Green Item, it will cause there paddle to shrink. :P

But there's a Problem...after when the Item have caused the paddle to shrink, I can't seem to make it go back. I've tried If/Then's, Functions, Constants, Cases, and Gosubs but it still won't work.

Maybe I'm not using the Event Coding Correctly to Make That Event Happen?

PlayBASIC Code: [Select]
openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

IWidth=8
IHeight=8
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Playerx = 355
Playery = 550
BallSize =8

Constant StartGame = 1
Constant Playing = 2
Constant Win = 3
Constant Gameover = 4


GameTimer=5000
GreenItem=0

Global Green=RGB(0,255,0)

;Item Spawns and the like
ItemMax=1
T1=timer()
T2=timer()
Dim IAppear(ItemMax)
Dim IposX(ItemMax)
Dim IposY(ItemMax)

gosub Ball_Restart

type tmessage
x,y
Show$
EndDisplaytime
Colour
endtype
Dim message as tmessage list

Dim bl(160)

GreenItem = 0
Menu()

Do
Cls 0
GameState=StartGame
;Create Movements of Player
Playerx = MouseX()

Bally = bally + ballspeedy
;Create Collision for Ball on Player
if PointInBox(ballx,bally,playerx,playery,playerx+width,playery+height)
ballspeedy=ballspeedy * -1
endif


;Create Player Collision on Screen
If playerx < 0 then playerx = 0
If playerx + width > getscreenwidth() then playerx = getscreenwidth()-width
;Create Ball Movement
Ballx = ballx + ballspeedx


;Create Collision for Ball on Screen
if ballx + ballsize > getscreenwidth()
ballx = getscreenwidth() - ballsize
ballspeedx = ballspeedx * -1
endif

if ballx - ballsize < 0
ballx = 0 + ballsize
ballspeedx=ballspeedx * -1
endif



/*if bally + ballsize > getscreenheight()
bally = getscreenheight() - ballsize
ballspeedy=ballspeedy * -1
endif*/


if bally - ballsize < 0
bally = 0 + ballsize
ballspeedy=ballspeedy * -1
endif

;Create Blocks
for blocky = 100 to 280 step 20
for blockx = 1 to 751 step 50
if bl(ba)=0
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)
endif
if PointInBox (ballx,bally,blockx,blocky,blockx+48,blocky+18) and bl(ba)=0
bl(ba)=1
ballspeedy=ballspeedy * -1
PlayerScore=PlayerScore+50
endif
Inc BA
next
next


if PlayerY+Height>IPosY(I)-IHeight and PlayerY<IposY(I)+IHeight
if PLayerX+Width>IPosX(I)-IWidth and PlayerX<IPosX(I)+IWidth
Width=50
GreenItem = 1
if GreenItem=1
Greentimer= 5000
dec Greentimer
if Greentimer = 0
Width=90
Endif
Endif
endif
endif



if BA=160 then BA=0 ;"BA" can Represent the word "Break Away"


;Create Item
If Rnd(500)=20 then gosub Item_Appear

If ItemCount > 0 then gosub Move_Item

;Creat Player
Boxc Playerx,playery,playerx+width,playery+height,1,RGB(180,0,0)

;Create Ball
Circlec ballx,bally,ballsize,1,rgb(0,0,200)

//Display Messages
text 345,10,"PlayerScore: "+Str$(PlayerScore)
text 350,40,"PlayerLives: "+Str$(PlayerLives)

Login required to view complete source code



kevin

#17
Quote
But there's a Problem...after when the Item have caused the paddle to shrink, I can't seem to make it go back. I've tried If/Then's, Functions, Constants, Cases, and Gosubs but it still won't work.

 Well, think of it this way.  If player needs different operating states, such as difference sizes.  Then we'll need something (such as a variable) to remember what size the player is current in.   If this state wears off  after a certain time period, then we'll need something to help us monitor when the mode is meant to change back.

 
PlayBASIC Code: [Select]
   Width =90
Height =15


PlayerScore = 0
PlayerLives = 3
Playerx = 355
Playery = 550
PlayerSize = Width ; size of the player
PlayerReSizeTimer = 0 ; time when the player, will revert to

playerX=400
Playery=590

setfps 60

Do
; cleat of contents of screen
Cls 0

PlayerX=mouseX()



; check if the player is can be shrunk or not ??
if PlayerReSizeTimer=0

; use the space key to shrink the player
if SpaceKey()

; remember the current time and 5000 milliseconds (5 seconds) to it. So the mode will last 1 second
PlayerReSizeTimer=timer() + 5000
PLayerSize = Width/2

endif

else

; check if the current time is beyond the Resize time yet ?
if Timer()>PlayerReSizeTimer
PlayerReSizeTimer=0
PLayerSize = Width ; restore width
endif


print "You shrunk Me"

endif


;Draw Player
Boxc Playerx,playery,playerx+PlayerSize,playery+height,1,RGB(180,0,0)


Sync
loop










 Here's a bit of 'clean up' of the original code structure (with the ball size changing :) ).


PlayBASIC Code: [Select]
openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

IWidth=8
IHeight=8
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Playerx = 355
Playery = 550
BallSize =8


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


GameTimer=5000
GreenItem=0

Global Green=RGB(0,255,0)

;Item Spawns and the like
ItemMax=1
T1=timer()
T2=timer()
Dim IAppear(ItemMax)
Dim IposX(ItemMax)
Dim IposY(ItemMax)

gosub Ball_Restart

;type tmessage
; x,y
; Show$
; EndDisplaytime
; Colour
;endtype
;Dim message as tmessage list

Dim bl(160)

GreenItem = 0
Menu()






; Init Game Starting variables
GameState=PLaying



; Play game loop
Do
Cls 0


;Create Movements of Player
Playerx = MouseX()

Bally = bally + ballspeedy

;Create Collision for Ball on Player
if PointInBox(ballx,bally,playerx,playery,playerx+width,playery+height)
ballspeedy=ballspeedy * -1
endif


;Create Player Collision on Screen
If playerx < 0 then playerx = 0
If playerx + width > getscreenwidth() then playerx = getscreenwidth()-width
;Create Ball Movement
Ballx = ballx + ballspeedx


;Create Collision for Ball on Screen
if ballx + ballsize > getscreenwidth()
ballx = getscreenwidth() - ballsize
ballspeedx = ballspeedx * -1
endif

if ballx - ballsize < 0
ballx = 0 + ballsize
ballspeedx=ballspeedx * -1
endif



/*if bally + ballsize > getscreenheight()
bally = getscreenheight() - ballsize
ballspeedy=ballspeedy * -1
endif*/


if bally - ballsize < 0
bally = 0 + ballsize
ballspeedy=ballspeedy * -1
endif


; check if the Ball has left the bottom of the screen, if it has set the GAme State to LOSTLIFE
If Bally > getscreenheight()
GameState = LostLife
endif


; ----------------------------------------------------------------------------
;Draw Blocks
; ----------------------------------------------------------------------------

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 (ballx,bally,blockx,blocky,blockx+48,blocky+18)
bl(ba)=1
ballspeedy=ballspeedy * -1
PlayerScore=PlayerScore+50
endif

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

Inc BA
Login required to view complete source code



markel422

Currently I am trying to make an Extra Ball or Two Appear when the Player gets the Blue Dot This time. I was able to make the ball appear and move when you get that Item, but it moves and shows itself only when I'm under that Particular Spot from which I got the Item ???

I've tried Arrays on the original ball itself to make it create more than one of itself, but it wasn't coming out right and the ball would just end restarting itself over and over again so I created another Gosub as well as different ball variables to represent another ball just to make an ExtraBall Appear.

And I know I can't set a variable on the Drawing of the Circle-Colour itself or I will get an Error, I can't seem to figure out another way to approach this.

openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

//Item Sizes
IWidth=8
IHeight=8

//Player Sizes
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Playerx = 355
Playery = 550
PlayerSize=Width  ;Name the same variable so that PB will know the Original Size
PlayerNormalState=0  ;Create Variable that will soon represent the item timer

///Normal BallSize
BallSize =8
;MultiBall Sizes--------------------
MultiBall=Ballsize
Dim MultiBall(2)

//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

Global Green=RGB(0,255,0)

;Item Spawns and the like
T1=timer()
T2=timer()

;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 Ball
BallMax=2
Dim BAppear(BallMax)
Dim Ballx(BallMax)
Dim bally(BallMax)


gosub Ball_Restart
gosub Ball_Restartv2

;type tmessage
; x,y
; Show$
; EndDisplaytime
;   Colour
;endtype
;Dim message as tmessage list

Dim bl(160)

Menu()






; Init Game Starting variables
  GameState=PLaying
 
 

; Play game loop
Do
Cls 0
    PlayerState=Normal

 ;Create Movements of Player
 Playerx = MouseX()
 
 Bally = bally + ballspeedy

;Create Collision for Ball on Player
if PointInBox(ballx,bally,playerx,playery,playerx+PlayerSize,playery+height)
ballspeedy=ballspeedy * -1
 endif
 

  ;Create Player Collision on Screen
  If playerx < 0 then playerx = 0
  If playerx + PlayerSize > getscreenwidth() then playerx = getscreenwidth()-PlayerSize
;Create Ball Movement
   Ballx = ballx + ballspeedx

 
;Create Collision for Ball on Screen
if ballx + ballsize > getscreenwidth()
 ballx = getscreenwidth() - ballsize
 ballspeedx = ballspeedx * -1
endif

if ballx - ballsize < 0
ballx = 0 + ballsize
ballspeedx=ballspeedx * -1    
  endif
   
 
   
/*if bally + ballsize > getscreenheight()
bally = getscreenheight() - ballsize
ballspeedy=ballspeedy * -1
endif*/

if bally - ballsize < 0
bally = 0 + ballsize
ballspeedy=ballspeedy * -1
  endif


; check if the Ball has left the bottom of the screen, if it has set the GAme State to LOSTLIFE
  If Bally > getscreenheight()
GameState = LostLife
  endif


; ----------------------------------------------------------------------------
  ;Draw Blocks
; ----------------------------------------------------------------------------

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 (ballx,bally,blockx,blocky,blockx+48,blocky+18)
          bl(ba)=1
          ballspeedy=ballspeedy * -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
 

; check if the number of blocks left is zero. If it is, then all the blocks have been be killed
; so we can
 if NumberOfBlocksLeft=0
GameState=Win
endif  

; ----------------------------------------------------------------------------
;  Draw Game Characters to The Screen
; ----------------------------------------------------------------------------
 
 ;Draw Player
  Boxc Playerx,playery,playerx+playersize,playery+height,1,RGB(180,0,0)

;Draw Ball
Circlec ballx,bally,ballsize,1,rgb(0,0,200)  

//Display Messages
text 345,10,"PlayerScore: "+Str$(PlayerScore)
text 350,40,"PlayerLives: "+Str$(PlayerLives)
  ; ----------------------------------------------------------------------------
;  DRaw/HAndle ITEMS ??
; ----------------------------------------------------------------------------
  ;If the GREEN ITEM TOUCHED PLAYER
 If PlayerNormalState = 0 ;Is Player Normal Back to Normal?
    if PlayerY+Height>GIPosY(I)-IHeight and PlayerY<GIposY(I)+IHeight //Check if Player has Touched Green Item.
      if PLayerX+PlayerSize>GIPosX(I)-IWidth and PlayerX<GIPosX(I)+IWidth
       PlayerState=Shrunk  
          Endif
       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
       PlayerSize=Width ;Player will Come to back to it's origial width
       PlayerState=Normal
       Endif
       
    ;If the BLUE ITEM TOUCHED PLAYER  
    If PlayerNormalState = 0
       if PlayerY+Height>BIPosY(I)-IHeight and PlayerY<BIposY(I)+IHeight
      if PLayerX+PlayerSize>BIPosX(I)-IWidth and PlayerX<BIPosX(I)+IWidth
             PlayerState=MultiBall
        Endif
     Endif
    Endif
   
          If PlayerState=MultiBall
           multiballx=multiballx+multiballspeedx
              multibally=multibally+multiballspeedy
                  ;Draw Extra Ball
            Circlec multiballx,multibally,MultiBall,1,rgb(0,0,200)  
          Endif
       

 
 ;Create Items
 If Rnd(500)=20 then gosub Item_GreenAppear
 If Rnd(500)=20 then gosub Item_BlueAppear
 
 If ItemCount > 0 then gosub Move_GreenItem
    If ItemCount > 0 then gosub Move_BlueItem
//=============================================================================
//Check the Game STATE
//=============================================================================
 
; ----------------------------------------------------
; React to the current GameState variables
; ----------------------------------------------------

if GameState=LostLife

 text 310,getscreenheight()/2,"Player Has Missed Ball!"
 sync
 for lp=1 to 3
 wait 1000
 next

 gosub Ball_Restart


; 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 ste 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

;
GameState=LevelUp

endif


if GameState=LevelUp
; Init all the game character variables and array to start a new level..
; a Sub routine would be

gosub Ball_Restart

; 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


; The Game has ended, so lets end the program
  if GameState = GameOver
  cls 0
  End
  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

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


MultiBall_Start:

Return

Move_GreenItem:
For I = 1 to ItemMax
if GIAppear(I)=1
GIPosY(I)=GIposY(I)+3
 Boxc GIposX(I),GIposY(I),GIposX(I)+IWidth,GIPosY(I)+IHeight,1,RGB(20,200,0)
 if PlayerY+Height>GIPosY(I)-IHeight and PlayerY<GIposY(I)+IHeight
   if PLayerX+PlayerSize>GIPosX(I)-IWidth and PlayerX<GIPosX(I)+IWidth or GIPosY(I)>getscreenheight()+Iheight
    GIAppear(I)=0
    Endif
    Endif
Endif
Next I
Return

Item_GreenAppear:

For I = 1 to ItemMax
If GIAppear(I)=0
GIAppear(I)=1
GIposX(I)=Rndrange(20,780)
 GIPosY(I)=300
 Inc ItemCount
 Exit
Endif
Next I
Return

Move_BlueItem:
For I = 1 to ItemMax
if BIAppear(I)=1
BIPosY(I)=BIposY(I)+3
 Boxc BIposX(I),BIposY(I),BIposX(I)+IWidth,BIPosY(I)+IHeight,1,RGB(0,0,200)
 if PlayerY+Height>BIPosY(I)-IHeight and PlayerY<BIposY(I)+IHeight
   if PLayerX+PlayerSize>BIPosX(I)-IWidth and PlayerX<BIPosX(I)+IWidth or BIposY(I)>getscreenheight()+Iheight
    BIAppear(I)=0
       Endif
    Endif
Endif
Next I
Return

Item_BlueAppear:
For I = 1 to ItemMax
If BIAppear(I)=0
BIAppear(I)=1
BIposX(I)=Rndrange(20,780)
 BIPosY(I)=300
 Inc ItemCount
 Exit
Endif
Next I
Return

Ball_Restart:

ballspeedx = 2
ballspeedy = 3
ballx = 400
bally = 300
 
return

Ball_Restartv2:

multiballspeedx = 2
multiballspeedy = 3
multiballx = 400
multibally = 300
 
return

[code/]

kevin

#19
  If your going to control more than one moving object, then variables are not the way approach this.   Variable are good for storing individual bits of information about a game character, but soon become a nightmare when  we wish to expand beyond one character to handle many.    For this we need arrays

 An array is simple a collection of variables that we can access through an index.    Bellow is an example that uses a TYPED ARRAY to store and display two game characters (as circles).



 Example #1

  Refer to the HELP under ABOUT -> TYPES and ABOUT -> ARRAYS..

PlayBASIC Code: [Select]
   ; Declare a type that will describe all the of required properties that
; each of our game character will have

Type tObject
x#,y# ; it's position on screen
size ; size of the object
colour ; colour of this object
EndType

; create an array with space for 10 objects to stored within
Dim Ball(10) as tObject


; When we dim an array it's contents are empty, so next well manually create 2 objects

; --------------------
; Create Object #1
; --------------------

; This ball with be position in the cventer of the default screen, has 40 pixel radius
; an be RED

Ball(1) = New Tobject
Ball(1).x# = 400
Ball(1).y# = 300
Ball(1).size = 40
Ball(1).colour =rgb(255,0,0) ; set colour to red





; --------------------
; Create Object #2
; --------------------

; This ball with be positioned in the top left part of the screen,
; it'll be 20 pixel radius and be coloured green.

Ball(2) = New Tobject
Ball(2).x# = 100
Ball(2).y# = 100
Ball(2).size = 20
Ball(2).colour =rgb(0,255,0) ; set colour to green




; ------------------------------------------------------------
; Draw all of the objects in our array as circles on the screen
; ------------------------------------------------------------

for Obj=1 to 10
; check if this ball object exists ?? - We only created 2 objects, and our array
; has space for 10. So we only want to di8splay the one's that exist.
if Ball(Obj)

; get this balls X# & Y# position
x#=ball(obj).x
y#=ball(obj).y
; get its size, which we'll use the circles radius
Radius=ball(obj).size

; get it's colour
colour=ball(obj).colour

; draw a circle to represent this character on screen
circlec x#,y#,radius,true,colour

endif
next

Print "Done"
Sync
waitkey







 Example #2 - Basic Character Management

   This version builds upon the version above,  the main difference is that this version adds some speed fields to the character properties.  These properties are used as the speed this particular  character  is moving in.  

   So unlike the first example, this time when we draw the characters,  we're also adding the speed parameters to the position.  This will move the character in whatever direction/speed was set.   After we add the old position with the speed, we draw the character at it's new position then store it's new position back in the array.   So each time this process is executed, the characters will appear to move in whatever direction  they're set to.



PlayBASIC Code: [Select]
   ; Declare a type that will describe all the of required properties that
; each of our game character will have

Type tObject
x#,y# ; it's position on screen
SpeedX# ; The Speed (along the X axis) the character is moving
SpeedY# ; The Speed (along the y axis) the character is moving
size ; size of the object
colour ; colour of this object
EndType

; create an array with space for 10 objects to stored within
Dim Ball(10) as tObject


; When we dim an array it's contents are empty, so next well manually create 2 objects

; --------------------
; Create Object #1
; --------------------

; This ball with be position in the center of the default screen, has 40 pixel radius
; an be RED

Ball(1) = New Tobject
Ball(1).x# = 400
Ball(1).y# = 300
Ball(1).speedx# = 1
Ball(1).speedy# = 0

Ball(1).size = 40
Ball(1).colour =rgb(255,0,0) ; set colour to red





; --------------------
; Create Object #2
; --------------------

; This ball with be positioned in the top left part of the screen,
; it'll be 20 pixel radius and be coloured green.

Ball(2) = New Tobject
Ball(2).x# = 100
Ball(2).y# = 100
Ball(2).speedx# = 0
Ball(2).speedy# = 1
Ball(2).size = 20
Ball(2).colour =rgb(0,255,0) ; set colour to green





; Tell PB to limit the refresh rate (The number of syncs per second) to 30 frames per second
Setfps 30



Do

Cls 0

; ------------------------------------------------------------
; Draw all of the objects in our array as circles on the screen
; ------------------------------------------------------------

for Obj=1 to 10
; check if this ball object exists ?? - We only created 2 objects, and our array
; has space for 10. So we only want to di8splay the one's that exist.
if Ball(Obj)

; get this balls X# & Y# position & and his objects Speed values to it
x#=ball(obj).x+ball(obj).Speedx
y#=ball(obj).y+ball(obj).Speedy

; get its size, which we'll use the circles radius
Radius=ball(obj).size

; get it's colour
colour=ball(obj).colour

; draw a circle to represent this character on screen at it's current position
circlec x#,y#,radius,true,colour

; store the characters new position (the position it was drawn in)
ball(obj).x=x#
ball(obj).y=y#

endif
next

Sync
loop




 Example #3

   This version builds upon the example #2.  The main changes are we're monitoring the X# & y# coordinate of each character.  if the character leaves the screen,  then we reset it's position.  So the character appears to moving infinitely. (never dies)


PlayBASIC Code: [Select]
   ; Declare a type that will describe all of the required properties that
; each of our game character will have

Type tObject
x#,y# ; it's position on screen
SpeedX# ; The Speed (along the X axis) the character is moving
SpeedY# ; The Speed (along the y axis) the character is moving
size ; size of the object
colour ; colour of this object
EndType

; create an array with space for 10 objects to stored within
Dim Ball(10) as tObject


; When we dim an array it's contents are empty, so next well manually create 2 objects

; --------------------
; Create Object #1
; --------------------

; This ball with be position in the cventer of the default screen, has 40 pixel radius
; an be RED

Ball(1) = New Tobject
Ball(1).x# = 400
Ball(1).y# = 300
Ball(1).speedx# = -2
Ball(1).speedy# = 0

Ball(1).size = 40
Ball(1).colour =rgb(255,0,0) ; set colour to red





; --------------------
; Create Object #2
; --------------------

; This ball with be positioned in the top left part of the screen,
; it'll be 20 pixel radius and be coloured green.

Ball(2) = New Tobject
Ball(2).x# = 100
Ball(2).y# = 100
Ball(2).speedx# = 2
Ball(2).speedy# = 1
Ball(2).size = 20
Ball(2).colour =rgb(0,255,0) ; set colour to green





; Tell PB to limit the refresh rate (The number of syncs per second) to 30 frames per second
Setfps 60



Do

Cls 0


; ------------------------------------------------------------
; Draw all of the objects in our array as cicles on the screen
; ------------------------------------------------------------

for Obj=1 to 10
; check if this ball object exists ?? - We only created 2 objects, and our array
; has space for 10. So we only want to di8splay the one's that exist.
if Ball(Obj)

; get this balls X# & Y# position & and his objects Speed values to it
x#=ball(obj).x+ball(obj).Speedx
y#=ball(obj).y+ball(obj).Speedy

; check if the X# position is within the screen bounds
if x#<0 then x#=800
if x#>800 then x#=0

if y#<0 then y#=800
if y#>600 then y#=0


; get its size, which we'll use the circles radius
Radius=ball(obj).size

; get it's colour
colour=ball(obj).colour

; draw a circle to represent this character on screen at it's current position
circlec x#,y#,radius,true,colour






; store the characters new position (the position it was drawn in)
ball(obj).x=x#
ball(obj).y=y#

endif
next



Sync
loop








 Example #4  (Character Life Cycles)

   This version builds upon the example #2 & 3.  Except this version introduces the concept of character life cycles.  In this version, when a character leaves the screen,  rather than rebound it's position,  we're KILLING it.   When we kill something,  we're removing it from the Array.  

    What this sample shows us how we can create characters, manage/draw and ultimately kill them.   This is an important principal to grasp as it's found in virtually every video game ever made.


PlayBASIC Code: [Select]
   ; Declare a type that will describe all of the required properties that
; each of our game character will have

Type tObject
x#,y# ; it's position on screen
SpeedX# ; The Speed (along the X axis) the character is moving
SpeedY# ; The Speed (along the y axis) the character is moving
size ; size of the object
colour ; colour of this object
EndType

; create an array with space for 10 objects to stored within
Dim Ball(10) as tObject


; When we dim an array it's contents are empty, so next well manually create 2 objects

; --------------------
; Create Object #1
; --------------------

; This ball with be position in the center of the default screen, has 40 pixel radius
; an be RED

Ball(1) = New Tobject
Ball(1).x# = 400
Ball(1).y# = 300
Ball(1).speedx# = -2
Ball(1).speedy# = 0

Ball(1).size = 40
Ball(1).colour =rgb(255,0,0) ; set colour to red





; --------------------
; Create Object #2
; --------------------

; This ball with be positioned in the top left part of the screen,
; it'll be 20 pixel radius and be coloured green.

Ball(2) = New Tobject
Ball(2).x# = 100
Ball(2).y# = 100
Ball(2).speedx# = 2
Ball(2).speedy# = 1
Ball(2).size = 20
Ball(2).colour =rgb(0,255,0) ; set colour to green





; Tell PB to limit the refresh rate (The number of syncs per second) to 30 frames per second
Setfps 60



Do

Cls 0


; ------------------------------------------------------------
; Draw all of the objects in our array as cicles on the screen
; ------------------------------------------------------------

Number_OF_Living_Objects=0


for Obj=1 to 10

; check if this ball object exists ?? - We only created 2 objects, and our array
; has space for 10. So we only want to di8splay the one's that exist.
if Ball(Obj)

; get this balls X# & Y# position & and his objects Speed values to it
x#=ball(obj).x+ball(obj).Speedx
y#=ball(obj).y+ball(obj).Speedy

; check if the new position is inside the screens X & Y bounds, if it's not, we'll KILL this object
if x#<0 or x#>800 or y#<0 or Y#>600

// THis object has left the screen, so lets kill it (remove it from the Ball() array
Ball(Obj) = NULL

; since this object has been removed, we don't do want PB to continue and try and access
; this object anymore.

Continue ; This jumps us out of this loop and CONTINUES on with the next FOR/NEXT loop
endif


; get its size, which we'll use the circles radius
Radius=ball(obj).size

; get it's colour
colour=ball(obj).colour

; draw a circle to represent this character on screen at it's current position
circlec x#,y#,radius,true,colour



; store the characters new position (the position it was drawn in)
ball(obj).x=x#
ball(obj).y=y#

; bump the counter for how many objects are still alive
inc Number_OF_Living_Objects

endif


next


print "Number of living objects="+Str$(Number_OF_Living_Objects)


Sync
loop







 Example #5  (Character Life Cycles)


  This version extends example #4, this one adds a function to 'randomly' spawn a character.  


PlayBASIC Code: [Select]
   ; Declare a type that will describe al of the required properties that
; each of our game character will have

Type tObject
x#,y# ; it's position on screen
SpeedX# ; The Speed (along the X axis) the character is moving
SpeedY# ; The Speed (along the y axis) the character is moving
size ; size of the object
colour ; colour of this object
EndType

; create an array with space for 10 objects to stored within
Dim Ball(10) as tObject


; When we dim an array it's contents are empty, so next well manually create 2 objects

; --------------------
; Create Object #1
; --------------------

; This ball with be position in the cventer of the default screen, has 40 pixel radius
; an be RED

Ball(1) = New Tobject
Ball(1).x# = 400
Ball(1).y# = 300
Ball(1).speedx# = -2
Ball(1).speedy# = 0

Ball(1).size = 40
Ball(1).colour =rgb(255,0,0) ; set colour to red





; --------------------
; Create Object #2
; --------------------

; This ball with be positioned in the top left part of the screen,
; it'll be 20 pixel radius and be coloured green.

Ball(2) = New Tobject
Ball(2).x# = 100
Ball(2).y# = 100
Ball(2).speedx# = 2
Ball(2).speedy# = 1
Ball(2).size = 20
Ball(2).colour =rgb(0,255,0) ; set colour to green





; Tell PB to limit the refresh rate (The number of syncs per second) to 30 frames per second
Setfps 60



Do

Cls 0


; Check if the user presses the space bar

if SpaceKey()=true
; create a ball
RandomlyCreateBall()

endif



; ------------------------------------------------------------
; Draw all of the objects in our array as cicles on the screen
; ------------------------------------------------------------

Number_OF_Living_Objects=0


for Obj=1 to GetArrayElements(Ball(),1)

; check if this ball object exists ?? - We only created 2 objects, and our array
; has space for 10. So we only want to di8splay the one's that exist.
if Ball(Obj)

; get this balls X# & Y# position & and his objects Speed values to it
x#=ball(obj).x+ball(obj).Speedx
y#=ball(obj).y+ball(obj).Speedy

; check if the new position is inside the screens X & Y bounds, if it's not, we'll KILL this object
if x#<0 or x#>800 or y#<0 or Y#>600

// THis object has left the screen, so lets kill it (remove it from the Ball() array
Ball(Obj) = NULL

; since this object has been removed, we don't do want PB to continue and try and access
; this object anymore.

Continue ; This jumps us out of this loop and CONTINUES on with the next FOR/NEXT loop
endif


; get its size, which we'll use the circles radius
Radius=ball(obj).size

; get it's colour
colour=ball(obj).colour

; draw a circle to represent this character on screen at it's current position
circlec x#,y#,radius,true,colour



; store the characters new position (the position it was drawn in)
ball(obj).x=x#
ball(obj).y=y#

; bump the counter for how many objects are still alive
inc Number_OF_Living_Objects

endif


next


print "Number of living objects="+Str$(Number_OF_Living_Objects)


Sync
loop



Function RandomlyCreateBall()

; ask PB to locate a FREE (unused cell) in the ball array, if a spot can't be found it'll expand
; BALL() array for you
Index =GetFreeCell(Ball())

; init this objects
Ball(Index) = New Tobject
Ball(Index).x# = rnd(800) ; screen x position
Login required to view complete source code




 Example #6  (Character Life Cycles)


    In this version we're created a functions to simplify the creation process.    n this we've create a function callled CreateNewBall,  this function create a new ball character in the ball() array and initialized it's properties with whatever we pass this function.    So any time we need to create a ball we could call our 'custom' function rather than manually like the previous examples do.     This refers back to what I was getting at previously about how we can use functions to write our 'game engine'.    

PlayBASIC Code: [Select]
   ; Declare a type that will describe al of the required properties that
; each of our game character will have

Type tObject
x#,y# ; it's position on screen
SpeedX# ; The Speed (along the X axis) the character is moving
SpeedY# ; The Speed (along the y axis) the character is moving
size ; size of the object
colour ; colour of this object
EndType

; create an array with space for 10 objects to stored within
Dim Ball(10) as tObject


; When we dim an array it's contents are empty, so next well manually create 2 objects

; --------------------
; Create Object
; --------------------

CreateNewBall(400,300,2,180,40,rgb (255,0,0))


; --------------------
; Create Object
; --------------------

CreateNewBall(100,100,2,180+45,20,rgb(0,255,0))


; Tell PB to limit the refresh rate (The number of syncs per second) to 30 frames per second
Setfps 60



Do

Cls 0


; Check if the user presses the space bar

if SpaceKey()=true
; create a ball
RandomlyCreateBall()

endif



; ------------------------------------------------------------
; Draw all of the objects in our array as cicles on the screen
; ------------------------------------------------------------

Number_OF_Living_Objects=0


for Obj=1 to GetArrayElements(Ball(),1)

; check if this ball object exists ?? - We only created 2 objects, and our array
; has space for 10. So we only want to di8splay the one's that exist.
if Ball(Obj)

; get this balls X# & Y# position & and his objects Speed values to it
x#=ball(obj).x+ball(obj).Speedx
y#=ball(obj).y+ball(obj).Speedy

; check if the new position is inside the screens X & Y bounds, if it's not, we'll KILL this object
if x#<0 or x#>800 or y#<0 or Y#>600

// THis object has left the screen, so lets kill it (remove it from the Ball() array
Ball(Obj) = NULL

; since this object has been removed, we don't do want PB to continue and try and access
; this object anymore.

Continue ; This jumps us out of this loop and CONTINUES on with the next FOR/NEXT loop
endif


; get its size, which we'll use the circles radius
Radius=ball(obj).size

; get it's colour
colour=ball(obj).colour

; draw a circle to represent this character on screen at it's current position
circlec x#,y#,radius,true,colour



; store the characters new position (the position it was drawn in)
ball(obj).x=x#
ball(obj).y=y#

; bump the counter for how many objects are still alive
inc Number_OF_Living_Objects

endif


next


print "Number of living objects="+Str$(Number_OF_Living_Objects)


Sync
loop




Function CreateNewBall(x#,y#,speed#,angle#,radius,colour)

; ask PB to locate a FREE (unused cell) in the ball array, if a spot can't be found it'll expand
; BALL() array for you
Index =GetFreeCell(Ball())

; init this objects
Ball(Index) = New Tobject
Ball(Index).x# = x# ; screen x position
Ball(Index).y# = y# ; screen y position
Ball(Index).speedx# = cos(angle#)*Speed#
Ball(Index).speedy# = sin(angle#)*Speed#
Ball(Index).size = radius
Ball(Index).colour =colour

EndFunction Index




Function RandomlyCreateBall()
ThisBall=CreateNewBall(rnd(800),rnd(600),rndrange#(1,5),rnd(360),rndrange(10,50),rndrgb())
EndFunction ThisBall











markel422

WOW! I always knew that you could make a list of things with arrays but I had no idea it was THIS POWERFUL! :o

This is really going to help in the long run  :) and to hope that I don't need any extra stuff to finally be able to complete this but two questions...

1. What is the reason for setting this code at the end all the time?

; store the characters new position (the position it was drawn in)
ball(obj).x=x#
ball(obj).y=y#


2. Does NULL always mean to delete an object?

kevin

#21
1)

   Remove it and find out.

   If you look closely at movement calculation, you'll see  it's reading current ball objects x coordinate  + this object speed the along X axis speed then storing this in the X# variable.     The X# variable is used a temp,  and it holds the characters new position (the position it's going to be moving to)  - It's just easier (less trying), so when we can screen it's new position against the screen bounds (in this case), then later draw it we can use the variable.   However, if we want to ball to remember this movement, we have to store X# into the ball object again.

   You could do it like this also,

PlayBASIC Code: [Select]
            ; get this balls X# & Y# position  & and his objects Speed values to it
ball(obj).x=ball(obj).x+ball(obj).Speedx
ball(obj).y=ball(obj).y+ball(obj).Speedy

; check if the new position is inside the screens X & Y bounds, if it's not, we'll KILL this object
if ball(obj).x<0 or ball(obj).x>800 or ball(obj).y<0 or ball(obj).y>600

// THis object has left the screen, so lets kill it (remove it from the Ball() array
Ball(Obj) = NULL

; since this object has been removed, we don't do want PB to continue and try and access
; this object anymore.

Continue ; This jumps us out of this loop and CONTINUES on with the next FOR/NEXT loop
endif

; draw a circle to represent this character on screen at it's current position
circlec ball(obj).x,ball(obj).y,ball(obj).size,true,ball(obj).colour




  In this version we're not using temp variables,  we're constantly querying the current ball objects properties.    Generally this temps to be bit slower than original though.  As operations between variables are faster than operations between arrays()


2)

   Yep.  We're storing a NULL (none existent) object into the array.  This overwrites whatever was in this position originally. Thus  deleting it.   You can also use FreeCell command.  They do the same thing.



markel422

#22
I've Finally got the extra balls to function correctly! :)

Now when you play this code you may realize that the 2 balls don't disappear until both of them are at the bottom of the screen but hope that I can soon find out why. For some reason the items stop appearing also as soon as I get any item.

I'm also I trying to display messages of the players current powers it is using but when I try, it only display for a split second and then disppears, I not looking on using timer-based display as the other item does not function on timers even though I don't know if it should.

Here is an update:

PlayBASIC Code: [Select]
openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

//Item Sizes
IWidth=8
IHeight=8

//Player Sizes
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Playerx = 355
Playery = 550
PlayerSize=Width ;Name the same variable so that PB will know the Original Size
PlayerNormalState=0 ;Create Variable that will soon represent the item timer

///Normal BallSize
BallSize =8

//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

Global Green=RGB(0,255,0)

;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 Ball

Dim BAppear(BallMax)


PlayerGotBall=0
gosub Ball_Restart

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 The Array of 2 Balls Only
Dim Ball(3) as tBall

;Create Ball One
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(100,100,100)

;Create Ball Two
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(100,100,100)

Dim bl(160)

Dim Power$(3)
Power$(1)="Normal"
Power$(2)="Shrunk"
Power$(3)="MultiBall"


Menu()




; Init Game Starting variables
GameState=PLaying



; Play game loop
Do
Cls 0
PlayerState=Normal

;Create Movements of Player
Playerx = MouseX()

Bally = bally + ballspeedy

;Create Collision for Ball on Player
if PointInBox(ballx,bally,playerx,playery,playerx+PlayerSize,playery+height)
ballspeedy=ballspeedy * -1
endif


;Create Player Collision on Screen
If playerx < 0 then playerx = 0
If playerx + PlayerSize > getscreenwidth() then playerx = getscreenwidth()-PlayerSize
;Create Ball Movement
Ballx = ballx + ballspeedx


;Create Collision for Ball on Screen
if ballx + ballsize > getscreenwidth()
ballx = getscreenwidth() - ballsize
ballspeedx = ballspeedx * -1
endif

if ballx - ballsize < 0
ballx = 0 + ballsize
ballspeedx=ballspeedx * -1
endif



/*if bally + ballsize > getscreenheight()
bally = getscreenheight() - ballsize
ballspeedy=ballspeedy * -1
endif*/


if bally - ballsize < 0
bally = 0 + ballsize
ballspeedy=ballspeedy * -1
endif
Login required to view complete source code



markel422

#23
UPDATE!

-Added Display of Level

-Added Display of Power ;)

-New Power "Grab"


The Good News:

The Extra Balls System Functions even MORE Correctly as they can each hit blocks, can both rebound your paddle when touched by it. They can also Respawn over again when you Collect the Blue Item and can ONLY Respawn if both of the Balls are Gone.

Shrink System is working as usual but was able to add the Power Display to notify you what Power you are currently using.

A knew addition to my skill I have never done before successfully was being able to Grab your Ball System, and am REALLY glad of how it is doing it so far. When you Get the Yellow Item, the blue ball will stick to your paddle until you press the Spacekey.

The Bad News:

As far as I got now, the Game is getting closer to completion :) But I still need to know how to work the Items respawn because everytime it respawns, it repeatedly Respawns 2 times, sometimes even 3, or nothing at all, and it is really Frustrating for me trying to figure out what is really causing that! >:(

Here is the Code:

PlayBASIC Code: [Select]
openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

//Item Sizes
IWidth=8
IHeight=8

//Player Sizes
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Level = 1
Playerx = 355
Playery = 550
PlayerSize=Width ;Name the same variable so that PB will know the Original Size
PlayerNormalState=0 ;Create Variable that will soon represent the item timer

///Normal BallSize
BallSize =8

//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
Constant Grab=10

Global Green=RGB(0,255,0)


;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)

;Dimension for Ball
Dim BAppear(BallMax)
T1=timer()
PlayerGotBall=0

gosub Ball_Restart

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
Dim Ball(3) as tball

;Create Ball One
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(100,100,100)

;Create Ball Two
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(100,100,100)

Dim bl(160)

Dim Power$(4)
Power$(1)="Normal"
Power$(2)="Shrunk"
Power$(3)="MultiBall"
Power$(4)="Grab"

;Create Variable Mode displaying Power Options
ItemCount=0
DisplayMode=0
EnableItem=1
Menu()




; Init Game Starting variables
GameState=PLaying

PlayerState=Normal


; Play game loop
Do
Cls 0
;Create Movements of Player
Playerx = MouseX()

Bally = bally + ballspeedy


;Create Collision for Ball on Player
if PointInBox (ballx,bally,playerx,playery,playerx+PlayerSize,Playery+Height)
Ballspeedy=ballspeedy * -1
Endif


;Create Player Collision on Screen
If playerx < 0 then playerx = 0
If playerx + PlayerSize > getscreenwidth() then playerx = getscreenwidth()-PlayerSize
;Create Ball Movement
Ballx = ballx + ballspeedx


;Create Collision for Ball on Screen
if ballx + ballsize > getscreenwidth()
ballx = getscreenwidth() - ballsize
ballspeedx = ballspeedx * -1
endif

if ballx - ballsize < 0
ballx = 0 + ballsize
ballspeedx=ballspeedx * -1
endif



/*if bally + ballsize > getscreenheight()
Login required to view complete source code

kevin


   Seems like it's coming along, but there seems to be some redundant code creeping in.     So a couple of points to mull over.

   *)  The game treats the single ball separately (variables) from the multi ball.   Wouldn't it make for an easier solution if the game stored all the balls in the typed array.  Then it doesn't matter if there's 1 ball or 50.

   *) In the multi ball processing loop, why it is drawing the blocks each time it processes a ball ?     (approximately lines 380)

   *) In the block drawing loop at approximately line 170->210, the loop is drawing the block and performing ball collisions on with the main ball (represented as variables) and with one of the balls in the ball array...  seems like double up to me

   *) Is that any reason why your using multiple arrays for the Green/Blue/Yellow items.    Seem to me like these couple of condensed in a single array.  And the objects couple be universally managed...

    Just something to chew on.. 

markel422

#25
Quote*)  The game treats the single ball separately (variables) from the multi ball.   Wouldn't it make for an easier solution if the game stored all the balls in the typed array.  Then it doesn't matter if there's 1 ball or 50.

So I should change the original ball into part of the Ball Array too?

Quote*) In the multi ball processing loop, why it is drawing the blocks each time it processes a ball ?    (approximately lines 380)

Because for what I found out that without that code inside there too, the multiple balls won't be able to detect if its hitting any of the blocks to delete it. Unless there is another approach... :-\

Quote*) In the block drawing loop at approximately line 170->210, the loop is drawing the block and performing ball collisions on with the main ball (represented as variables) and with one of the balls in the ball array...  seems like double up to me

So the way I will be able to fix the problem of double to triple item Respawns is to include the Original Ball in the Ball Array?

Quote*) Is that any reason why your using multiple arrays for the Green/Blue/Yellow items.    Seem to me like these couple of condensed in a single array.  And the objects couple be universally managed...

It was the only way I could figure out how to create new items to appear and have there own stored power inside, unless I should use Arrays for this too...

It seems that to Beginners (like me) are not that good of becoming aware of other obvious shortcuts and can only see performing tasks in a longer and harder way (Performing things more than what is necessary). Unless TOLD that there is really another way to really approach it, like children. :)

kevin

#26
QuoteSo I should change the original ball into part of the Ball Array too?

   If you're going to have a single ball.  Then using a group of variables is a good enough way to manage it's properties.  But once  there's  need to handle more than one, then it's arrays.  In your example, all the balls should be represented within an array.   So the control code is universal.    Less code ='s less chance for programming errors to creep in.


QuoteBecause for what I found out that without that code inside there too, the multiple balls won't be able to detect if its hitting any of the blocks to delete it. Unless there is another approach...  

   I'm not sure i'm following your response.  Am I to assume you're not sure where the draw lines are ?  

  This stuff,

PlayBASIC Code: [Select]
 ; 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)



   When you do this inside the extra balls loop, then for each ball you process, it's also drawing all the blocks again.  So 2 extra balls means drawing the blocks 3 times.    So these don't need to be there.  



QuoteSo the way I will be able to fix the problem of double to triple item Respawns is to include the Original Ball in the Ball Array?

    I don't think it's related.  What i'm referring to is the extra code (shown bellow), which is really not necessary and potentially problematic. .    

  lines 198-205

PlayBASIC Code: [Select]
  ;Handle if Balls Touches Blocks
If PointinBox(ball(obj).x,ball(obj).y,blockx,blocky,blockx+48,blocky+18)
bl(ba)=1
ball(obj).speedy=ball(obj).speedy * -1
PlayerScore=PlayerScore+50
Endif



     Notice how the code is using the Obj variable.   The thing is, this variable isn't used in the loop and it's contents can't relied upon, so what ball is checking ?.  Which makes the PointInBox() collision comparison  potentially invalid and problematic.    

      We don't really need it, as were checking for collisions of the extra balls later on in the program.  So this check seems redundant..  


 
QuoteIt was the only way I could figure out how to create new items to appear and have there own stored power inside, unless I should use Arrays for this too...

       Well, using a  typed array to handle all the different items,  would help reduce the code dramatically, and therefore reduce the potential for programmer error.   So i'd be using arrays for this also.  

        Programmer error is when the programmer enters a sequence of code that is logically flawed in some way.   Note: Programming languages are very dumb !..

        At it's core,  Programming is simply about laying out a set of rules for the computer to follow.   The problem is, the computer takes what we tell it,  literally.    So It does exactly what we tell it ,  there's no  if's / but's / maybe's, there's no grey area for it.   If we tell to loop a 1,000,000 times, then what's it'll do.   If we tell it to jump off a cliff, then it will.      Where as new programmers often tend to think in more abstract concepts.   Thinking the machine will fill in the gaps..  Sadly it won't.



   
QuoteIt seems that to Beginners (like me) are not that good of becoming aware of other obvious shortcuts and can only see performing tasks in a longer and harder way (Performing things more than what is necessary).

        When we start learning to program,  there's lots of hurdles to overcome.   Learning a programming languages syntax  (it's  grammar & commands)   is one side of the problem, another is learning the vast array of programming concepts that are implied.   Which are often not language specific..  

        You can write programs without having a great understanding programming concepts,   but the latter helps make people much more self-reliant.    So they can create their own solutions to whatever problem they need to solve.  As often, the problems you'll encounter are specific to your program and way it's set up.  




QuoteUnless TOLD that there is really another way to really approach it, like children.  

       Yes & No.  Generally speaking,  spoon feeding (gimme the codes) doesn't really work to well.    The problem people run into,  is this that it often creates the happy cut an paste programmer.   Which can work, there's nothing really wrong with it, but unless the learner takes the time to go through the solution codes logic ( line by line, command by command if need be) then it doesn't really teach them anything either.      Unfortunately what  inevitably happens is they become dependent on others to solve their programming problems.  

       Which reminds me of the old saying.    "Give a man a fish"  

       A better approach is a combination,  Some code snippets,  the occasional 'do it like this' etc etc do help.  But really the most valued part of the forums and the human resources here, are the concepts and methods being imparting.      Taking the time to get an understanding, will be more beneficially in the long run.  



markel422

#27
My Game is currently stuck right now and am having a few issues on the code and here is the problem, I was trying to experiment with the Arrays for the balls and created an extra one in order for the "Original" Ball to now be part of it. Just as soon as this was replaced as part of to now draw my circle to the screen, it stopped showing the ball, but only appears for a split second and dissappears. To my own knowledge so far this could of been because the ball array not being put inside the Do/Loop. As soon as I did that, it didn't fix the problem, I had even created a variable to control when this ball will show, but now I only end up getting nothing of the ball showing at all this time.

Here's an code example for what I had previously for my ball:

Now why is it that this seems to work?

PlayBASIC Code: [Select]
setfps 60
gosub ball_restart

do
cls 0
x=ballx+ballspeedx
y=bally+ballspeedy
radius=20
colour=rgb(0,0,200)

circlec x,y,radius,1,colour

ballx=x
bally=y

sync
loop

ball_restart:
ballspeedx = rndrange(2,3)
ballspeedy = rndrange(3,4)
ballx=400
bally=300

return



And this doesn't... ???

PlayBASIC Code: [Select]
openscreen 800,600,32,1
setfps 60

type tball
x,y
speedx
speedy
size
colour
endtype
dim ball(3) as tBall
ball(1).x = 400
ball(1).y = 300
ball(1).speedx = rndrange(2,3)
ball(1).speedy = rndrange(3,4)
Ball(1).Size=20
Ball(1).Colour=RGB(0,0,200)

gosub ball_restart

Do
cls 0


x=ball(1).x+ball(1).speedx
y=ball(1).y+ball(1).speedy
radius=ball(1).size
colour=ball(1).colour

circlec x,y,radius,1,colour
ball(1).x=x
ball(1).y=y

Sync
Loop

Ball_restart:
Dim ball(3) as tBall

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)

return



and here's the original code with the type array put in for my original ball:

PlayBASIC Code: [Select]
; PROJECT : BreakoutProject
; AUTHOR : Michael
; CREATED : 11/6/2009
; EDITED : 11/20/2009
; ---------------------------------------------------------------------

openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

//Item Sizes
IWidth=8
IHeight=8

//Player Sizes
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Level = 1
Playerx = 355
Playery = 550
PlayerSize=Width ;Name the same variable so that PB will know the Original Size
PlayerNormalState=0 ;Create Variable that will soon represent the item timer

///Normal BallSize
BallSize =8

gosub Ball_Restart

//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
Constant Grab=10

Global Green=RGB(0,255,0)


;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)

;Dimension for Ball
Dim BAppear(BallMax)
T1=timer()
PlayerGotBall=0



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
Dim Ball(3) as tball


;Create "ORIGINAL" Ball One
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
Ball(2).x=400
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
Ball(3).x=350
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)

Dim bl(160)

Dim Power$(4)
Power$(1)="Normal"
Power$(2)="Shrunk"
Power$(3)="MultiBall"
Power$(4)="Grab"

;Create Variable Mode displaying Power Options
ItemCount=0
DisplayMode=0
EnableItem=1
Menu()




; Init Game Starting variables

GameState=PLaying
PlayerState=Normal


; Play game loop
Do
Cls 0

;Create Movements of Player
Playerx = MouseX()

ball(1).x=Ball(1).x+ball(1).speedx
ball(1).y=Ball(1).y+ball(1).speedy


;Create Collision for Ball on Player
if PointInBox (ball(1).x,ball(1).y,playerx,playery,playerx+PlayerSize,Playery+Height)
Ball(1).speedy=ball(1).speedy * -1
Endif


;Create Player Collision on Screen
If playerx < 0 then playerx = 0
Login required to view complete source code


Does Arrays & Types not work on loops?

kevin

QuoteAnd this doesn't..

   Have you run your second example in the debug mode (F7) ?    I suggest doing that.   If you click the Variable Tab and select the Balls array under Main,  it'll show you just what's inside that array.   If you closely, you'll notice the size field is set to zero.  


Debugger output.

Quote
 ----------------------------------------------------------
BALL
Type =tball
Size =16
Bank =106
----------------------------------------------------------


Element:(1).TBALL
---------------------------------------------
 X  =896
Y  =1044
SPEEDX  =2
SPEEDY  =3
SIZE  =0
COLOUR  =200

   What you're doing, is initializing ball #1 in the array in approximately lines 18 to 25, then immediately after, you're calling the ball_restart subroutine.  This sub routine then overwrites ball #1 with all new settings.   If you look carefully, at the code in Ball_restart subroutine you'll see that the size field is being set to a variable, which you've not assigned anything to.   So the size of the ball you're drawing is zero.  You can't draw nothing.


markel422

#29
Ok alittle Update on my BreakOut Game, so far I remodified the Ball Arrays to accept the Original Ball, that way, you can now safely lose even your original Ball and the game will still go on :).

But now I am puzzled on how to do the same thing with the Item Arrays which is in Line 64-69, if I were to create a type with itemtype after dimming it, I would also have to dim it again in order to accept the ItemMax variable and would have to look like this "Dim Item(1).Appear=(ItemMax)" which will NOT end up good. How could I still DIM the ItemMax into the Item array if it was already dimed for how many Items I want Active (Line 70)?

Also, there is a problem with the switch with the Grab function. What I was trying to do was even with the Original Ball GONE from the screen, I wanted the computer to now concentrate on the other Balls that are left only when the Number_Of_Balls_Left = 1, but for some reason the function would still Grab the Original Ball even if it's pass the screen! ( Which is Cheating! ;)), the only way I came up was to use a variable to represent the switch which is "st=1" in Line 98. Then change the variable in order to make the switch happen which is seen from Lines 249-290. I even PRINTED out the st variable to see what it was doing and found out that it was still on st=1, which got me confused. If this isnt really telling the computer to make another decision to grab another ball if the original ball is through the screen then I don't know what will...

Here's the Code that is remodified with the Ball Arrays this time:

PlayBASIC Code: [Select]
; PROJECT : Breakoutdebug
; AUTHOR : Michael
; CREATED : 12/8/2009
; EDITED : 12/11/2009
; ---------------------------------------------------------------------

openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

//Item Sizes
IWidth=8
IHeight=8

//Player Sizes
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Level = 1
Playerx = 355
Playery = 550
PlayerSize=Width ;Name the same variable so that PB will know the Original Size
PlayerNormalState=0 ;Create Variable that will soon represent the item timer

///Normal BallSize
BallSize =8

//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
Constant Grab=10

Green=RGB(20,200,0)

;Dimension for Ball
Dim BAppear(BallMax)
T1=timer()
PlayerGotBall=0

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
Dim Ball(3) as tball

type tItem
appear
posx
posy
colour
endtype
Dim Item(3) as tItem
;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)

gosub Ball_Restart

Dim bl(160)

Dim Power$(4)
Power$(1)="Normal"
Power$(2)="Shrunk"
Power$(3)="MultiBall"

;Create Variable Mode displaying Power Options
ItemCount=0
DisplayMode=0
EnableItem=1
st=1
Menu()

; Init Game Starting variables
GameState=PLaying

PlayerState=Normal

; Play game loop
Do
Cls 0
;Create Movements of Player
Playerx = MouseX()
Number_of_Balls_Left=1
;Create Collision for Ball on Player
if PointInBox (ball(1).x,ball(1).y,playerx,playery,playerx+PlayerSize,Playery+Height)
Ball(1).speedy=ball(1).speedy * -1
Endif


;Create Player Collision on Screen
If playerx < 0 then playerx = 0
If playerx + PlayerSize > getscreenwidth() then playerx = getscreenwidth()-PlayerSize
;Create Ball Movement

;Create Collision for Ball on Screen
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 bally + ballsize > getscreenheight()
bally = getscreenheight() - ballsize
ballspeedy=ballspeedy * -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

; ----------------------------------------------------------------------------
;Draw Blocks
; ----------------------------------------------------------------------------

NumberOfBlocksLeft=0
BA =0
Login required to view complete source code