C 貪吃蛇二

2021-06-21 11:32:47 字數 2677 閱讀 6462

小菜學習winform(一)貪吃蛇》

說到oo可能一說一大堆,這裡面小菜只是簡單的把貪吃蛇抽象出來,先來說蛇,具有的屬性和行為,屬性比如蛇的長度、蛇的寬度、蛇的行動方向等;行為比如是否吃到食物、是否撞牆等,那我們可以抽象乙個蛇的類,這樣實現:

1

using

system;

2using

system.collections.generic;

3using

system.linq;

4using

system.text;

5using

system.drawing;67

namespace

snakegame830

this.snkdrt = 2;//

初始方向31}

32///

33///

判斷是否撞到自己

34///

35///

36public

bool

checksnakeheadinsnakebody()

3740

///41

///檢查輸入的座標是否在蛇的身上

42///

43///

44///

45///

46///

47public

bool checkinsnakebody(int x, int y, int

snkhead)

4855}56

return

false;57

}58///59

///判斷是否撞牆

60///

61///

62public

bool

checksnakebodyinfrm()

6369

///70

///判斷是否吃到食物

71///

72///

73///

74public

bool

eatedfoot(point foodlct)

7584

return

true

; 85}

86else

87return

false

; 88}

89///

90///

設定point陣列座標

91///

92///

93public

void forward(int

drc)

94103

104switch

(drc)

105

123}

124}

125 }

抽象完蛇,那我們再說下貪吃蛇遊戲,貪吃蛇這個遊戲具有的元素有:蛇、食物、得分等,那我們這樣來表現:

1

using

system;

2using

system.collections.generic;

3using

system.linq;

4using

system.text;

5using

system.drawing;67

namespace

snakegame824

///25

///隨機顯示食物

26///

27public

void

showfood()

2838 foodlct.x =x;

39 foodlct.y =y;40}

41///

42///

分數43

///44

///45

public

bool

addscore()

4652

return

false;53

}54///55

///判斷遊戲是否結束

56///

57///

58public

bool

dead()

5962

}63 }

其實使用oo還有很多地方需要細化,這裡不是講oo,所以只是簡單的帶過下。

然後是得分模式,就是蛇吃到乙個食物就加10分,這個在addscore()方法中有實現:

1

///2

///分數

3///

4///

5public

bool

addscore()612

return

false

;13 }

1

if (this.game.score < 100)2

5else

if (this.game.score < 150)6

9else

10

最後是減少介面重繪這個問題,因為小菜不是搞遊戲開發的,那我只能減少介面的重繪數量,這邊我是這樣實現的,食物用label控制項,顯示用定位來體現,蛇的動作還是用gdi+來繪製。

主程式**:

執行截圖:

附錄:小菜學習程式設計-winform系列(初學者)

C 貪吃蛇設計

class map enum char map row col 地圖 map void drawmap 繪製地圖 bool isvaildpoint int x,int y 1.利用列舉來定義常量,在類外部可以通過作用域解析來獲取列舉常量,便於所有類的交流.2.通過二維陣列儲存地圖,在構造的時候讀取...

C語言貪吃蛇

include include include include include include define screen width 40 遊戲螢幕寬度 define screen length 15 遊戲螢幕長度 define start x 16 螢幕起始x座標 define start y ...

C語言 貪吃蛇

include include include define framewidght 22 define frameheight 22 short generaterandnumber short a,short b void posconsolecursor short x,short y voi...