跳转到内容

BlitzMax/模块/BASIC/BlitzMax 运行时

来自维基教科书,开放世界中的开放书籍

Blitz 运行时模块提供 BlitzMax 应用程序运行时所需的低级功能。 这包括内存管理、异常处理以及字符串和数组操作等。

此模块提供的许多功能对应用程序程序员隐藏,而是由编译器在“幕后”使用。 但是,有一些非常有用的命令用于调试、内存管理和简单的标准 IO。

全局变量

[编辑 | 编辑源代码]

Global AppDir$="bbAppDir"

描述: 应用程序目录

信息: AppDir 全局变量包含当前正在执行的应用程序的完全限定目录。 应用程序启动时,应用程序的初始当前目录也设置为 AppDir

示例:

' appdir.bmx
' requests the user to select a file from the application's directory

Print "Application Directory="+AppDir$

file$=RequestFile("Select File to Open","",False,AppDir$)

Print "file selected was :"+file

Global AppFile$="bbAppFile"

描述: 应用程序文件名

信息: AppFile 全局变量包含当前正在执行的应用程序的完全限定文件名。

示例:

' appfile.bmx

Print "This program's executable is located at "+AppFile$

Global AppTitle$="bbAppTitle"

描述: 应用程序标题

信息: AppTitle 全局变量在需要默认应用程序标题时由各种命令使用 - 例如,在打开简单的窗口或请求器时。 最初,AppTitle 设置为值“BlitzMax Application”。 但是,您可以随时使用简单的赋值更改 AppTitle

Global AppArgs$[]="bbAppArgs"

描述: 启动时传递给应用程序的参数

信息: AppArgs 全局数组包含应用程序启动时发送给应用程序的命令行参数。 AppArgs 的第一个元素始终包含应用程序的名称。 但是,名称的格式可能会根据应用程序的启动方式而改变。 使用 AppDirAppFile 获取有关应用程序名称或目录的一致信息。

示例:

' appargs.bmx
' print the command line arguments passed to the program at runtime

Print "Number of arguments = "+AppArgs.length

For a$=EachIn AppArgs
Print a$
Next

LaunchDir

[编辑 | 编辑源代码]

Global LaunchDir$="bbLaunchDir"

描述: 启动应用程序的目录

信息: LaunchDir 全局变量包含启动应用程序时的当前目录。 这主要用于可能需要访问“shell”当前目录而不是应用程序目录的命令行工具。

示例:

' launchdir.bmx

Print "This program was launched from "+LaunchDir$

RuntimeError

[编辑 | 编辑源代码]

Function RuntimeError( message$ )

描述: 产生运行时错误

信息: 抛出 TRuntimeException。

示例:

' runtimeerror.bmx

If a=0 RuntimeError "This program has failed badly."

DebugStop

[edit | edit source]

Function DebugStop()

描述: 停止程序执行并进入调试器

信息: 如果没有调试器,则忽略此命令。

DebugLog

[edit | edit source]

Function DebugLog( message$ )

描述: 将字符串写入调试日志

信息: 如果没有调试器,则忽略此命令。

注释: 请注意,与 Assert 不同,DebugLog 将在无论编译模式如何都会被编译 - 只是在发布模式下不打印任何内容。

OnEnd

[edit | edit source]

Function OnEnd( fun() )

描述: 添加一个在程序结束时调用的函数

信息: OnEnd 允许您指定一个在程序结束时调用的函数。OnEnd 函数以添加它们的相反顺序调用。

示例:

' onend.bmx

Function cleanup()
Print "cleaning up"
End Function

OnEnd cleanup
Print "program running"
End	'the cleanup function will be called at this time

ReadStdin

[edit | edit source]

Function ReadStdin$()

描述: 从 stdin 读取字符串

返回值: 从 stdin 读取的字符串。如果存在换行符,则将其包含在返回的字符串中。

WriteStdout

[edit | edit source]

Function WriteStdout( str$ )

描述: 将字符串写入 stdout

信息: 将 str 写入 stdout 并刷新 stdout。

WriteStderr

[edit | edit source]

Function WriteStderr( str$ )

描述: 将字符串写入 stderr

信息: 将 str 写入 stderr 并刷新 stderr。

Delay

[edit | edit source]

Function Delay( millis )

描述: 等待给定的毫秒数

信息: Delay 暂停程序执行至少 millis 毫秒。毫秒是千分之一秒。

MilliSecs

[edit | edit source]

Function MilliSecs())

描述: 获取毫秒计数器

返回值: 计算机开机以来的毫秒数。

信息: MilliSecs 返回计算机开机以来的毫秒数。毫秒是千分之一秒。

MemAlloc

[edit | edit source]

Function MemAlloc:Byte Ptr( size )

描述: 分配内存

返回值: 一个新的内存块,长度为 size 字节

MemFree

[edit | edit source]

Function MemFree( mem:Byte Ptr )

描述: 释放分配的内存

信息: 由 mem 指定的内存必须先前由 MemAllocMemExtend 分配。

MemExtend

[edit | edit source]

Function MemExtend:Byte Ptr( mem:Byte Ptr,size,new_size )

描述: 扩展内存块

返回值: 一个新的内存块,长度为 new_size 字节

信息: 由 memsize 指定的现有内存块被复制到一个新的长度为 new_size 字节的内存块中。释放现有块并返回新块。

MemClear

[edit | edit source]

Function MemClear( mem:Byte Ptr,size )

描述: 将内存块清除为 0

MemCopy

[edit | edit source]

Function MemCopy( dst:Byte Ptr,src:Byte Ptr,size )

描述: 复制一个非重叠的内存块

MemMove

[edit | edit source]

Function MemMove( dst:Byte Ptr,src:Byte Ptr,size )

描述: 复制一个可能重叠的内存块

GCSetMode

[edit | edit source]

Function GCSetMode( mode )

描述: 设置垃圾收集器模式

信息: mode 可以是以下之一
1 : 自动 GC - 内存将自动被垃圾回收
2 : 手动 GC - 在调用 GCCollect 之前不会回收任何内存
默认的 GC 模式是自动 GC。

GCSuspend

[edit | edit source]

Function GCSuspend()

描述: 暂停垃圾收集器

信息: GCSuspend 临时暂停垃圾收集器。在调用 GCSuspend 之后,将不会执行垃圾回收。
使用 GCResume 恢复垃圾收集器。请注意,GCSuspendGCResume '嵌套',这意味着对 GCSuspend 的每次调用都必须与对 GCResume 的调用匹配。

GCResume

[edit | edit source]

Function GCResume()

描述: 恢复垃圾收集器

信息: GCResume 在调用 GCSuspend 之后恢复垃圾回收。
有关更多详细信息,请参阅 GCSuspend

GCCollect

[edit | edit source]

Function GCCollect()

描述: 运行垃圾收集器

返回值: 收集的内存量(以字节为单位)。

信息: 如果垃圾收集器因 GCSuspend 而被暂停,则此函数将无效。

GCMemAlloced

[edit | edit source]

Function GCMemAlloced()

描述: 应用程序分配的内存

返回值: 应用程序当前分配的内存量(以字节为单位)。

信息: 此函数仅返回 '托管内存'。这包括应用程序使用的所有对象、字符串和数组。

GCEnter

[edit | edit source]

Function GCEnter()

描述: 私有:请勿使用

Function GCLeave()

描述: 私有:请勿使用

HandleFromObject

[编辑 | 编辑源代码]

Function HandleFromObject( obj:Object )

描述: 将对象转换为整数句柄

返回值: 一个整数对象句柄

信息: 将对象转换为整数句柄后,您必须使用 Release 命令释放它。

HandleToObject

[编辑 | 编辑源代码]

Function HandleToObject:Object( handle )

描述: 将整数句柄转换为对象

返回值: 与整数句柄关联的对象

示例:

' handlefromobject.bmx

Type MyType
	Field x:Int=100
End Type

Local obj:MyType=New MyType
Local handle:Int=HandleFromObject(obj)
Print "handle = "+handle

Local obj2:MyType=MyType(HandleToObject(handle))
If obj=obj2 Then Print "obj = obj2"
Print "x = "+obj2.x

Release handle

关键词

[编辑 | 编辑源代码]

描述: 设置严格模式

信息: 有关严格模式编程的更多信息,请参阅 BASIC 兼容性

注释: 在严格模式下,允许自动数据类型。默认数据类型为 Int。

示例:

Rem
Strict advises the BlitzMax compiler to report as errors all auto defined variables.
End Rem

Strict

local a=10

b=20	'compiler error, strict mode means variables must be declared b4 use

SuperStrict

[编辑 | 编辑源代码]

描述: 设置 SuperStrict 模式

注释: 在 SuperStrict 模式下,必须始终指定数据类型。

描述: 结束程序执行

示例:

Rem
The End command causes a program to exit immediately.
End Rem

Print "Hello!"
End
Print "This line will never be printed as the program has already been ended."

描述: 开始一个注释块

示例:

Rem

My Rem Example
First created 9th August, 2004

(C)2050 Blitz Intergalactic Software Corporation

End Rem

Print "This program has no useful function"

Rem

Remarks are useful for making your program easily readable.
You can leave details explaining the function of your program
in a remarks section so that you and any other programmers
that work with your code can more easily understand the workings
of your program

End Rem

Print "Sorry."
End

描述: 结束一个注释块

示例:

Rem
All code between Rem and EndRem is ignored by the BlitzMax compiler.
Print "hello"
End Rem

Print "goodbye"

描述: 值为 1 的常量整数

示例:

Rem
True is a Constant Integer assigned the value 1.
End Rem

Print "True="+True

If True
	Print "This line will always be executed"
EndIf

If Not True
	Print "This line will never be executed"
EndIf

描述: 值为 0 的常量整数

示例:

Rem
False is a Constant Integer assigned the value 0.
End Rem

Print "False="+False

If False
	Print "This code will never be executed"
Endif

描述: 常量 Pi 值: 3.1415926535897932384626433832795

示例:

Rem
Pi is a Constant Double assigned the value 3.1415926535897932384626433832795
End Rem

Print "Pi="+Pi

描述: 获取默认 Null 值

示例:

Rem
Null is a BlitzMax Constant representing an empty Object reference.
End Rem

Type mytype
	Field	atypevariable
End Type

Global a:mytype

if a=null Print "a is uninitialized"
a=new mytype
if a<>null Print "a is initialized"

描述: 无符号 8 位整数类型

示例:

Rem
Byte is an unsigned 8 bit integer BlitzMax primitive type.
End Rem

Local a:Byte

a=512;Print "a="+a	'prints 0
a=-1;Print "a="+a	'prints 255

描述: 无符号 16 位整数类型

示例:

Rem
Short is an unsigned 16 bit integer BlitzMax primitive type.
End Rem

Local a:Short

a=65536;Print "a="+a	'prints 0
a=-1;Print "a="+a	'prints 65535

描述: 有符号 32 位整数类型

示例:

Rem
Int is a signed 32 bit integer BlitzMax primitive type.
End Rem

Local a:Int

' the following values all print 0 as BlitzMax "rounds to zero"
' when converting from floating point to integer

a=0.1;Print a
a=0.9;Print a
a=-0.1;Print a
a=-0.9;Print a

描述: 有符号 64 位整数类型

示例:

Rem
Long is a signed 64 bit integer BlitzMax primitive type.
End Rem

Const MAXLONG:Long=$7fffffffffffffff:Long
Const MINLONG:Long=$8000000000000000:Long

Print "A long can have the maximum value of:"+MAXLONG
Print "A long can have the minimum value of:"+MINLONG

描述: 32 位浮点类型

示例:

Rem
Float is a 32 bit floating point BlitzMax primitive type.
End Rem

Local a:Float

a=1

For i=1 To 8
	Print a
	a=a*0.1
Next

For i=1 To 8
	a=a*10
	Print a
Next

描述: 64 位浮点类型

示例:

Rem
Double is a 64 bit floating point BlitzMax primitive type.
End Rem

Local speedoflight:Double
Local distance:Double
Local seconds:Double

speedoflight=299792458:Double	'meters per second
distance=149597890000:Double	'average distance in meters from earth sun

seconds=distance/speedoflight

Print "Number of seconds for light to travel from earth to sun="+seconds

描述: 字符串类型

示例:

Rem
String is a BlitzMax container type containing a sequence of unicode characters.
End Rem

quote:String=Chr(34)
Print quote+"Hello World!"+quote

描述: 对象类型

示例:

Rem
Object is the base class of all BlitzMax types.

The following function attempts to cast from any object to
the custom type TImage and returns true if the given object
is an instance of TImage or an instance of a &Type derived
from TImage.
End Rem

Function IsImage(obj:Object)
	If TImage(obj) return True
	Return False
End Function

描述: 用于“按引用”类型的复合类型说明符

示例:

Rem
Var is a composite type containing a reference to a variable of the 
specified Type.
End Rem

' the following illustrates parsing function parameters by reference

Function ReturnMultiplevalues(a Var,b Var,c Var)
	a=10
	b=20
	c=30
	Return
End Function

Local x,y,z

ReturnMultipleValues(x,y,z)

Print "x="+x	'10
Print "y="+y	'20
Print "z="+z	'30

描述: 用于指针类型的复合类型说明符

示例:

Rem
Ptr is a composite type containing a pointer to a variable of the specified Type.
End Rem

' the following illustrates the use of traditional c style pointers

Local c[]=[1,2,3,4]
Local p:Int Ptr

p=c
Print "pointer 'p' points to:"+p[0]	'1

p:+1
Print "pointer 'p' points to:"+p[0]	'2

p:+1
Print "pointer 'p' points to:"+p[0]	'3

p:+1
Print "pointer 'p' points to:"+p[0]	'4

描述: 开始一个条件块。

示例:

Rem
If begins a conditional block.
End Rem

If 3<5 Print "3<5"	'single line if

If 5<7			'if block
	Print "5<7"
EndIf

描述: If 语句中条件和关联代码之间的可选分隔符。

示例:

Rem
Then is an optional separator between the condition and the block of code following an If statement.
End Rem

If 3<5 Then Print "3<5"

描述: Else 提供了 If Then 结构在 If 条件为 False 时执行第二块代码的功能。

示例:

Rem
Else provides the ability for an If Then construct to execute a second block of code when the If condition is false.
End Rem

i=3

If i<5 Print "i<5" Else Print "i>=5"	'single line If Else

If i<5					'block style If Else
	Print "i<5"
Else
	Print "i>=5"
EndIf

描述: ElseIf 提供了在初始条件失败时测试和执行代码段的功能。

示例:

Rem
ElseIf provides the ability to test and execute a section of code if the initial condition failed.
End Rem

age=Int( Input("How old Are You?") )

If age<13
	Print "You are young"
ElseIf age<20
	Print "You are a teen!"
Else
	Print "You are neither young nor a teen"
EndIf

EndIf

[edit | edit source]

描述: 标记 If Then 块的结束。

示例:

Rem
EndIf marks the end of an If Then block.
End Rem

i=5

If i<10
	Print "i<10"
EndIf

描述: 标记使用迭代器重复执行代码段的循环的开始。

示例:

Rem
For marks the start of a loop that uses an iterator to execute a section of code repeatedly.
End Rem

' print 5 times table

For i=1 to 12
	Print "5*"+i+"="+5*i
Next

描述: 后面跟着一个常量,用于计算何时退出 For..Next 循环。

示例:

Rem
Followed by a constant which is used to calculate when to exit a For..Next loop.
End Rem

For i=1 To 5
	Print i
Next

描述: 指定一个可选常量,用于递增 For 迭代器。

示例:

Rem
Specifies an optional constant that is used to increment the For iterator.
End Rem

' count backwards from 10 to 0

For i=10 to 0 step -1
	Print i
Next

描述: 结束 For 块

示例:

Rem
Marks the end of a For section.
End Rem

For i=1 To 5;Print i;Next

EachIn

[edit | edit source]

描述: 遍历数组或集合

示例:

Rem
Specifies a BlitzMax collection type whose values are assigned sequentially to the For iterator.
End Rem

Local a[]=[0,5,12,13,20]

For b=EachIn a
	Print b
Next

While

[edit | edit source]

描述: 在条件为真时执行代码块

示例:

Rem
While executes the following section of code repeatedly while a given condition is true.
End Rem

Graphics 640,480
While Not KeyHit(KEY_ESCAPE)	'loop until escape key is pressed
	Cls
	For i=1 to 200
		DrawLine rnd(640),rnd(480),rnd(640),rnd(480)
	Next
	Flip
Wend

描述: 结束 While 块

示例:

Rem
Wend marks the end of a While section.
End Rem

While i<5
	Print i
	i:+1
Wend

EndWhile

[edit | edit source]

描述: 结束 While 块

Repeat

[edit | edit source]

描述: 直到满足终止条件或永远执行代码块

示例:

Rem
Repeat executes the following section of code until a terminating condition is true.
End Rem

Repeat
	Print i
	i:+1
Until i=5

Until

[edit | edit source]

描述: 有条件地继续 Repeat 块

示例:

Rem
Until marks the end of a Repeat block and is followed by a terminating condition.
End Rem

i=2
Repeat
	Print i
	i:*2
Until i>1000000

Forever

[edit | edit source]

描述: 无限地继续 Repeat 块

示例:

Rem
Forever is an alternate ending to a Repeat block that will cause the loop to always repeat.
End Rem

Repeat
	Print i+" Ctrl-C to End!" 
	i:+1
Forever

Select

[edit | edit source]

描述: 开始 Select 块

示例:

Rem
Select begins a block featuring a sequence of multiple comparisons with a single value.
End Rem

a=Int( Input("Enter Your Country Code ") )

Select a
	Case 1
		Print "You are from America"
	Case 44
		Print "You are from the United Kingdom"
	Case 62
		Print "You are from Australia"
	Case 64
		Print "You are from New Zealand"
	Default
		Print "I cannot tell which country you are from"
End Select

EndSelect

[edit | edit source]

描述: 结束 Select 块

示例:

Rem
EndSelect marks the end of a Select block.
End Rem

SeedRnd MilliSecs()

a=Rand(5)

Select a
	Case 1 Print "one"
	Case 2 Print "two"
	Case 3 Print "three"
	Case 4 Print "four"
	Case 5 Print "five"
	Default Print "Program Error"
End Select

描述: Select 块中的条件代码

示例:

' case.bmx

' Case performs a comparison with the preceeding value(s) and that
' listed in the enclosing Select statement:

a=Int( Input("Enter a number between 1 and 5 ") )

Select a
	Case 1 Print "You think small"
	Case 2 Print "You are even tempered"
	Case 3,4 Print "You are middle of the road"	
	Case 5 Print "You think big"
	Default Print "You are unable to follow instructions"
End Select		

Default

[edit | edit source]

描述: Select 块中的默认代码

示例:

Rem
Default is used in a Select block to mark a code section that is executed if all prior Case statements fail.
End Rem

a$=Input("What is your favorite color?")
a$=Lower(a$)	'make sure the answer is lower case

Select a$
	Case "yellow" Print "You a bright and breezy"
	Case "blue" Print "You are a typical boy"
	Case "pink" Print "You are a typical girl"
	Default Print "You are quite unique!"
End Select

描述: 退出封闭循环

示例:

Rem
Exit causes program flow to exit the enclosing While, Repeat or For loop.
End Rem

Repeat
	Print n
	n:+1
	If n=5 Exit
Forever

Continue

[edit | edit source]

描述: 继续执行封闭循环

示例:

Rem
Continue causes program flow to return to the start of the enclosing While, Repeat or For program loop
End Rem

For i=1 To 20
	If i Mod 2 Continue
	Print i
Next

Const

[edit | edit source]

描述: 声明一个常量

示例:

Rem
Const defines the preceeding variable declaration as constant.
End Rem

Const ON=True
Const OFF=False

Const TWOPI#=2*Pi

Print TWOPI

Local

[edit | edit source]

描述: 声明一个局部变量

示例:

Rem
Local defines a variable as local to the Method or Function it is defined meaning it is automatically released when the function returns.
End Rem

Function TestLocal()
	Local	a
	a=20
	Print "a="+a
	Return
End Function

TestLocal
Print "a="+a	'prints 0 or if in Strict mode is an error as a is only local to the TestLocal function

Global

[edit | edit source]

描述: 声明一个全局变量

示例:

Rem
Global defines a variable as Global allowing it be accessed from within Methods and Functions.
End Rem

Global a=20

Function TestGlobal()
	Print "a="+a
End Function

TestGlobal
Print "a="+a

Field

[edit | edit source]

描述: 声明一个 Field 变量

示例:

Rem
Field is used to declare the member variable(s) of a type.
End Rem

Type TVector
	Field	x,y,z
End Type

Local a:TVector=New TVector

a.x=10
a.y=20
a.z=30

Function

[edit | edit source]

描述: 开始一个函数声明

示例:

Rem
Function marks the beginning of a BlitzMax function declaration.

When a function does not return a value the use of brackets when
calling the function is optional.
End Rem

Function NextArg(a$)
	Local	p
	p=Instr(a$,",")
	If p 
		NextArg a$[p..]
		Print a$[..p-1]
	Else
		Print a$
	EndIf
End Function

NextArg("one,two,three,four")

NextArg "22,25,20"	'look ma, no brackets

EndFunction

[edit | edit source]

描述: 结束一个函数声明

示例:

Rem
Function marks the end of a BlitzMax function declaration.
End Rem

Function RandomName$()
	local a$[]=["Bob","Joe","Bill"]
	Return a[Rnd(Len a)]
End Function

For i=1 To 5
	Print RandomName$()
Next

Return

[edit | edit source]

描述: 从函数返回

示例:

Rem
Return exits a BlitzMax function or method with an optional value.
The type of return value is dictated by the type of the function.
End Rem

Function CrossProduct#(x0#,y0#,z0#,x1#,y1#,z1#)
	Return x0*x1+y0*y1+z0*z1
End Function

Print "(0,1,2)x(2,3,4)="+CrossProduct(0,1,2,2,3,4)

Function LongRand:Long()
	Return (Rand($80000000,$7fffffff) Shl 32)|(Rand($80000000,$7fffffff))
End Function

Print "LongRand()="+LongRand()
Print "LongRand()="+LongRand()

描述: 开始一个用户定义的类型声明

示例:

Rem
Type marks the beginning of a BlitzMax custom type.

Standard BlitzMax types use a preceeding "T" naming
convention to differentiate themselves from standard
BlitzMax variable names.
End Rem

Type TVector
	Field	x,y,z
End Type

Local a:TVector=New TVector

a.x=10
a.y=20
a.z=30

EndType

[edit | edit source]

描述: 结束一个用户定义的类型声明

示例:

Rem
EndType marks the end of a BlitzMax custom type.
End Rem

Type TVector
	Field	x,y,z
End Type

Local a:TVector=New TVector

a.x=10
a.y=20
a.z=30

Extends

[edit | edit source]

描述: 指定用户定义类型超类型

注释: 类型继承了给定的基本类型。

示例:

Rem
Extends is used in a BlitzMax Type declaration to derive the Type from a specified base class.
End Rem

Type TShape
	Field	xpos,ypos
	Method Draw() Abstract
End Type

Type TCircle Extends TShape
	Field	radius
	
	Function Create:TCircle(x,y,r)
		Local c:TCircle=New TCircle
		c.xpos=x;c.ypos=y;c.radius=r
		Return c
	End Function
	
	Method Draw()
		DrawOval xpos,ypos,radius,radius
	End Method
End Type

Type TRect Extends TShape
	Field	width,height
	
	Function Create:TRect(x,y,w,h)
		Local r:TRect=New TRect
		r.xpos=x;r.ypos=y;r.width=w;r.height=h
		Return r
	End Function
	
	Method Draw()
		DrawRect xpos,ypos,width,height
	End Method
End Type

Local 	shapelist:TShape[4]
Local	shape:TShape

shapelist[0]=TCircle.Create(200,50,50)
shapelist[1]=TRect.Create(300,50,40,40)
shapelist[2]=TCircle.Create(400,50,50)
shapelist[3]=TRect.Create(200,180,250,20)

Graphics 640,480
While Not KeyHit(KEY_ESCAPE)
	Cls
	For shape=EachIn shapelist
		shape.draw
	Next
	Flip
Wend
End

方法

[edit | edit source]

描述: 开始方法声明

注释: 方法是特定于实例的函数。

示例:

Rem
Method marks the beginning of a BlitzMax custom type member function.
End Rem

Type TPoint
	Field	x,y

	Method ToString$()
		Return x+","+y
	End Method
End Type

a:TPoint=New TPoint
Print a.ToString()

EndMethod

[edit | edit source]

描述: 结束方法声明

示例:

Rem
EndMethod marks the end of a BlitzMax Method declaration.
End Rem

Type TPoint
	Field	x,y

	Method ToString$()
		Return x+","+y
	End Method
End Type

a:TPoint=New TPoint
Print a.ToString()

抽象

[edit | edit source]

描述: 将类型或方法标记为抽象

注释: 类型或方法必须扩展(如果未扩展,编译将失败)。

示例:

Rem
A BlitzMax type that contains Abstract methods becomes abstract itself.
Abstract types are used to define interfaces that extending types must 
implement before they can be used to create new instances.

In the following code TShape is an abstract type in that you can not
create a TShape but anything extending a TShape must implement a Draw()
method.
End Rem

Type TShape
	Field	xpos,ypos
	Method Draw() Abstract
End Type

Type TCircle Extends TShape
	Field	radius
	
	Function Create:TCircle(x,y,r)
		Local c:TCircle=New TCircle
		c.xpos=x;c.ypos=y;c.radius=r
		Return c
	End Function
	
	Method Draw()
		DrawOval xpos,ypos,radius,radius
	End Method
End Type

Type TRect Extends TShape
	Field	width,height
	
	Function Create:TRect(x,y,w,h)
		Local r:TRect=New TRect
		r.xpos=x;r.ypos=y;r.width=w;r.height=h
		Return r
	End Function
	
	Method Draw()
		DrawRect xpos,ypos,width,height
	End Method
End Type

Local 	shapelist:TShape[4]
Local	shape:TShape

shapelist[0]=TCircle.Create(200,50,50)
shapelist[1]=TRect.Create(300,50,40,40)
shapelist[2]=TCircle.Create(400,50,50)
shapelist[3]=TRect.Create(200,180,250,20)

Graphics 640,480
While Not KeyHit(KEY_ESCAPE)
	Cls
	For shape=EachIn shapelist
		shape.draw
	Next
	Flip
Wend
End

最终

[edit | edit source]

描述: 将类型或方法标记为最终

注释: 类型或方法不可扩展。

示例:

Rem
Final stops methods from being redefined in super classes.
End Rem

Type T1
	Method ToString$() Final
		Return "T1"
	End Method
End Type

Type T2 Extends T1
	Method ToString$()	'compile time error "Final methods cannot be overridden"
		Return "T2"
	End Method
End Type

描述: 创建用户定义类型的实例

示例:

Rem
New creates a BlitzMax variable of the Type specified.
End Rem

Type MyType
	Field	a,b,c
End Type

Local t:MyType
t=New MyType
t.a=20

Print t.a

' if a new method is defined for the type it will also be called

Type MyClass
	Field	a,b,c
	Method New()
		Print "Constructor invoked!"
		a=10
	End Method
End Type

Local c:MyClass
c=New MyClass
Print c.a

自我

[edit | edit source]

描述: 对此方法的对象实例的引用

注释: 允许对象访问其自身的实例(在大多数情况下不需要)。

示例:

Rem
Self is used in BlitzMax Methods to reference the invoking variable.
End Rem

Type MyClass
	Global	count	
	Field	id
	
	Method New()
		id=count
		count:+1
		ClassList.AddLast(Self)	'adds this new instance to a global list		
	End Method
End Type

Global ClassList:TList

classlist=New TList

Local c:MyClass

c=New MyClass
c=New MyClass
c=New MyClass

For c=EachIn ClassList
	Print c.id
Next

超级

[edit | edit source]

描述: 对超类型对象实例的引用

注释: 允许对象访问其父类型(它扩展的类型)。

示例:

Rem
Super evaluates to Self cast to the method's immediate base class.
End Rem

Type TypeA
	Method Report()
		Print "TypeA reporting"
	End Method
End Type

Type TypeB Extends TypeA
	Method Report()
		Print "TypeB Reporting"
		Super.Report()
	End Method
End Type

b:TypeB=New TypeB
b.Report()

删除

[edit | edit source]

描述: 为将来扩展保留

示例:

Rem
Reserved for future expansions.
End Rem

释放

[edit | edit source]

描述: 释放整数对象句柄

示例:

Rem
Release removes the internal reference caused by creating an integer handle to a type.
End Rem

Type MyType
	Field bigmap[1024*1024]
End Type

GCCollect
Print GCMemAlloced()

a=New MyType
GCCollect
Print GCMemAlloced()

Release a
GCCollect
Print GCMemAlloced()

公开

[edit | edit source]

描述: Public 使常量、全局变量或函数从当前源文件外部访问(默认)

示例:

Rem
Public makes a variable, function or method accessible from outside the current source file (default).
End Rem

Public

Global	Score,Lives,Health

Private

Global	posx,posy,posz

私人

[edit | edit source]

描述: Private 使常量、全局变量或函数只能从当前源文件内部访问

示例:

Rem
Private makes a variable, function or method only accessible from within the 
current source file.
End Rem

Public

Global	Score,Lives,Health

Private

Global	posx,posy,posz

外部

[edit | edit source]

描述: Extern 标记外部函数声明列表的开头

示例:

Rem
Extern marks the beginning of an external list of function declarations.
End Rem

Extern 
	Function puts( str$z )
	Function my_puts( str$z )="puts"
End Extern

puts "Using clib's put string!"
my_puts "Also using clib's put string!"

EndExtern

[edit | edit source]

描述: EndExtern 标记 Extern 部分的结尾

示例:

Rem
EndExtern marks the end of an Extern section.
End Rem

Extern 
	Function puts( str$z )
End Extern

puts "Using clib's put string!"

模块

[edit | edit source]

描述: 声明模块范围和标识符

信息: 有关更多信息,请参见 模块

示例:

Rem
The Module keyword advises the BlitzMax program maker BMK to create a code 
module from the source file.
End Rem

Module PUB.Sequencer

ModuleInfo "Framework: Audio Sequencer for use with FreeAudio"
ModuleInfo "Copyright: Blitz Research Ltd"
ModuleInfo "Author: Simon Armstrong"
ModuleInfo "Version: 1.00"

ModuleInfo

[edit | edit source]

描述: 定义模块属性

示例:

Rem
ModuleInfo allows properties such as Author and Copyright to be included in a module file.
End Rem

Module PUB.Sequencer

ModuleInfo "Framework: Audio Sequencer for use with FreeAudio"
ModuleInfo "Copyright: Blitz Research Ltd"
ModuleInfo "Author: Simon Armstrong"
ModuleInfo "Version: 1.00"

Incbin

[edit | edit source]

描述: 嵌入数据文件

示例:

Rem
IncBin embeds an external data file in a BlitzMax program that can 
then be read using the "incbin::" device name.
End Rem

' code snippet from demos/firepaint/firepaint.bmx

Incbin "stars.png"

Local stars=LoadImage( "incbin::stars.png" )

IncbinPtr

[edit | edit source]

描述: 获取嵌入数据文件的起始地址

示例:

Rem
IncBinPtr returns a byte pointer to the specified embedded binary file.
End Rem

Incbin "incbinptr.bmx"

Local p:Byte Ptr=IncbinPtr("incbinptr.bmx")
Local bytes=IncbinLen("incbinptr.bmx")

Local s$=String.FromBytes(p,bytes)

Print "StringFromBytes(p,bytes)="+s$

IncbinLen

[edit | edit source]

描述: 获取嵌入数据文件的长度

示例:

Rem
IncBinLen returns the size in bytes of the specified embedded binary file.
End Rem

Incbin "incbinlen.bmx"

Local p:Byte Ptr=IncbinPtr("incbinlen.bmx")
Local bytes=IncbinLen("incbinlen.bmx")

Local s$=StringFromBytes(p,bytes)

Print "StringFromBytes(p,bytes)="+s$

包括

[edit | edit source]

描述: Include 有效地将指定文件“插入”正在编译的文件中。

框架

[edit | edit source]

描述: Framework 使用指定的模块构建 BlitzMax 应用程序,而不是构建所有已安装的模块。

注释: 如果使用 Framework 指定了某个模块,则仅该模块及其导入的模块会与代码一起编译。然后,您可以使用 Import 导入其他模块。常见的框架模块是 _Brl.Basic_ 和 Brl.Max2D,分别用于控制台和 2D 应用程序。

进口

[edit | edit source]

描述: 从模块或源文件导入声明

示例:

Rem
:Import specifies the external BlitzMax modules and source files used by the program.
End Rem

Framework BRL.GlMax2D

Import BRL.System

Graphics 640,480,32

While Not KeyHit(KEY_ESCAPE)
	Cls
	DrawText "Minimal 2D App!",0,0
	Flip
Wend

断言

[edit | edit source]

描述: 如果条件为 False,则抛出 RuntimeError

注释: Assert 在调试模式下抛出运行时错误。

示例:

Rem
Assert generates a BlitzMax runtime error if the specified condition is false.
End Rem

a=LoadImage("nonexistant image file")
Assert a,"Image Failed to Load"

描述: 将程序流程转移到指定的标签

注释: Goto 在 SuperStrict 或 Strict 模式下不起作用。 此外,标签仅限于 DefData.

示例:

Rem
Causes program execution to jump to the #label specified.
End Rem

Print "one"
Goto here
Print "two"
#here
Print "three"

描述: 开始声明 Try 块

注释: Try 块尝试捕获可能在 Try 和 Catch 之间抛出的异常,并尝试处理该异常(而不是终止程序)。

示例:

Rem
Begin declaration of a Try block.
End Rem

Try
	Repeat
		a:+1
		Print a
		If a>20 Throw "chunks"
	Forever
Catch a$
	Print "caught exception "+a$
EndTry

描述: 结束声明 Try 块

示例:

Rem
EndTry
EndTry marks the end of a Try block.
End Rem

描述: 在 Try 块中捕获异常对象

示例:

Rem
Catch defines an exception handler following a Try..EndTry Block.
End Rem

Try
	Repeat
		a:+1
		Print a
		If a>20 Throw "chunks"
	Forever
Catch a$
	Print "caught exception "+a$
EndTry

描述: 将异常对象抛出到包含的 Try 块

示例:

Rem
Throw generates a BlitzMax exception.
End Rem

Try
	Repeat
		a:+1
		Print a
		If a>20 Throw "chunks"
	Forever
Catch a$
	Print "caught exception "+a$
EndTry

描述: 定义类 BASIC 样式数据

示例:

' defdata.bmx

ReadData name$
ReadData age,skill

Print "name="+name+" age="+age+" skill="+skill

DefData "Simon",37,5000

描述: 读取经典 BASIC 样式数据

RestoreData

[编辑 | 编辑源代码]

描述: 恢复经典 BASIC 样式数据

示例:

' restoredata.bmx

For i=1 To 5
	RestoreData mydata	'reset the data pointer everly loop so we don't read past the end
	ReadData name$,age,skill
	Print "name="+name+" age="+age+" skill="+skill
Next

#mydata	'program label that can be used with the RestoreData command

DefData "Simon",37,5000

描述: 条件“And”二元运算符

示例:

Rem
And is a boolean operator that performs the AND function.
End Rem

For i=1 To 10
	If i>3 And i<6 Print "!" Else Print i
Next

描述: 条件“Or”二元运算符

示例:

Rem
Or is a boolean operator that performs the OR function.
End Rem

For i=1 To 5
	If i=2 Or i=4 Print "!" Else Print i
Next

描述: 条件“Not”二元运算符

示例:

Rem
Not is a boolean unary operator that performs the NOT function.
End Rem

Print Not 0		'prints 1 (TRUE)
Print Not 20		'prints 0 (FALSE)

描述: 位“左移”二元运算符

示例:

Rem
Shl is a binary operator that performs the shift to left function.
End Rem

b=1
For i=1 To 32
	Print Bin(b)
	b=b Shl 1
Next

描述: 位“右移”二元运算符

示例:

Rem
Shr is a binary operator that performs the shift to right function.
End Rem

b=-1
For i=1 To 32
	Print Bin(b)
	b=b Shr 1
Next

描述: 位“算术右移”二元运算符

示例:

Rem
Sar is a binary operator that performs the arithmetic shift to right function.
End Rem

b=$f0f0f0f0
For i=1 To 32
	Print Bin(b)
	b=b Sar 1
Next

描述: 字符串中字符数量或数组中元素数量

注释: 您也可以使用数组或字符串的 length 字段,因为它们是对象。

示例:

Rem
Len is a BlitzMax operator that returns the number of elements in a container Type.
End Rem

a$="BlitzMax Rocks"
Print Len a$	'prints 14

Local b[]
Print Len b		'prints 0

b=New Int[20]
Print Len b		'prints 20

描述: 数值“绝对值”一元运算符

示例:

Rem
Abs is a mathematical operator that performs the Absolute function.
End Rem

For f#=-1 To 1 Step 0.125
	Print "Abs "+f+"="+Abs f
Next 

描述: 数值“模数”或“余数”二元运算符

示例:

Rem
Mod is a mathematical operator that performs the Modulo function.
End Rem

For i=6 to -6 Step -1
	Print i+" Mod 3="+(i Mod 3)
Next 

描述: 数值“符号”一元运算符

示例:

Rem
Sgn is a mathematical operator that returns the sign of a value.
End Rem

Print Sgn 50	'1
Print Sgn 0	'0
Print Sgn -50	'-1

描述: 数值“最小值”内置函数

返回: 两个数值参数中较小的那个

描述: 数值“最大值”内置函数

返回: 两个数值参数中较大的那个

描述: 查找变量的地址

示例:

Rem
Varptr returns the address of a variable in system memory.
End Rem

Local a:Int
Local p:Int Ptr

a=20
p=Varptr a
Print p[0]

描述: 变量、字符串、数组或对象占用的字节大小

示例:

Rem
SizeOf returns the number of bytes of system memory used to store the variable.
End Rem

Type MyType
	Field a,b,c
End Type

Local t:MyType
Print SizeOf t	'prints 12

Local f!
Print SizeOf f	'prints 8

Local i
Print SizeOf i	'prints 4

Local b:Byte
Print SizeOf b	'prints 1

a$="Hello World"
Print SizeOf a	'prints 22 (unicode characters take 2 bytes each)

描述: 获取字符串第一个字符的字符值

示例:

Rem
Asc returns the unicode value of the first character of a string.
End Rem

Print Asc("A")	'65
Print "A"[0]	'65 - equivalent index style implementation

描述: 创建一个长度为 1 的字符串,其中包含字符代码

示例:

Rem
Chr returns a String of length 1 containing the unicode character of the value.
End Rem

Print Chr(65)	'A
华夏公益教科书