賽車遊戲(vb6)

2008-06-01 7:06 am
急~下星期交~
求code(最好) OR 直接指導~
版面:5架車打橫行,以線為終結。
有制寫住START
玩法:按START, Inputbox出現要玩家輸入車號碼
輸入後…
車開始自己動去終點到終點,message box 出現
估對: message box話佢嬴左
估錯: message box話佢輸左
按OK,有另一個message box問佢要5要玩多次
Yes:繼續
No:結束
p.s.我覺得最難係要d車開始自己動去終點同埋去到終點就停
有位人兄答我:
Private choice As String ' <-- Module level

Private Sub StartButton_Click()
Dim I As Integer

For I = 0 To 4
Image1(I).Left = 10000
Next
choice = InputBox("Please enter the number of the car", "car", " number")
Timer1.Enabled = True ' <-- 開始 Timer
End Sub

Private Sub Timer1_Timer()
Dim I As Integer
Dim race_over As Boolean

race_over = False
Randomize
For I = 0 To 4
Image1(I).Left = Image1(I).Left - (Int(Rnd * 100) + 100)
If Image1(I).Left < 500 Then ' <-- 到終點
Timer1.Enabled = False ' <-- 停止 Timer
If choice = CStr(I) Then '<-- 估對
MsgBox "Win"
Else
MsgBox "Loose"
End If
race_over = True
Exit For ' <-- 離開 For Next Loop
End If
Next

If race_over Then
If MsgBox("Do you want to play again?", vbYesNo, "car") = vbYes Then
StartButton_Click '<-- 再玩過
End If
End If
End Sub
首先多謝個位人兄寶貴既意見~
不過我試過發現有D問題!(當中有D我吾明既)
吾明:
1) Private Sub StartButton_Click() 可以用Private Sub Command1_Click()嗎?

2) Dim I As Integer For I = 0 To 4 Image1(I).Left = 10000 Next上面要來仲咩~

3) 最吾明Timer D code~
4) Dim race_over As Boolean
If choice = CStr(I) Then
未學過~最好5用~用咩取代?
基本上,5要個D我吾明,個programme work 5到!
特別係~(要d車開始自己動去終點同埋去到終點就停)得一架車?
祈求各位幫助~(係個位好人既人兄就最好~)
有人幫我改下就最好~
多謝~

回答 (1)

2008-06-02 6:54 am
✔ 最佳答案
用 Array 做 Image,Image1(0) 到 Image1(4),代表 1 至 4 號車,唔要 Image2 到 Image5,睇番
http://hk.knowledge.yahoo.com/question/question?qid=7008053001120
我有講點整 Array。

start button 叫 Command1,click event 就係 Command1_Click()。

Timer interval 定為 100即是 timer event 每 10分1秒行一次。

再逐行解釋一次

Private choice As String ' <-- Module level

Private Sub Command1_Click()
Dim i As Integer

For i = 0 To 4 ' <-- 每架車一次
Image1(i).Left = 10000 ' <-- 設定開車位置
Next
choice = InputBox("Please enter the number of the car", "car", " number")
Timer1.Enabled = True ' <-- 開始 Timer(即是開車)
End Sub

Private Sub Timer1_Timer()
Dim i As Integer
Dim race_over As Boolean

race_over = False '<-- 設比賽結束為 False
Randomize '<-- 設定隨幾數
For i = 0 To 4 <-- 每架車做一次
Image1(i).Left = Image1(i).Left - (Int(Rnd * 100) + 100)
If Image1(i).Left < 500 Then ' <-- 到終點
Timer1.Enabled = False ' <-- 停止 Timer (停車)
If choice = CStr(i) Then '<-- 估對 (CStr(i) 將 i 轉做 字串(string))
MsgBox "You win"
Else
MsgBox "You lose"
End If
race_over = True ' <-- 比賽結束
Exit For ' <-- 離開 For Next Loop
End If
Next

If race_over Then '<-- 比賽結束
If MsgBox("Do you want to play again?", vbYesNo, "car") = vbYes Then
Command1_Click '<-- 再玩過 (再行Command1_Click)
End If
End If
End Sub

2008-06-02 02:00:15 補充:
Image1(0) 到 Image1(4),係代表 0 至 4 號車

2008-06-02 23:50:04 補充:
在 For Next loop 內
If Image1(i).Left < 500 Then ' <-- 到終點,即是第 i 架車首先過終點。

If choice = CStr(i) Then '<--玩家輸入的數字等如 i 就嬴


收錄日期: 2021-04-25 20:34:27
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080531000051KK03235

檢視 Wayback Machine 備份