VB开发安卓程序_游戏2重力感应游戏

运行截图 


布局文件设计

 源码来自:Android Tutorial Game Physics: Gravity 

Main窗体代码如下:

#Region  Project Attributes 
	#ApplicationLabel: 重力游戏
	#VersionCode: 1
	#VersionName: 
	
	'SupportedOrientations possible values: unspecified, landscape or portrait.
	#SupportedOrientations: Landscape 
	
	'可以安装外部存储
	#CanInstallToExternalStorage: False 
	
	'SupportedOrientations possible values: unspecified, landscape or portrait.
	#SupportedOrientations: unspecified
	'《第5章屏幕大小与分辨率》
	
	'屏幕方向
	'可以定义三种不同的屏幕方向:只有纵向、只有横向、纵横向
	'屏幕方向可以以下两个地方定义:
	'项目顶部的 Project Attributes区域
	'#SupportedOrientations: unspecified (表示纵横向自动适应)
	'#SupportedOrientations: portrait (纵向)
	'#SupportedOrientations: landscape (横向)
	
	'使用 Phone 库
	'Phone1.SetScreenOrientation(0)'横向
	'Phone1.SetScreenOrientation(1)'纵向
	'Phone1.SetScreenOrientation(-1)'纵横向

	'结束:屏幕方向 ---------------------------------------------

#End Region


#Region  Activity Attributes 

	'全屏
	#FullScreen: True
	
	'是否需要标题栏
	#IncludeTitle: False
	
#End Region


Sub Process_Globals

	'创建一个自定义类型
	'Angle 角度 ;Velocity 速率 ; 
	'Grounded 触地 ;Mass 块;
	'Elasticity 弹性; Collision 碰撞;
	Type Physical_Object(X As Float, Y As Float, Old_X As Float, Old_Y As Float, _
						 Width As Float, Height As Float, Angle As Float, Velocity As Float, _
						 Velocity_X As Float, Velocity_Y As Float, Jump_Velocity As Float, _
						 Grounded As Boolean, Mass As Float, Elasticity As Float, Collision As Boolean)
	
	'Timer 必须在Process_Global 里声明
	Dim Main_Cycle As Timer

End Sub


Sub Globals
		
	'Game Engine  引擎
		Dim Target_FPS = 60 As Int
		Dim Delta_Time = 1 / Target_FPS As Float
		Dim Frame_Timestamp As Long

	'World
		Dim World_Gravity As Float
		
	'Output Text 输出
		Dim     Score      As Int
		Dim     Score_Done As Boolean
		Private Output     As Label

	'Objects 对象
		Private pnlGround   As Panel
		Private pnlObstacle As Panel
		Private pnlPlayer   As Panel
		Private pnlTouch As Panel
	
	'Object Physical Properties 对象的物理特性
		Dim Ground    As Physical_Object
		Dim Obstacle  As Physical_Object
		Dim Player    As Physical_Object		
	
End Sub


Sub Activity_Create(FirstTime As Boolean)
	
	'Load Layout
		Activity.LoadLayout("Layout1")
		
	'Setup Output Text
		Output.Left         =   0%x
		Output.Top          =   0%y
		Output.Width        = 100%x
		Output.Height       = 100%y
		
	'Setup User Input 设置用户输入
		pnlTouch.Left    	=   0%x
		pnlTouch.Top     	=   0%y
		pnlTouch.Width   	= 100%x
		pnlTouch.Height  	= 100%y
		
	'Setup Initial Game Conditions 游戏设置初始条件 
		Setup_Initial_Conditions

	'Apply everything above to our panels
		Obj2Pnl
		
	'Make sure the Touch Panel is the front most one		
		pnlTouch.BringToFront
		
	'Startup the Main Cycle
		Main_Cycle.Initialize("Main_Cycle", Delta_Time * 1000)
		Main_Cycle.Enabled = True

End Sub

Sub Setup_Initial_Conditions

	'Setup Object dimensions and location on screen
		Ground.Width    	 = 100%x
		Ground.Height   	 = 60%y
		Ground.X        	 = 0%x
		Ground.Y        	 = 80%y
	 	
		Obstacle.Width  	 = 1%x * Rnd(6, 26)
		Obstacle.Height 	 = Round(Obstacle.Width * (Rnd(100, 151) / 100))
		Obstacle.X      	 = 100%x
		Obstacle.Y      	 = Ground.Y - Obstacle.Height
		Obstacle.Velocity_X  = -60%x
		
		Player.Width    	 = 4%x
		Player.Height   	 = Player.Width
		Player.X        	 = 50%x - (Player.Width / 2)
		Player.Y   		     = Ground.Y - Player.Height
		Player.Jump_Velocity = 200%x
		
	'Setup World Properties
		World_Gravity = 800%y		

End Sub


Sub Activity_Resume
End Sub


Sub Activity_Pause (UserClosed As Boolean)
End Sub


Sub Main_Cycle_Tick	
	Backup_Object_Positions
	Get_Delta_Time
	Run_Run_Run
	Apply_Physics
	Collision_Detector
	Score_Control
	Obstacle_Respawn_Control
	Obj2Pnl
	Display_Score	
End Sub


Sub Backup_Object_Positions
	Player.Old_X   = Player.X
	Player.Old_Y   = Player.Y	
	Obstacle.Old_X = Obstacle.X
	Obstacle.Old_Y = Obstacle.Y
End Sub


Sub Get_Delta_Time
	If Frame_Timestamp <> 0 Then Delta_Time = (DateTime.Now - Frame_Timestamp) / 1000
	Frame_Timestamp = DateTime.Now
End Sub


Sub Run_Run_Run
	Obstacle.X = Obstacle.X + (Obstacle.Velocity_X * Delta_Time)	
	If Not(Game_Over) Then Obstacle.Velocity_X = Max(Obstacle.Velocity_X - (2%x * Delta_Time), -250%x)
End Sub


Sub Apply_Physics
	Player.Velocity_Y   = Player.Velocity_Y   + (World_Gravity       * Delta_Time)
	Player.Y            = Player.Y            + (Player.Velocity_Y   * Delta_Time)		
	Obstacle.Velocity_Y = Obstacle.Velocity_Y + (World_Gravity       * Delta_Time)
	Obstacle.Y          = Obstacle.Y          + (Obstacle.Velocity_Y * Delta_Time)		
End Sub


Sub Collision_Detector

	'Detect Ground Collisions	
		If Player.Y + Player.Height >= Ground.Y Then Player.Y = Ground.Y - Player.Height		
		If Obstacle.Y + Obstacle.Height >= Ground.Y Then Obstacle.Y = Ground.Y - Obstacle.Height
		
	'Detect Player / Obstacle Collisions
		Dim which_side                             As String	
		Dim top    = Obstacle.Y                    As Float
		Dim bottom = Obstacle.Y + Obstacle.Height  As Float
		Dim left   = Obstacle.X                    As Float
		Dim right  = Obstacle.X + Obstacle.Width   As Float	
	
		If  (Player.X + Player.Width)  >= Obstacle.X And Player.X < (Obstacle.X + Obstacle.Width ) _
		And (Player.Y + Player.Height) >= Obstacle.Y And Player.Y < (Obstacle.Y + Obstacle.Height) Then
		
				If Player.Collision = False Then
					If Player.Velocity_Y < 0 Then Player.Velocity_Y = 0
					Player.Collision = True
				End If
			
				If Player.Old_x                 < left   And Player.X + Player.Width  > left   Then which_side = "LEFT"
				If Player.Old_x + Player.Width  > right  And Player.X                 < right  Then which_side = "RIGHT"
				If Player.Old_y                 < top    And Player.Y + Player.Height > top    Then which_side = "TOP"
				If Player.Old_y + Player.Height > bottom And Player.Y                 < bottom Then which_side = "BOTTOM"
				
				If which_side = "TOP" Then
		            Player.Grounded = True
		            Player.Y = top - Player.Height
		        Else If which_side = "BOTTOM" Then
		            Player.Grounded = False
		            Player.Y = bottom
		        Else If which_side = "LEFT" Then
		            Player.Grounded = False
		            Player.X = left - Player.Width
		        Else If which_side = "RIGHT" Then
		            Player.Grounded = False
		            Player.X = right
				Else
					Player.Grounded = False
		        End If
		Else
			Player.Grounded = False				
		End If
End Sub


Sub Player_Jump(Jump_Velocity_Y As Float)
	Player.Velocity_y = Jump_Velocity_Y
End Sub


Sub Player_On_The_Ground As Boolean
	If (Player.Y + Player.Height >= Ground.Y) Or Player.Grounded Then
		Return True
	Else
		Return False
	End If
End Sub


Sub Display_Score
	If Not(Game_Over) Then 
		Output.Text = CRLF & "SCORE: " & Score
	Else
		Output.Text = CRLF & "GAME OVER" & CRLF & "SCORE: " & Score
	End If
End Sub


Sub Score_Control
	If Not(Game_Over) And Player.X > Obstacle.X And Score_Done = False Then 
		Score = Score + 1
		Score_Done = True
	End If
End Sub


Sub Game_Over As Boolean	
	If Player.X + Player.Width < 0%x Then
		Return True
	Else
		Return False
	End If
End Sub


Sub Obstacle_Respawn_Control
	If Obstacle.X + Obstacle.Width < 0%x Then		
		Obstacle.Width  =  1%x * Rnd(6, 26)
		Obstacle.Height = Round(Obstacle.Width * (Rnd(100, 151) / 100))
		Obstacle.X = 100%x
		Obstacle.Y = Ground.Y - Obstacle.Height
		Score_Done = False
		Player.Collision = False
	End If
End Sub


Sub Obj2Pnl
	pnlGround.Width    = Ground.Width
	pnlGround.Height   = Ground.Height
	pnlGround.Left     = Ground.X
	pnlGround.Top      = Ground.Y
		
	pnlObstacle.Width  = Obstacle.Width
	pnlObstacle.Height = Obstacle.Height
	pnlObstacle.Left   = Obstacle.X
	pnlObstacle.Top    = Obstacle.Y
	
	pnlPlayer.Width    = Player.Width
	pnlPlayer.Height   = Player.Height
	pnlPlayer.Left     = Player.X
	pnlPlayer.Top      = Player.Y
End Sub


Sub pnlTouch_Touch (Action As Int, X As Float, Y As Float)
	If Not(Game_Over) Then
		Select Action
			Case Activity.ACTION_DOWN		
				If Player_On_The_Ground Then 
					Player.Grounded = False
					Player_Jump(-Player.Jump_Velocity)
				End If
				
			Case Activity.ACTION_MOVE
			
			Case Activity.ACTION_UP
				If Player.Velocity_Y < (-Player.Jump_Velocity * 0.33) Then 
					Player_Jump(-Player.Jump_Velocity * 0.33)
				End If
				
		End Select
	Else
		Score = 0
		Setup_Initial_Conditions		
	End If
End Sub

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值