定义整型变量记录游戏的得分,并初始化为0:
Dim score As Long = 0 '得分
当方块跑到画面最左边时,得分增加1:
If (rect_left_x <= -20) Then '如果方块跑到最左边
score = score + 1 ' 得分+1
当方块碰到小球时,得分清零:
'如果小球碰到方块
If ((rect_left_x <= ball_x + radius) And _
(rect_left_x + rect_width >= ball_x ) And _
(hh - rect_height <= ball_y + radius)) Then
score = 0 ' 得分清零
Sleep(200) ' 慢动作效果
Else
Sleep(10) '暂停10毫秒
End If
另外,文字输出功能
gg.DrawString 10,10,score '显示得分
代码写在刷新显示之前
gg.Brush(BGR(204, 0, 0)) '方块的颜色
gg.DrawFrame(rect_left_x, rect_top_y, rect_width, rect_height) '画方块
gg.DrawString 10,10,score '显示得分
gg.Redraw '刷新显示画面
也可以先设置字体和大小
gg.Font "黑体",14
gg.DrawString 10,10,score '显示得分
最后执行效果:

完整代码:
Sub 游戏执行过程(hWndForm As hWnd)
Dim gg As yGDI = hWndForm
Dim As Single ww, hh, gravity '重力加速度
Dim As Single ball_x, ball_y, ball_vy, radius '小球坐标、y方向速度、直径
ww = DpiUnScaleF(Form1.ScaleWidth) '游戏画面宽度
hh = DpiUnScaleF(Form1.ScaleHeight) '游戏画面高度
radius = 20 '小球直径
ball_x = ww / 4 '小球x位置
ball_y = hh - radius '小球y位置
ball_vy = 0 ' 小球初始y速度为0
Dim As Single rect_left_x, rect_top_y, rect_width, rect_height, rect_vx ' 方块障碍物的相关参数
rect_height = 100 ' 方块高度
rect_width = 20 ' 方块宽度
rect_left_x = ww * 3 / 4 ' 方块左边x坐标
rect_top_y = hh - rect_height '方块顶部y坐标
rect_vx = -3 '方块x方向速度
Dim score As Long = 0 '得分
Do
If IsKeyPress(VK_SPACE) Then '按下了空格键"
ball_vy = -16 ' 给小球一个向上的初速度
End If
ball_vy = ball_vy + gravity ' 根据重力加速度更新小球y方向速度
ball_y = ball_y + ball_vy ' 根据小球y方向速度更新其y坐标
If (ball_y >= hh - radius) Then '如果小球落到地面上
ball_vy = 0 'y速度为0
ball_y = hh - radius ' 规范其y坐标,避免落到地面下
End If
If ball_y < 0 Then '小球跳起到顶部,让小球掉下来
ball_y = 0
ball_vy = 2
gravity = 0.5
End If
gg.Brush(&HFF0000) '球的颜色
gg.DrawEllipse(100, ball_y, radius, radius) '画小球
rect_left_x = rect_left_x + rect_vx ' 方块向左移
If (rect_left_x <= -20) Then '如果方块跑到最左边
score = score + 1 ' 得分+1
rect_left_x = ww ' 在最右边重新出现
Randomize
rect_height = Int(Rnd * (hh / 2 - hh / 4)) + hh / 2
Randomize
rect_vx = - (Int(Rnd * 4) + 3) ' 3 到 7 之间
rect_top_y = hh - rect_height '方块顶部y坐标
End If
gg.Brush(BGR(204, 0, 0)) '方块的颜色
gg.DrawFrame(rect_left_x, rect_top_y, rect_width, rect_height) '画方块
gg.Font "黑体",14
gg.DrawString 10,10,score '显示得分
gg.Redraw '刷新显示画面
'如果小球碰到方块
If ((rect_left_x <= ball_x + radius) And _
(rect_left_x + rect_width >= ball_x ) And _
(hh - rect_height <= ball_y + radius)) Then
score = 0 ' 得分清零
Sleep(200) ' 慢动作效果
Else
Sleep(10) '暂停10毫秒
End If
gg.Cls() '清空画面
Loop
End Sub
评论一下?