跪求大俠幫忙寫vb的程式。謝謝了。題目如下。急在

時間 2021-06-12 20:03:10

1樓:況知慧

private sub command1_click()static a as boolean

if a = false then

label1.left = label1.left + 50else

label1.left = label1.left - 50end if

if label1.left + label1.width + 100 > me.width then

a = not a

elseif label1.left < 0 thena = not a

end if

end sub

private sub command2_click()timer1.enabled = not timer1.enabled

end sub

private sub form_load()timer1.enabled = falsetimer1.interval = 100label1.

autosize = truelabel1.caption = "歡迎使用vb"

command1.caption = "手動"

command2.caption = "自動"

end sub

private sub timer1_timer()command1_click

end sub

2樓:匿名使用者

接執行就行了

option explicit

private withevents timer1 as timer

private withevents label1 as label

dim gfangxiang as boolean

dim hwb as single

dim she() as shenti

dim x as long, y as long

dim zhuangtai(23, 23) as long

private type shenti

f as long

x as long

y as long

end type

private sub form_keydown(keycode as integer, shift as integer)

dim c as long

if keycode = 27 then end

if keycode = 32 then

if timer1.enabled = true then

timer1.enabled = false

label1.visible = true

else

timer1.enabled = true

label1.visible = false

end if

end if

c = ubound(she)

if gfangxiang = true then exit sub

select case keycode

case 37

if she(c).f = 2 then exit sub

she(c).f = 0

gfangxiang = true

case 38

if she(c).f = 3 then exit sub

she(c).f = 1

gfangxiang = true

case 39

if she(c).f = 0 then exit sub

she(c).f = 2

gfangxiang = true

case 40

if she(c).f = 1 then exit sub

she(c).f = 3

gfangxiang = true

end select

end sub

private sub form_load()

me.autoredraw = true

me.backcolor = &hc000&

me.fillcolor = 255

me.fillstyle = 0

me.windowstate = 2

set timer1 = controls.add("vb.timer", "timer1")

set label1 = controls.add("vb.label", "label1")

label1.autosize = true

label1.backstyle = 0

label1 = "暫停"

label1.forecolor = rgb(255, 255, 0)

label1.fontsize = 50

chushihua

end sub

private sub form_resize()

on error goto 1:

with me

if .windowstate <> 1 then

.cls

.scalemode = 3

hwb = .scaleheight / .scalewidth

.scalewidth = 24

.scaleheight = 24

label1.move (me.scalewidth - label1.width) / 2, (me.scaleheight - label1.height) / 2

huatu

me.line (x, y)-(x + 1, y + 1), rgb(255, 255, 0), bf

end if

end with

1:end sub

private sub timer1_timer()

dim c as long, i as long

on error goto 2:

qingchu

c = ubound(she)

select case she(c).f

case 0

if zhuangtai(she(c).x - 1, she(c).y) = 2 then

c = c + 1

redim preserve she(c)

she(c).f = she(c - 1).f

she(c).x = she(c - 1).x - 1

she(c).y = she(c - 1).y

chanshengshiwu

goto 1:

elseif zhuangtai(she(c).x - 1, she(c).y) = 1 then

goto 2:

end if

case 1

if zhuangtai(she(c).x, she(c).y - 1) = 2 then

c = c + 1

redim preserve she(c)

she(c).f = she(c - 1).f

she(c).x = she(c - 1).x

she(c).y = she(c - 1).y - 1

chanshengshiwu

goto 1:

elseif zhuangtai(she(c).x, she(c).y - 1) = 1 then

goto 2:

end if

case 2

if zhuangtai(she(c).x + 1, she(c).y) = 2 then

c = c + 1

redim preserve she(c)

she(c).f = she(c - 1).f

she(c).x = she(c - 1).x + 1

she(c).y = she(c - 1).y

chanshengshiwu

goto 1:

elseif zhuangtai(she(c).x + 1, she(c).y) = 1 then

goto 2:

end if

case 3

if zhuangtai(she(c).x, she(c).y + 1) = 2 then

c = c + 1

redim preserve she(c)

she(c).f = she(c - 1).f

she(c).x = she(c - 1).x

she(c).y = she(c - 1).y + 1

chanshengshiwu

goto 1:

elseif zhuangtai(she(c).x, she(c).y + 1) = 1 then

goto 2:

end if

end select

zhuangtai(she(0).x, she(0).y) = 0

for i = 0 to c

select case she(i).f

case 0

she(i).x = she(i).x - 1

case 1

she(i).y = she(i).y - 1

case 2

she(i).x = she(i).x + 1

case 3

she(i).y = she(i).y + 1

end select

next

tiaozheng

1:gfangxiang = false

zhuangtai(she(c).x, she(c).y) = 1

huatu

exit sub

2:if msgbox("遊戲結束,點“是”重新開始遊戲,點“否”", vbyesno, "貪吃蛇") = vbyes then

chushihua

else

endend if

end sub

private sub chushihua()

me.cls

timer1.enabled = true

timer1.interval = 200

erase zhuangtai

redim she(2)

she(0).f = 2

she(0).x = 9

she(0).y = 11

zhuangtai(9, 11) = 1

she(1).f = 2

she(1).x = 10

she(1).y = 11

zhuangtai(10, 11) = 1

she(2).f = 2

she(2).x = 11

she(2).y = 11

zhuangtai(11, 11) = 1

huatu

chanshengshiwu

end sub

private sub qingchu()

dim i as long

for i = 0 to ubound(she)

me.line (she(i).x, she(i).y)-(she(i).x + 1, she(i).y + 1), me.backcolor, bf

next

end sub

private sub huatu()

dim i as long

for i = 0 to ubound(she)

me.circle (she(i).x + 0.5, she(i).y + 0.5), 0.49, rgb(255, 255, 0), , , hwb

next

end sub

private sub tiaozheng()

dim i as long

for i = 0 to ubound(she) - 1

she(i).f = she(i + 1).f

next

end sub

private sub chanshengshiwu()

randomize timer

1:x = int(rnd * 24)

y = int(rnd * 24)

if zhuangtai(x, y) > 0 then goto 1:

zhuangtai(x, y) = 2

me.line (x, y)-(x + 1, y + 1), rgb(255, 255, 0), bf

end sub