Code archives/Algorithms/Simple Type

This code has been declared by its author to be Public Domain code.

Download source code

Simple Type by Filax2004
This is a simple method to learn Type
Graphics 800,600,0

' ---------------------------------
' Init a new type list
' ---------------------------------
Global MyList:TList=CreateList() 
Global MyID

' ----------------
' Type creation
' ----------------
Type MonTest
	Field ID
	Field Px#
	Field Py#
	Field MySpeed#=0.2
	Field MyDirection%=Rand(0,360)
	
	' ------------------------------
	' Redraw
	' ------------------------------
	Method Refresh() 
		SetColor 255,255,255
		DrawRect PX#,PY#,30,30 
		DrawText ID,Px#,Py#-15
	End Method 
	
	' --------------------------------
	' Move rectangle
	' --------------------------------
	Method Go()
		Px#:+MySpeed#*Cos(MyDirection%)
		Py#:+MySpeed#*Sin(MyDirection%)
		
		
		' -----------------------------------------------------------
		' When mousedown i delete type number 5
		' -----------------------------------------------------------
		If Mousedown(1) Then
			If ID=5 Then
				MyList.remove(Self)
			EndIf
		EndIf
	End Method 
End Type

' -----------------------
' Creation des rectangles
' -----------------------
Create()

' -----------------
' Main Loop
' -----------------
While Not KeyDown(Key_Escape)
	For T:MonTest= EachIn MyList
		T.Refresh()
		T.Go()
	next

	Flip
	Cls
Wend 

' -----------------------
' Rect creation
' -----------------------
Function Create()
	For i=1 To 5
		MyID=MyID+1
		
		NewProto:MonTest= New MonTest
		NewProto.ID=MyID
		NewProto.Px#=Rand(5,700)
		NewProto.Py#=Rand(5,500)	
		
		' ---------------------------------
		' Add this entry in the type list
		' ---------------------------------
		ListAddLast MyList,NewProto
	next
End Function

Comments

Koriolis2004
From my memory (didn't bought BlitzMax yet) you can put functions in the type. It is thus better to put the Create function right in the type.
This is the standard way, and beside documenting what this create function realy creates, it also allows to encapsulate the type's data.


Damien Sturdy2004
You need to use FLushMem in there mate ;)


Filax2004
Yes i know my friend :) but for the moment this is my first step in blitzmax :)


Filax2004
This is a new version with create function under type :)




Garfield2005
Hi, just starts a week ago with BMAX, I´m very happy :),
need more those small examples

put a little code inside:
- Event Handling
- push Keys 1-5 to delete specified Boxes
- Re-Create if empty List

Graphics 800,600,0

' ---------------------------------
' Creation d'une liste pour le type
' ---------------------------------
Global MyList:TList=CreateList()
Global MyID

' ----------------
' Creation du type
' ----------------
Type MonTest
Field ID
Field Px#
Field Py#
Field MySpeed#=0.2
Field MyDirection%=Rand(0,360)

' ------------------------------
' La methode de rafraichissement
' ------------------------------
Method Refresh()
SetColor 255,255,255
DrawRect PX#,PY#,30,30
DrawText ID,Px#,Py#-15
End Method

' --------------------------------
' La methode pour les faire bouger
' --------------------------------
Method Go(Key)
Px#:+MySpeed#*Cos(MyDirection%)
Py#:+MySpeed#*Sin(MyDirection%)


' -----------------------------------------------------------
' Si on clique sur le bouton on vire un des type, le numero 5
' -----------------------------------------------------------

If MouseDown(1) Then
If ID=5 Then
MyList.remove(Self)
EndIf
EndIf

If ID = Key Then ' by pushing the button 1-5 of the keyboard
MyList.remove(Self)
EndIf


End Method

' -----------------------
' Creation des rectangles
' -----------------------
Function Create()
For i=1 To 5
MyID=MyID+1

NewProto:MonTest= New MonTest
NewProto.ID=MyID
NewProto.Px#=Rand(5,700)
NewProto.Py#=Rand(5,500)

' ---------------------------------
' On ajoute cette entrée a la liste
' ---------------------------------
ListAddLast MyList,NewProto
Next
MyID = 0 ' Reset for Re-Create
End Function
End Type

' -----------------------
' Creation des rectangles
' -----------------------
MonTest.Create()

' -----------------
' Boucle principale
' -----------------
While Not KeyDown(Key_Escape)
Select WaitEvent ()
Case EVENT_KEYDOWN ' pushin key 1-5 on the keyboard
Key = EventData()-48 ' -48 --> because EventData returns the ASCI Code
End Select

For T:MonTest= EachIn MyList
T.Refresh()
T.Go(Key)
Next
Key = 0
Flip
Cls

If CountList( MyList:TList ) = 0
MonTest.Create() ' Re-Create
End If

Wend


Code Archives Forum