游戏开发论坛

 找回密码
 立即注册
搜索
12
返回列表 发新帖
楼主: OGRE新手

求世界坐标转屏幕坐标的方法

[复制链接]

0

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2011-6-22 07:53:00 | 显示全部楼层

Re:求世界坐标转屏幕坐标的方法

//转的哈,别人的按键精灵的代码


Dim Num(9)
Num(0) = "1000110001"
Num(1) = "0010000100"
Num(2) = "0000100010"
Num(3) = "0000100110"
Num(4) = "0101001010"
Num(5) = "1111010001"
Num(6) = "1000011110"
Num(7) = "0001000100"
Num(8) = "1000101110"
Num(9) = "1000101111"
Function 找数字(StartX, StartY)
    Dim x, y
    Dim iY, iX
    Dim temp
    temp = ""
    x = StartX : y = StartY + 9
    If GetPixelColor(x, y) = "000000" Then
        找数字 = - 1   //若是找到的是逗号,则返回-1
        Exit Function
    End If
    x = StartX : y = StartY + 5
    If GetPixelColor(x, y) = "000000" Then      
        If GetPixelColor(x + 1, y) = "000000" Then
            找数字 = - 3   //若是找到的是负号,则返回-3
            Exit Function
        End If        
    End If
   
    x = StartX : y = StartY + 3
    For iY = 0 To 1
        For iX = 0 To 4
            If GetPixelColor(x+iX, y+iY) = "000000" Then   
                temp = temp & "1"     
            Else
                temp = temp & "0"
            End If
        Next
    Next
    For iX = 0 To 9
        If temp = Num(iX) Then
            找数字 = iX //找到匹配的字符串,返回该数字
            Exit Function
        End If
    Next
    找数字 = -2  //找不到匹配的字符串,返回-2
End Function
Function 取得当前坐标()  
    Dim intX, intY
    Dim Flag //定义数字串结束标志
    Dim  result
    result = "" //定义返回值
    FindPic 0,0,1024,768,"Attachment:\未命名.bmp",1,intX,intY //通过找图法,确定数字串的起始坐标
   
    If intX > 0 And intY > 0 Then     
     intX = intX + 33 : intY = intY + 2  //第一个数字的偏移
    Else
     FindPic 0,0,1024,768,"Attachment:\未命名1.bmp",1,intX,intY //通过找图法,确定数字串的起始坐标
     If intX > 0 And intY > 0 Then
      intX = intX + 27 : intY = intY + 3  //第一个数字的偏移
     Else
      取得当前坐标 = result
      TracePrint "没有找到坐标串的起始图片,请检查附件中的截图是否正确"
      Exit Function  
     End If     
    End If
        
        Do  
            Flag = 找数字(intX, intY)
            If Flag > - 1  Then
                result = result & Cstr(Flag)
                intX = intX + 6 // > -1 表示找到了数字,下一个数字向右偏移6个点
            ElseIf Flag = - 3  Then
                result = result & "-"              
                intX = intX + 3 // -1表示找到了负号,下一个数字向右偏移3个点
            ElseIf Flag = - 1  Then
                result = result & ","
                intX = intX + 3 // -1表示找到了逗号,下一个数字向右偏移3个点
            End If         
        Loop While Flag <> -2 //前面定义了,若找数字的返回值为-2,表示没有找到匹配的数字
  
    取得当前坐标 = result
End Function
Sub 鼠标移动到目的坐标(x, y, K,T) //x,y为目的坐标,K为比例系数,T为精确移动的最小范围
    //此函数的思路是这样的:
    //     因为大地图上的鼠标坐标与屏幕坐标不是对应的,甚至不是绝对的正比例对应关系,但是总有一个近似的比例对应关系
    //     比如,屏幕上大约移动3个像素,大地图上的坐标变化1 则 K= 3/1
    //     首先,判断 当前坐标与目的坐标的x或y方向的差,若大于T,则采用比例移动,若小于等于T,则采用逐个屏幕坐标移动的方法
    Dim nowXYarray
    Dim runFlag
    Dim NowX,NowY
    Dim TracePrintStr
    runFlag = true
   
    While runFlag
        TracePrintStr = ""
        nowXYarray = split(取得当前坐标(), ",")
        If UBound(nowXYarray) >= 1 Then
            If (nowXYarray(0) <> "" and nowXYarray(1) <> "") Then
                nowX = CInt(nowXYarray(0)):nowY=CInt(nowXYarray(1))            
                If (x = nowX and y = nowY) Then
                    runFlag = false        
                Else
               
                    If abs(x - nowX) > T Then
                        MoveR Round((x-nowX)*K),0
                    Else
                        If x - nowX > 0 Then
                            MoveR 1, 0
                            TracePrintStr = TracePrintStr&">>X+1"
                        ElseIf x - nowX < 0 Then
                            MoveR - 1 , 0
                            TracePrintStr = TracePrintStr&">>X-1"
                        End If
                    End If
                    If abs(y - nowY) > T Then
                     MoveR 0,Round((y-nowY)*K)
                 Else
                     If y - nowY > 0 Then
                         MoveR 0, 1
                         TracePrintStr = TracePrintStr&">>Y+1"
                     Elseif y - CInt(nowY) < 0 Then
                         MoveR 0, - 1
                         TracePrintStr = TracePrintStr&">>Y-1"
                     End If                    
                 End If
                 TracePrint "X=" & Cstr(x - nowX) & ",Y=" & Cstr(y - nowY)&TracePrintStr
             End If                     
         End If
     End If
        
     LeftClick 1
     Delay 30
      
Wend
  
End Sub
/////////////////////////////////下面是脚本的主程序
Call 鼠标移动到目的坐标(50, 50, 2.5,3)//这里我的K取2.5,T取3,通过测试K取3时,函数执行更加迅速。
                                    //你测试的时候可以改变这两个系数,看看执行效果及效率。
                                    //当然,我这里采用的是的y轴方向是竖直向上的,你可以根据游戏具体方向编写合适的代码。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-8 09:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表