vb做的貪吃蛇

2022-03-11 20:40:03 字數 2717 閱讀 1268

dim q as integer: dim w as integer

dim sq as integer: dim sw as integer

dim cq as integer: dim cw as integer

dim ss as long

dim c as integer

dim a as integer

dim t as integer

dim sh(350) as integer   '為了偷懶而都弄成了全域性變數

private sub form_load()

randomize  '和隨機函式配套的語句,為了重新整理第一次的隨機數,不然可能每次開始都是同乙個數字

form1.backcolor = rgb(999, 999, 999)   '把背景顏色弄成白色

t = 3  '表示蛇頭此時的方向為3(不是這個位置上的方向變數),也就是朝上,作為一直在進行的計時器用到的乙個引數

ks  '建立控制項陣列函式

sc   '隨機建立食物的函式

q = 7: w = 9  '初始化剛開始時候蛇的位置,總共有三段,這個是蛇頭

sq = 9: sw = 9  '這個是蛇尾

sh(20 * sq + sw) = 3   '設定蛇尾的方向變數為3,也就是朝上

sh(169) = 3  '設定蛇身的方向變數為3,朝上

end sub

public sub sc()   '隨機建立乙個食物的座標

cq = int((14) * rnd + 1): cw = int((19) * rnd + 1)

while sh(20 * cq + cw) > 0   '當食物建立在了蛇的身上時重新建立,sh表示蛇經過的方向,用1,2,3,4表示,所以它不為0時就表示這個位置有蛇,蛇正在經過

cq = int((14) * rnd + 1): cw = int((20) * rnd + 1)

wend   'while語句的結束語句

c = 1   '這個是表示沒有吃到食物,下面會說到

end sub

private sub picture1_keydown(index as integer, keycode as integer, shift as integer)  '即時的更改變數t,來更改蛇頭的方向

if keycode = 37 and t <> 2 and a = 0 then

t = 1

a = 1  '設定這個變數的原因是保證在計時器中迴圈過一次操作後才能再次改變蛇頭的方向,不然會出現蛇頭還沒移動就此處亂轉的囧境

elseif keycode = 39 and t <> 1 and a = 0 then

t = 2

a = 1

elseif keycode = 38 and t <> 4 and a = 0 then

t = 3

a = 1

elseif keycode = 40 and t <> 3 and a = 0 then

t = 4

a = 1

elseif keycode = 83 then  '如果按下『a』就加速

timer1.interval = timer1.interval + 50

elseif keycode = 65 then  '按下『s』 就減速

if timer1.interval <> 50 then  '如果速度等於50了就不能再減了呵呵,速度的上限出於vb的原因倒是有上限的

timer1.interval = timer1.interval - 50

end if

end if

end sub

private sub timer1_timer()

if t = 1 then   '根據變數t來判斷蛇頭的方向

if c = 0 then  '如果吃到的話,就重新初始化食物,並增加分數

scss = ss + 100000 / timer1.interval * 1.33  '加分,公示裡面加入了計時器的引數,所以蛇走的越快加的分數越高

end if

label1.caption = "你的得分:" & ss   '重新整理分數

label2.caption = "速度up按a,速度down按s :" & int(200000 / timer1.interval)   '顯示速度

end sub

public sub hh()  '根據蛇尾的方向變數設定下乙個蛇尾的位置,然後把之前的方向變數清零

if sh(20 * sq + sw) = 3 then

sh(20 * sq + sw) = 0

sq = sq - 1

elseif sh(20 * sq + sw) = 4 then

sh(20 * sq + sw) = 0

sq = sq + 1

elseif sh(20 * sq + sw) = 1 then

sh(20 * sq + sw) = 0

sw = sw - 1

elseif sh(20 * sq + sw) = 2 then

sh(20 * sq + sw) = 0

sw = sw + 1

end if

end sub

大一時候學了vb這門課程,突然想做個玩玩,弄了一下午,過程挺痛苦的,畢竟才大一,**寫的也很臃腫吧,不過那時出來了還是很有成就感的

最近學弟在學vb,就把**注釋了一下,方便閱讀,順便發到這裡

vb做的貪吃蛇

dim q as integer dim w as integer dim sq as integer dim sw as integer dim cq as integer dim cw as integer dim ss as long dim c as integer dim a as int...

如何入手做貪吃蛇?

對於新手而言,似乎是令人難以忍受的事情。但實際上,隨著難題乙個個解決,不論是誰,都會由衷地感到激動。先從乙個經典的貪吃蛇遊戲開始。我們做的是貪吃蛇的極為簡化的版本,使用命令列的 按一次走一次的那種。首先,我們要用自頂向下 逐步求精的思想來逐步構建框架。構建框架的最好的方式,當然是通過標頭檔案了。我們...

c語言做貪吃蛇遊戲

閒話不多說,直接上 include stdio.h include windows.h include stdbool.h 座標系 y x 按鍵 移動 8 4 5 6 9 暫停 define x 20 define y 70 define wait time 700 移動一次需要的時間 enum 對...