c 簡單貪吃蛇遊戲的思想

2021-06-06 20:00:20 字數 1229 閱讀 4747

就是用graphic畫出來的,用個timer控制,我已經封裝成乙個類了。

public class game

for (int i = 0; i < 4; i++)//初始化食物

}public void gamestart()

public void letgameover()

public int direction//給外部用

set

}/*most important*/

private void timer_e(object s, eventargs e)//間隔時間要做的事

}private void move()//移動函式

//下面還要-1所以寫40

this.snakel.addfirst(new point(font.x,font.y-1));

break;

}case 2:

this.snakel.addfirst(new point(font.x, font.y + 1));

break;

}case 3:

this.snakel.addfirst(new point(font.x - 1, font.y));

break;

}case 4:

this.snakel.addfirst(new point(font.x + 1, font.y));

break;

}default: return;

}if (this.foodjudge(font))//如果前方為食物

else//不是食物去掉蛇尾

}private void draw()//重繪

foreach(point p in foodl)

}private bool foodjudge(point p)//判斷前方是否是食物

return false;

}private void setfood()//設定食物,因為是demo且食物種類就乙個,食物重疊就不考慮,反正也沒影響

private void cutsnake(int n)//剪短射的身體的函式n是數量}}

注意類裡面的timer的t事件為乙個新執行緒,如果類內要改變窗體上的某些資源的話是互斥的,需要用委託

做複雜了的話也可以做成我這樣

C 簡單貪吃蛇遊戲

用c 實現的非常簡單的非圖形介面的簡單貪吃蛇遊戲 include include include include include includeusing namespace std void transport short short short void set short,short void...

貪吃蛇遊戲

貪吃蛇遊戲 結構化程式設計 c語言程式設計 重要的的是結構化的程式設計思想 include include include include include include define field width 300 就做20個格仔的 define field height 300 define f...

貪吃蛇遊戲

include include include include include 使用當前時間做種子 enum dir 列舉型別enum dir 圍牆 class fencef 定義物件 畫框框 void fence initfence 顯示框框 void fence outputf int snak...