第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

蛇游戲:蛇段的錯(cuò)誤定位

蛇游戲:蛇段的錯(cuò)誤定位

慕村225694 2021-10-06 11:03:50
我正在編寫蛇游戲,所以我制作了一個(gè)SnakeLogic代表蛇邏輯模型的類。實(shí)現(xiàn)如下:snake 由段組成,每個(gè)段保存它的起始位置、長(zhǎng)度和運(yùn)動(dòng)方向。這是Segment該類(的內(nèi)部類SnakeLogic)的完整代碼:protected class Segment{    public Point location;    public SnakeDirection dir;    public int length;    public Segment(Point l, SnakeDirection dir,int length){        location=l;        this.dir=dir;        this.length=length;    }}段用一個(gè)LinkedList:private LinkedList<Segment> nodes; 當(dāng)方向改變時(shí),新段被添加到 的開頭LinkedList:public void setDirection(SnakeDirection dir){    //gets location and direction of first segment    Point head = nodes.getFirst().location;    SnakeDirection currentDir = nodes.getFirst().dir;    //if direction isn't changed, return    if (currentDir == dir) return;    //ignores directions that are opposite to current one.    switch(currentDir){        case LEFT:            if (dir==SnakeDirection.RIGHT) return;            break;        case RIGHT:            if (dir==SnakeDirection.LEFT) return;            break;        case UP:            if (dir==SnakeDirection.DOWN) return;            break;        case DOWN:            if (dir==SnakeDirection.UP) return;            break;    }    //adds new segment with 0 length,current first segment's location     //and given direction    nodes.addFirst(new Segment(head,dir,0));}該方法Next()計(jì)算蛇的運(yùn)動(dòng)。根據(jù)運(yùn)動(dòng)方向,第一段的位置發(fā)生變化;如果蛇包含 1 個(gè)以上的段,則第一個(gè)段的長(zhǎng)度增加給定值 ( stepSize),最后一段的長(zhǎng)度減少此值。如果最后一段的長(zhǎng)度變?yōu)?<=0,則刪除最后一段(如果長(zhǎng)度小于零,則從當(dāng)前最后一段中減去余數(shù))。
查看完整描述

2 回答

?
BIG陽(yáng)

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

沒有 MCVE 就不可能知道您的問題出在哪里,但設(shè)計(jì)似乎過于復(fù)雜。不要使用段,而是使用點(diǎn)。


假設(shè)你的觀點(diǎn)看起來像


class Point {

    int x, y;

    // getters/setters if you want

}

然后蛇由一個(gè)點(diǎn)列表和一個(gè)方向表示:


class Snake {

    List<Point> body = new LinkedList<>();

    Point head; // easier to separate the head, but you can do with including it in the list

    Direction dir;

}

您可以添加next()方法來計(jì)算蛇的表示:


void next() {

    int temp1x = head.x;

    int temp1y = head.y;

    switch(dir) {

        case LEFT:

            head.x -= stepSize;

            break;

        //...

    }

    int temp2x, temp2y;

    for (Point point : points) {

        temp2x = point.x;

        temp2y = point.y;

        point.x = temp1x;

        point.y = temp1y;

        temp1x = temp2x;

        temp1y = temp2y;

    }

}

我會(huì)把它留給你來簡(jiǎn)化實(shí)現(xiàn)(如果你擴(kuò)展類以允許它,你可以使用Point而不是單獨(dú)的 x 和 y ints Point)。


筆記:


LinkedList 確實(shí)是列表實(shí)現(xiàn)的不錯(cuò)選擇。

方法名稱以小寫字母開頭(next而不是Next)。


查看完整回答
反對(duì) 回復(fù) 2021-10-06
?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

創(chuàng)建新段時(shí),您傳遞第一個(gè)段的位置對(duì)象,而不是該位置的 COPY。所以你所有的段對(duì)象共享非常相同的位置對(duì)象。如果您在新段內(nèi)修改它,它也會(huì)在所有其他段中修改,因?yàn)樗峭粋€(gè)對(duì)象。(當(dāng)你傳遞一個(gè)對(duì)象時(shí),你傳遞的是對(duì)象的引用,而不是對(duì)象的值。)所以而不是這一行:

Point head = nodes.getFirst().location;

用這個(gè):

Point head = new Point(nodes.getFirst().location);


查看完整回答
反對(duì) 回復(fù) 2021-10-06
  • 2 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)