VisualFreeBasic游戏趣味编程_8.3_被鼠标点击后旋转

2026-1-22 / 0 评论 / 39 阅读

鼠标点击位置的坐标为(m.x,m.y),根据图8-3中5行5列小圆圈的坐标设置(代码8-1-2.cpp),被鼠标点中的小圆圈在二维数组rounds中的行、列序号为:

     Dim clicked_i As Long  = Int(ps.y -45)/45 
     Dim clicked_j As Long  = int(ps.x -45)/45 

被点中的小圆圈需要顺时针旋转90度,如图8-6所示。

只需要将其angleNum值依次+90即可:

      Rounds(i, j).angleNum += 90
      If Rounds(i, j).angleNum >= 360 Then Rounds(i, j).angleNum = 0

其中“+=”为复合运算符,x += 1等价于x = x ++ 1。

练习题8-2:写出以下程序并运行结果。

   Dim a As Long = 1
   a += 4
   Print a
   a -= 2
   Print a 
   a *= 3
   Print a 
   a /= 2
   Print a 
   a Mod= 3
   Print a   

把小圆圈顺时针旋转的功能封装在函数rotateRound( )中,修改代码如下:

Sub updateWithInput(hWndForm As hWnd) ' 和输入有关的更新
   Static pp As Long '预防鼠标一直按住,造成一直点击
   If IsKeyPress(VK_LBUTTON) Then '鼠标左键点击
      If pp = 0 Then
         pp = 1 '表示鼠标按下
         Dim ps As Point
         GetCursorPos(@ps) '获取鼠标在屏幕的位置
         MapWindowPoints HWND_DESKTOP, hWndForm, @ps, 1 '将鼠标位置从屏幕转换到游戏窗口的位置
         ps.x = DpiUnScaleI(ps.x) '响应系统DPI
         ps.y = DpiUnScaleI(ps.y)
         Dim clicked_i As Long = Int(ps.y -45) / 45
         Dim clicked_j As Long = int(ps.x -45) / 45
         rotateRound(clicked_i, clicked_j) '把当前圆圈顺时针旋转90度
      End If
   Else
      pp = 0 '表示鼠标放开
   End If

End Sub

评论一下?

OωO
取消