coursera-miniproject stopwatch任務總結

來源:互聯網
上載者:User

標籤:style   blog   color   io   ar   for   資料   sp   div   

---恢複內容開始---

首先是miniproject的說明:

combin text drawing in canvas with time to build digital stopwatch.tenths of a second-秒後有一個小數點3 buttons "Start" "Stop""Reset"1.interval: 0.1s---creat_timer參數interval以毫秒為單位,0.1秒=100ms.用int,不要float2.event handler function for canvas draws current time event handler 為了在畫布中間上顯示現在的時間。在draw之前用str轉換現在的時間為字串3.添加“Start Stop" event handler 作用是開始和結束 ”Reset"結束及時,重新開始現在的時間,從0開始。架構剛開啟的是0狀態4.寫一個helper function,  format(t)  return a string  A :BC.D   ACD [0,9]  B[0,5]有可供測試的模板。粘貼format函數def到模板中。。string return by format函數應該包含0佔位。例如:format(11) = 0:01.1  format(613) = 1:01.3---提示:用除法和餘數將全域變數轉化成相應格式5.插入一個call to format到draw handler裡面,秒錶只要能工作超過10min就行,超過10min之後隨便怎樣6.將stopwatch變成一個測反映速度的,X(停在整數秒)Y(總體實驗次數)7.按“Stop”後timer stop但不改變得分數。建議添加布爾變數 True-秒錶工作 False-秒錶停止,然後用這個數值決定按stop的時候是否更新score8.modify Reset設定這些數值歸零#第四步 format(t)參考code clinic(論壇),看那裡面的process可以節省時間評分要點:1.按Run,開啟一個架構-有個靜止的秒錶1.有”start“按鈕,可以實現開始功能1.有”stop“按鈕,實現停止功能,並更新success/attampts1.有”Reset“按鈕,實現複位功能4.畫布上顯示的時間符合格式要求,分秒,十分位秒,還有空白位的02.按”stop"後秒錶不停???success/attampts結果能更新.如果計時器已經停止,按stop仍然更新success/attampts只得1分1.“Reset"清除success、/attampts結果下面是我糾結的編碼思考過程:首先是 format(t),將遞增數字t轉化成A:BC.D的格式:t:每隔0.1s count+1format(t),將t轉化成A :BC.D的格式---第一節單位轉換1325-2:12.15      1325//600 = 2-----a1325-2*600 = 125       125%60 = 5          125-5 = 120    120//100 = 1 ----b (除60是不是多餘呢?換一個數字試試第二種演算法 125//10 = 121325-2*600 = 125       125%60 = 5          125-5 = 120    120/10 = 12    12//10 = 2----c有些算錯了,BC這兩個數字不對,考慮用string來表達?設t=214,格式為A:BC:D正確結果應該是0:21.4 如果按照這個公式計算B = ((t - t // 600 * 600) - (t - t // 600 * 600) % 60) // 100t//600 = 0   t-t//600*600 =214    (t-t//600*600)%60=10/8晚上洗完澡後,冷靜思考,確認A和D的表達沒問題,問題出在B和C,然後運用推理寫出了B和C的表達,format(t)的終板如下: 
def format(t):    A = t // 600    B = (t - t // 600 * 600) // 100    C = (t - t // 600 * 600) // 10 - (t - t // 600 *600) // 100 * 10    d = str (t)    D = d[-1]    return str(A) + ":" + str(B) + str(C) + "." + D

第二天實驗了一些資料,應該是沒問題的。

然後開始寫可視frame

Timer的格式?

Timer不能工作 報錯

Count在最前面定義t

Format(t)將count得到的t轉換成A:BC.D的格式

Draw_handler在畫布上將Format(t)顯示出來

刪掉多餘的,單純實驗timer

我解決了timer的問題!!!柳暗花明又一村啊!!!剛才躺床上都想放棄了,我刪掉多餘的,單純實驗timer就解決問題啦,原來simplegui.creat_timer  creat少打了一個e,應該是create啊!

然後又報錯canvas.draw_text(format(t),[100,100],24,”white”)  t沒有定義

我就設定t為全域變數 t = 0。但是畫布上只顯示了0:00.0,我再在tick裡面聲明全域變數t, 

Def tick()

t+=1

return t

點擊run,就可以啦:

 

現在還需要:

1.3個可以工作的按鈕;

貌似已經解決了,用timer.start()  timer.stop()

但是開始的時候秒錶就開始跑了,這是咋回事?

2.記分牌,按到stop整秒記1分,如何表達記分牌

3.按Run啟動的秒錶應該是停止狀態,按start才開始架構剛開啟的是0狀態

先處理第三個問題

關於第二個問題,Stop的邏輯應該是比較複雜的,start按鈕應該是比較簡單的,因為stop是要記分數的

按start後t = 0,

現在我run以後馬上開始count,不應該這樣,應該是我點start之後開始count

Tick 應該是停止的狀態?

Timer.start()

T=0這個全域變數是否應該存在?

點開始 timer就開始運行,第二個參數tick

解決了!!!在最後寫

Timer.stop()

就!行!啦!

第二個問題:如何寫兩個draw handler? 用空白的試一試?看一看視頻?看完視頻我就知道怎麼寫了,是寫兩行:

Def draw(canvas):

Canvas.draw_text(format(t), [100, 100], 24, “White)#format(t)必須是字串

Canvas.draw_text(“Hello!”, [200, 50], 24, “White)

按下stop,X/Y,Y值加1,X值是否加1要看是否是整數秒

Reset"清除success、/attampts結果

A:BC.D

每次點擊Stop, Y加1

只要D=0,X就加1

否則X不加1

D = d[-1] = str(t)[-1]

下面的代碼,不能工作,stop,start,Reset都不好使了

 

---------------------------------------------------------------------------

def stop():#stop不工作

    x = 0

    y = 0

   

    y += 1

    if int(str(t)[-1]) == 0:

        x += 1#x,y都是整數,非字串

    else:

        return x#這裡好像有問題?

    timer.stop()

    return str(x) + "/" + str(y)#這裡應該不用return成字串的,應該是 canvas.draw_text(str(x) + "/" + str(y)

    ======================================

Stop具體要實現什麼功能?

1.秒錶停止   2.Y值加1    3.如果t為整數秒,X加1

不改動的話功能1可以實現,現在要考慮如何?功能2。3

另外定義一個X/Y?

先看看code clinic

l  按照code clinic說法,stop 按鈕應該檢查t% 10 == 0

l  Draw handler call format(t)

用布爾函數,True of False; 。建議添加布爾變數 True-秒錶工作running False-秒錶停止stop,然後用這個數值決定按stop的時候是否更新score

If timer.is_running():

之前有模板說可以給出True 和false的?

def stop_button_handler(): 
    globalnum_pushes,num_succ_pushes
    if timer.is_running() andtenths_of_sec==0:#可以改
        num_succ_pushes += 1
        num_pushes += 1
        timer.stop()
    elif timer.is_running():
        num_pushes += 1
        timer.stop()
    else:
        timer.stop()

現在參考上面的代碼,試一試,上面的代碼啟發了我(論壇有人貼的)

難道是draw_handler_text出了問題?一定要是字串?

Canvas.draw_text(str(x) + “/” + str(y),[200, 200], 24, “Green”)#這裡才關鍵,之前一直想要這樣:canvas.draw_text(stop(), [200,200])

我搞!定!了!

 

defstop():

    global x, y

    if timer.is_running() and t % 10 == 0:

        x += 1

        y += 1

        timer.stop()

    elif timer.is_running():

        y += 1

        timer.stop

    else:

       timer.stop

刪掉timer.is_running()不行,因為刪掉後,計時器已經停止了,按stop x,y依然家1

最後完成了,reset是這樣的:

Def reset():

  Global t, x, y

  T = 0

X = 0

Y = 0

Timer.stop()

 

完整代碼如下:
# template for "Stopwatch: The Game"import simpleguit = 0x = 0y = 0# define helper function format that converts time# in tenths of seconds into formatted string A:BC.Ddef format(t):    A = t // 600    B = (t - t // 600 * 600) // 100    C = (t - t // 600 * 600) // 10 - (t - t // 600 *600) // 100 * 10    d = str (t)    D = d[-1]    return str(A) + ":" + str(B) + str(C) + "." + D   # define event handlers for buttons; "Start", "Stop", "Reset"def start():    timer.start()#在某一個時間停止,    def stop():    global x, y    if timer.is_running() and t % 10 == 0:        x += 1        y += 1        timer.stop()    elif timer.is_running():        y += 1        timer.stop()   def reset():        global t, x, y    t = 0        x = 0    y = 0    timer.stop()# define event handler for timer with 0.1 sec intervaldef tick():    global t    t += 1#不需要return# define draw handlerdef draw(canvas):    canvas.draw_text(format(t), [80, 120], 50, "White")    canvas.draw_text(str(x) + "/" + str(y), [220, 30], 35, "Green")# create framef = simplegui.create_frame("Stopwatch", 300, 200)# register event handlersf.add_button("Start", start, 100)f.add_button("Stop", stop, 100)f.add_button("Reset", reset, 100)f.set_draw_handler(draw)timer = simplegui.create_timer(100, tick)# start framef.start()timer.stop()# Please remember to review the grading rubric

 很有成就感!嗯!

coursera-miniproject stopwatch任務總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.