函数gg.DrawFrame(x,y,w,h)可以绘制矩形,其中(x,y)为矩形左上角的(x,y)坐标,(w,h)为矩形的宽度和高度,单位全部是像素。在3-2的基础上添加以下代码:
Dim As Single rect_left_x, rect_top_y, rect_width, rect_height ' 方块障碍物的相关参数
rect_height = 100 ' 方块高度
rect_width = 20 ' 方块宽度
rect_left_x = ww * 3 / 4 ' 方块左边x坐标
rect_top_y = hh - rect_height '方块顶部y坐标
'画方块
gg.DrawFrame(rect_left_x, rect_top_y, rect_width, rect_height)
进一步,添加变量rect_vx记录方块在x方向上的速度,并初始化为-3。在ddo语句中,让方块从右向左移动。当方块到达窗口最左边时,再让其从最右边出现。
完整代码
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方向速度
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 '如果方块跑到最左边
rect_left_x = ww ' 在最右边重新出现
End If
gg.Brush(BGR(204,0,0)) '方块的颜色
gg.DrawFrame(rect_left_x, rect_top_y, rect_width, rect_height) '画方块
gg.Redraw '刷新显示画面
Sleep(10) '暂停10毫秒
gg.Cls() '清空画面
Loop
End Sub
评论一下?