VB开发安卓程序_游戏3简单的乒乓球游戏

运行截图 

布局文件

源码来自:Android Example Simple Pong Game

Main代码如下:

#Region  Project Attributes 
	#ApplicationLabel: 2 Player Pong
	#VersionCode: 1
	#VersionName: 
	'SupportedOrientations possible values: unspecified, landscape or portrait.
	#SupportedOrientations: Landscape
	#CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
	#FullScreen: True
	#IncludeTitle: False
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

	Dim Main_Engine As Timer
	
End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.

	Private P1_Controls As Panel
	Private P2_Controls As Panel	
	Private P1 As Panel
	Private P2 As Panel	
	Private Ball As Panel
	
	Dim ball_speed = 10dip As Float
	Dim ball_direction_x = ball_speed As Float
	Dim ball_direction_y = ball_speed As Float
	
	Dim P1startX As Float
	Dim P1startY As Float	
	Dim P1deltaX As Float
	Dim P1deltaY As Float
	
	Dim P2startX As Float
	Dim P2startY As Float	
	Dim P2deltaX As Float
	Dim P2deltaY As Float
	
	Dim p1_score = 0 As Int
	Dim p2_score = 0 As Int
	
	Dim Pause = True As Boolean
		
	Private Player1_Score As Label
	Private Player2_Score As Label
		
End Sub

Sub Activity_Create(FirstTime As Boolean)
	'Do not forget to load the layout file created with the visual designer. For example:
	Activity.LoadLayout("Game")

	P1_Controls.Width = 50%x
	P1_Controls.Height = 100%y
	P1_Controls.Top = 0%y
	P1_Controls.Left = 0%x

	P2_Controls.Width = 50%x
	P2_Controls.Height = 100%y
	P2_Controls.Top = 0%y
	P2_Controls.Left = 50%x
	
	P1.Width = 2%x
	P1.Height = 24%y
	P1.Left = 10%x
	P1.Top = 50%y - (P1.Height / 2)
	
	P2.Width = P1.Width
	P2.Height = P1.Height
	P2.Left = 90%x - P1.Width
	P2.Top = P1.Top
	
	Ball.Width = 4%x
	Ball.Height = Ball.Width
	Ball.Left = 50%x - (Ball.Width / 2)
	Ball.Top = 50%y - (Ball.Height / 2)
	
	Player1_Score.Width = 30%x
	Player1_Score.Height = 20%y	
	Player1_Score.Left = 25%x - (Player1_Score.Width / 2)
	Player1_Score.Top = 50%y - (Player1_Score.Height / 2)

	Player2_Score.Width = 30%x
	Player2_Score.Height = 20%y	
	Player2_Score.Left = 75%x - (Player2_Score.Width / 2)
	Player2_Score.Top = 50%y - (Player2_Score.Height / 2)

	P1_Controls.BringToFront
	P2_Controls.BringToFront

	Main_Engine.Initialize("Main_Engine", 16)
	Main_Engine.Enabled = True

End Sub

Sub Main_Engine_Tick

	Player1_Score.Text = p1_score
	Player2_Score.Text = p2_score
		
		'Colision detector for Player 1
			If Ball.Left < P1.Left + P1.Width _
			And (Ball.Top + Ball.Height < P1.Top Or Ball.top > P1.Top + P1.Height) Then
				Ball.Left = 50%x - (Ball.Width / 2)
				Ball.Top = 50%y - (Ball.Height / 2)
				p2_score = p2_score + 1
				Pause = True
			End If		
		
		'Colision detector for Player 2
			If Ball.Left + Ball.Width > P2.Left _
			And (Ball.Top + Ball.Height < P2.Top Or Ball.top > P2.Top + P2.Height) Then
				Ball.Left = 50%x - (Ball.Width / 2)
				Ball.Top = 50%y - (Ball.Height / 2)
				p1_score = p1_score + 1
				Pause = True
			End If		
		
		'Colision detector for ball against the screen boundaries
			If (Ball.Left + Ball.Width) > P2.Left Then ball_direction_x = -ball_direction_x
			If Ball.Left < (P1.Left  + P1.Width) Then ball_direction_x = -ball_direction_x
			If (Ball.Top + Ball.Height) > 100%y Then ball_direction_y = -ball_direction_y
			If Ball.Top < 0%y Then ball_direction_y = -ball_direction_y
		
		If Pause = False Then
			Ball.Left = Ball.Left + ball_direction_x
			Ball.Top = Ball.Top + ball_direction_y
		End If
		
End Sub


Sub P1_Controls_Touch (Action As Int, X As Float, Y As Float)
		Select Action
			Case Activity.ACTION_DOWN			
				P1startX = X
				P1startY = Y
				
			Case Activity.ACTION_MOVE
				Pause = False
				P1deltaX = X - P1startX
				P1deltaY = Y - P1startY
				P1startX = X
				P1startY = Y
				
				If (P1.Top + P1deltaY) > 0%y And (P1.Top + P1.Height + P1deltaY) < 100%y Then
					P1.Top = P1.Top + P1deltaY * 1.5
				End If			
				
		End Select		
End Sub


Sub P2_Controls_Touch (Action As Int, X As Float, Y As Float)
		Select Action
			Case Activity.ACTION_DOWN			
				P2startX = X
				P2startY = Y
				
			Case Activity.ACTION_MOVE
				Pause = False
				P2deltaX = X - P2startX
				P2deltaY = Y - P2startY
				P2startX = X
				P2startY = Y
				
				If (P2.Top + P2deltaY) > 0%y And (P2.Top + P2.Height + P2deltaY) < 100%y Then
					P2.Top = P2.Top + P2deltaY * 1.5
				End If			
				
		End Select		
End Sub

Sub Activity_Resume
	Pause = True
	Main_Engine.Enabled = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
	Pause = True
	Main_Engine.Enabled = False
End Sub


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值