Types...

Blitz3D Forums/Blitz3D Beginners Area/Types...

Someone mind explaining how types work? from what i know its a group of things that share the same attrbutes...

what i'm trying to do is make the walls in a breakout game, how would i go about doing that... heres my code at the moment, i want to learn types, the last time i used them it was just copy and paste..

;Breakout by Yahfree
;2007-04-4 -> ????-??-(?)?
AppTitle "Breakout by Yahfree","Are you sure?"

;Hides our pointer
HidePointer

;sets the graphics mode, and the setbuffer
Graphics 800,600,32,2
SetBuffer BackBuffer()

;Globalized stuff
Global paddle, paddlex=380, paddley=560
Global ball,ballx=380, bally=580, ballxdir#=0, ballydir=-1, move=1, ballsleft=3, life1, life2, life3, speed=1
Global ;blope=LoadSound("Beep.wav")

;Constant numbers that never change..
Const ESC=1, RIGHTARR=205, LEFTARR=203, SPACE=57, MOUSE1=1, A=30, Z=44


;Mid handles all of the images and creates a image called paddle.
AutoMidHandle True
   paddle=CreateImage(70,15)
   ball=CreateImage(10,10)

;draws on the empty image container..
SetBuffer ImageBuffer(paddle)
   Rect 0,0,70,15,1
SetBuffer ImageBuffer(ball)
   Oval 0,0,10,10,1
SetBuffer BackBuffer()

;Copys the ball image 3 times these will be our lifes displayed on the bottom of the screen.
   life1=CopyImage(ball)
   life2=CopyImage(ball)
   life3=CopyImage(ball)

;!@!@!@!@!@!@!@!@!@!@!@!@!@!@
;!@!@!@!@ MAIN LOOP !@!@!@!!@
;!@!@!@!@!@!!@!@!@!@!@!@!@!@!
While Not KeyHit(1)
Cls

;Calls functions.
addplayercontrols()
drawmyimages()
addcollisions()
addtext()

;small delay in the loop for less slow down.
;flips, ends the loop and termanates the program..
Delay 5
  Flip
Wend
End

;Include "Functions.bb"

;Player controls
Function addplayercontrols()

;Paddle movemovement and controls
paddlex=paddlex+MouseXSpeed()
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
If paddlex<35 paddlex=35
If paddlex>765 paddlex=765

;Ball control.
If move=1 ballx=paddlex
If move=1 And ballx<35 ballx=35
If move=1 bally=paddley-15
If move=1 ballydir=-1 ballxdir=0
If KeyHit(A) And speed<25 speed=speed+1
If KeyHit(Z) And speed>1 speed=speed-1
If KeyHit(SPACE) Or MouseHit(MOUSE1)
   move=2
End If

;calls the moveball function if move=2
If move=2
   moveball()
End If

End Function


;Moving the ball
Function moveball()

;All physics behind moving the ball and bouncing it.
bally=bally+ballydir*speed
ballx=ballx+ballxdir*speed
If bally<0 ballydir=1 ;PlaySound(blope)
If bally>600 move=1 ballydir=-1 ballsleft=ballsleft-1
If ballx<0 ballxdir=1 ;PlaySound(blope)
If ballx>800 ballxdir=-1; PlaySound(blope)

End Function

;All the games collisions..
Function addcollisions()

;collisions
If ImagesCollide(paddle,paddlex,paddley,0,ball,ballx,bally,0)
   ballxdir=(ballx - paddlex)*.025
   ballydir=-1
   ;PlaySound(blope)
End If

End Function

;Draws the images
Function drawmyimages()

;Draws the images...
DrawImage paddle,paddlex,paddley
DrawImage ball,ballx,bally
If ballsleft>2 DrawImage life3,680,580
If ballsleft>1 DrawImage life2,700,580
If ballsleft>0 DrawImage life1,720,580
If ballsleft=0 ballsleft=3

End Function

;Text maths ect...
Function addtext()

;All the text..
Text 20,50,"Breakout physics by Yahfree"
Text 20,80,"Controls: Press ESC to exit, LMB to release the ball,"
Text 20,100," mouse to move, A accelerate ball, Z decelerate ball."
Text 20,140,"Status':"
Text 20,160,"Ballspeed: "+speed+" MPH"
Text 20,180,"Ball X: "+ballx
Text 20,200,"Ball Y: "+bally
If ballxdir#<0 And move=2
Text 20,220,"Ball direction Left or right? Left"
Else
Text 20,220,"Ball direction Left or right? Right"
End If
If ballydir<0 And move=2
Text 20,240,"Ball direction Up or Down? Up"
Else
Text 20,240,"Ball direction Up or Down? down"
End If

End Function


Check out this archived article from BlitzCoder. It's Title says "Blitz2D" but it is still relevant to Blitz3D:

Blitz2D Newbies: Definitive Guide to Types:
http://web.archive.org/web/20050319160810/www.blitzcoder.com/cgi-bin/articles/show_article.pl?f=mutteringgoblin09232002232208.html

I tried writing a program that will hopefully clear up the usage of types a bit:
;-----------------------------------------------------------------------------------------------
;								define type
;-----------------------------------------------------------------------------------------------

	Type Block
	
		Field index
		Field x, y ;not used in this program
		
	End Type
	
	
;-----------------------------------------------------------------------------------------------
;								create "Block"s
;-----------------------------------------------------------------------------------------------

	Print "Using 'New' command to create blocks"
	Print "===================================="
	Print
	
	;11 times	
	For i = 0 To 10
		;make new block
		b.Block = New Block
		;set fields
		b\index = i
		b\x = Rand(800)
		b\y = Rand(600)
		;print 'index' field
		Print b\index
	Next
		
	Print
	Print "press key.."
	WaitKey()
	Cls
	Locate 0, 0
	

;-----------------------------------------------------------------------------------------------
;								iterate through all blocks
;-----------------------------------------------------------------------------------------------

	Print "Using 'For..Each..Next' to show blocks"
	Print "======================================"
	Print
	
	;loop through blocks	
	For c.Block = Each Block
	
		;the variable 'c' holds the current block
		Print c\index
		
	Next
	
	Print
	Print "press key.."
	WaitKey()
	Cls
	Locate 0, 0
	
;-----------------------------------------------------------------------------------------------
;								manually iterate through blocks
;-----------------------------------------------------------------------------------------------

	Print "Using 'First' and 'After' commands" 
	Print "to find specific blocks"
	Print "=================================="
	Print
	
	;get first block
	c.Block = First Block
	Print c\index
	
	;get second block
	c = After c
	Print c\index
	
	;get third block
	c = After c
	Print c\index
	
	;get fourth block
	c = After c
	Print c\index
	
	Print
	Print "press key.."
	WaitKey()
	Cls
	Locate 0, 0
	
	
;-----------------------------------------------------------------------------------------------
;								delete block
;-----------------------------------------------------------------------------------------------

	Print "Using 'Delete' command to remove blocks"
	Print "======================================="
	Print
	
	;get first block
	c = First Block
	;get fourth block
	c = After After After c
	
	;delete block
	Delete c
	
	;show blocks again
	For c.Block = Each Block
	
		Print c\index
		
	Next
	
	Print
	Print "press key.."
	WaitKey()
	Cls
	Locate 0, 0
	
	
;-----------------------------------------------------------------------------------------------
;								insert block
;-----------------------------------------------------------------------------------------------

	Print "Using 'Insert' command to change the blocks order"
	Print "================================================="
	Print

	;insert first block after last block 3 times		
	Insert First block After Last Block
	Insert First block After Last Block
	Insert First block After Last Block
	
	;show blocks again
	For c.Block = Each Block
	
		Print c\index
		
	Next
		
	Print
	Print "press key.."
	WaitKey()
	End