用ahk来写个贪吃蛇吧

SetBatchLines -1

;新建一个对象,参数为盘子大小
snake := new Snake(30)
;设置食物的颜色和图标
snake.style.food := {"color":"red", "ico":"?"}
;设置蛇头的颜色和图标,列表中分别为左、上、下、右的蛇头图标
snake.style.header := {"color":"green", "ico":["左","上","下","右"]}
;设置蛇运动的速度,如350毫秒动一次
this.speed := 350
snake.start()
return

Class Snake
{
    __New(size:=20){
        this.size := size
        this.timer := ObjBindMethod(this, "move")
        this.style := {}
        this.ways := []
        this.style.food := {"color":"red", "ico":"█"}
        this.style.body := {"color":"blue", "ico":"█"}
        this.style.header := {"color":"green", "ico":["█","█","█","█"]}
        this.style.back := {"color":"BBBBBB", "ico":"█"}
        this.speed := 350
        this.create(size)
        return this
    }

    create(size){
        static len
        backcolor := this.style.back.color
        Gui, +Hwndhwnd
        Gui, Margin, 0, 0
        Gui, Font, S15 c%backcolor%
        loop % size {
            Gui, Add, Text, y+ xm, % this.style.back.ico
            loop % size - 1
            Gui, Add, Text, x+, % this.style.back.ico
        }
        Gui, show
        this.guiID := hwnd
        return
    }

    start(){
        timer := this.timer
        Random, x, 2, 5
        Random, y, 2, 5
        this.body := [[x, y]]
        this.way := [0, 1]
        this.direction := 4
        this.blindkeys()
        this.putfood()
        SetTimer % timer, % this.speed
    }

    move(){
        head := this.body[1]
        if(way_dir := this.ways.RemoveAt(1))
            if(this.body.length() = 1 || (way_dir[2] + this.direction != 5))
                this.way := way_dir[1], this.direction := way_dir[2]
            newhead := [head[1] + this.way[1], head[2] + this.way[2]]
            this.body.InsertAt(1, newhead)
            if(this.isequl(this.food, newhead))
                this.putfood()
            else
                removed := this.body.Pop()
            if(this.islose(newhead)){
                this.end()
            }else{
                this.setstyle(newhead, this.style.header.color, this.style.header.ico[this.direction])
                this.setstyle(removed, this.style.back.color, this.style.back.ico)
                this.setstyle(this.body[2], this.style.body.color, this.style.body.ico)
            }
        }

    blindkeys(){
        Hotkey, IfWinActive, % "ahk_id " . this.guiID
        fn := this.changeway.bind(this)
        for _, key in ["UP", "DOWN", "LEFT", "RIGHT"]
            Hotkey, % key, % fn
    }

    changeway(){
        static dic := {"UP":[[-1,0], 2], "DOWN":[[1,0], 3], "LEFT":[[0,-1], 1], "RIGHT":[[0,1], 4]}
        if(this.direction != dic[A_ThisHotkey][2])
            this.ways.push(dic[A_ThisHotkey])
    }

    putfood(){
        Random, x, 1, % this.size
        Random, y, 1, % this.size
        this.food := [x,y]
        if(this.isin(this.food, this.body))
            this.putfood()
        else
            this.setstyle(this.food, this.style.food.color, this.style.food.ico)
    }

    setstyle(pos, color, icon := "█"){
        Gui, Font, c%color%
        order := (pos[1] -1)* this.size + pos[2]
        GuiControl, Font, Static%order%
        GuiControl, , Static%order%, %icon%
        return
    }

    islose(head){
        if(head[1] > this.size || head[2] > this.size || head[1] < 1 || head[2] < 1)
            return True
        if(this.isin(head, this.body) && head != this.body[1])
            return True
    }

    end(){
        timer := this.timer
        SetTimer % timer, Off
        len := this.body.length()
        msgbox, 4, , 游戏结束,得分%len%分。是否继续
        IfMsgBox Yes
        {
            Gui, Destroy
            this.create(this.size)
            this.start()
        }
        else
            ExitApp
    }

    isequl(arg1, arg2){
        if(( arg1[1] = arg2[1] ) && ( arg1[2]=arg2[2] ))
            return True
    }

    isin(arg1, arg2){
        for _,v in arg2
            if(this.isequl(arg1, v))
                return True
    }

    isopposite(arg1, arg2){
        if((arg1[1] + arg2[1] = 0) && (arg1[2] + arg2[2] = 0))
            return True
    }
}

GuiClose:
ExitApp

给TA捐赠
共{{data.count}}人
人已捐赠
应用案例

字符串转Unicode及字符内存

2020-12-15 15:27:37

应用

星喆工具 -1.7

2020-12-24 13:50:37

12 条回复 A文章作者 M管理员
  1. 九离

  2. Tre

    ???

  3. xcgrehxc

    学习学习

  4. Deepsea

    打卡

  5. 。10536

    1

  6. ?10468

    1

  7. 宁静致远

    打卡,学习

  8. 双旋共舞

    学习。

  9. 冰诺诺☆

    学习学习

  10. Lychee

    学习学习

  11. 孤独求败

    厉害

  12. YUMI

    撞到自己不会死啊,这样没难度能改一下吗

个人中心
购物车
优惠劵
有新私信 私信列表
搜索