注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

[例程]简单的乒乓球游戏  

2015-08-19 14:39:17|  分类: 游戏开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

[例程]简单的乒乓球游戏 - 沉默蜂 - 沉默蜂B4A安卓编程实战资料
效果图


#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", 100)
' 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


 
  评论这张
 
阅读(73)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017