--- Restore content start ---
The first is the description of miniproject:
Combin text drawing in canvas with time to build digital stopwatch. tenths of a second-second has a decimal point 3 buttons "start" "Stop" "reset" 1. interval: 0.1 s --- creat_timer parameter interval in milliseconds, 0.1 seconds = Ms. use int instead of float2.event handler function for canvas draws current time event handler to display the current time in the middle of the canvas. Use STR to convert the current time to string 3 before draw. add the "Start Stop" event handler to start and end the "reset" and start the current time again, starting from 0. The framework is in the 0 status. 4. write a helper function, format (t) return a string a: BC. d ACD [] B [] has templates available for testing. Paste the format function def to the template .. The string return by format function should contain 0 placeholder values. Example: Format (11) = 0: 01. 1 format (613) =. 3 --- tip: Use Division and remainder to convert global variables into corresponding formats. 5. insert a call to format to the draw handler. the stopwatch can only work for more than 10 minutes. 6. change stopwatch to a test to reflect the speed. X (in integer seconds) y (total number of experiments) 7. after you press "stop", timer stop does not change the score. We recommend that you add a Boolean variable true-Stopwatch work false-Stopwatch stop, and then use this value to determine whether to update score8.modify reset when you press stop to set these values to zero # Step 4 format (t) refer to the Code Clinic (ForUM) to see which process can save time and score points: 1. press run to open a frame-there is a static stopwatch 1. with the "Start" button, you can implement the start function 1. the "stop" button is available to implement the stop function, and the success/attampts1. there is a "reset" button to implement the reset function 4. the time displayed on the canvas conforms to the format requirements, namely, minute, second, second (very bit), and 02 (blank bit. after you press "stop", the stopwatch keeps running ??? Success/attampts results can be updated. If the timer has been stopped, press stop to continue updating success/attampts only requires 1 point 1. "reset" to clear success,/attampts results The following is my tangled coding process: first, format (t) converts the incremental number t into a: BC. d format: T: Convert t to A: BC every 0.1 s count + 1 format (t. format of D --- unit conversion in Section 1: 1325. 15 1325 // 600 = 2 ----- a1325-2 * 600 = 125 125% 60 = 5 125-5 = 120 120 // 100 = 1 ---- B (except 60 is not redundant? Try the second algorithm 125 // 10 = 121325-2*600 = 125 125% 60 = 5 125-5 = 120 120/10 = 12 12 // 10 = 2 ---- c Some incorrect, the two numbers of BC are incorrect. Do you want to use string to express them? Set T = 214 in the format of a: BC: D. the correct result should be 0: 21. 4 if B = (t-t // 600*600)-(t-t // 600*600) % 60 according to this formula) // 100 t // 600 = 0 T-t // 600*600 = 214 (t-t // 600*600) % 60 = 10/8 think calmly after taking a bath at night, confirm that expression A and expression D is correct. The problem lies in expression B and C. Then, the expression B and expression C are written using reasoning. The ending plates of format (t) are as follows:
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
I tested some data the next day and it should be okay.
Then begin to write the visual Frame
Timer format?
Error reported when timer cannot work
Count defines t at the beginning
Format (t) converts the T obtained by count to the: BC. d format.
Draw_handler displays the format (t) on the canvas.
Delete unnecessary, experiment timer only
I solved the timer problem !!! It's a different village !!! I wanted to give up on my bed just now. I deleted the redundant ones and tried timer to solve the problem. In the past, simplegui. creat_timer creat played less E. It should be "CREATE!
The canvas. draw_text (format (t), [100,100], 24, "White") t is not defined.
I set T to the global variable t = 0. But only 0: 00. 0 is displayed on the canvas. I declare the global variable t in tick,
Def tick ()
T + = 1
Return t
Click Run:
Now you need:
1.3 buttons that can work;
It seems that the solution has been solved. Use timer. Start () timer. Stop ()
But at the beginning, the stopwatch started to run. Why?
2. How to express the score card by pressing stop for 1 minute
3. the stopwatch started by pressing run should be in the stopped state, and the frame started by pressing start is in the 0 state.
Handle the third problem first
For the second question, the Stop logic should be relatively complex, and the start button should be relatively simple, because the stop button should remember the score.
Press Start and T = 0,
Now count starts immediately after I run it. It should not be like this. It should be after I start it
Should tick Be In The stopped status?
Timer. Start ()
T = 0 should the global variable exist?
Start timer and start running. The second parameter is tick.
Solved !!! Write at the end
Timer. Stop ()
That's it! OK! Now!
The second question: How do I write two draw handler statements? Try it with blank ones? Watch the video? After watching the video, I know how to write it. There are two lines:
Def draw (canvas ):
Canvas. draw_text (format (t), [100,100], 24, "White) # format (t) must be a string
Canvas. draw_text ("Hello !", [200, 50], 24, "White)
Press "stop", "x/y", and "Y" to add 1, and "X" to check whether the value is an integer second.
Reset "clear success,/attampts results
A: BC. d
Each time you click STOP, Y and 1
If D = 0, add 1 to X.
Otherwise, 1 is not added to X.
D = d [-1] = STR (t) [-1]
The following code cannot work, stop, start, or reset is not easy to use.
---------------------------------------------------------------------------
Def stop (): # Stop does not work
X = 0
Y = 0
Y + = 1
If int (STR (t) [-1]) = 0:
X + = 1 # X, Y are integers, not strings
Else:
Return X # Is there a problem?
Timer. Stop ()
Return STR (x) + "/" + STR (y) # Here we should not use return as a string, it should be canvas. draw_text (STR (x) + "/" + STR (y)
==============================================
What are the specific functions of stop?
1. Stopwatch stop 2. Y value plus 1 3. If t is an integer second, x plus 1
Function 1 can be implemented without modification. Now we need to consider how to implement function 2. 3
Define another x/y?
First look at the code Clinic
L according to code Clinic, the stop button should check T % 10 = 0
L draw handler call format (t)
Use a Boolean function, true of false ;. It is recommended to add a Boolean variable true-Stopwatch running false-Stopwatch stop, and then use this value to determine whether to update the score when you press stop.
If timer. is_running ():
Which templates can provide true and false?
Def stop_button_handler ():
Globalnum_pushes, num_succ_pushes
If timer. is_running () andtenths_of_sec = 0: # You can change
Num_succ_pushes + = 1
Num_pushes + = 1
Timer. Stop ()
Elif timer. is_running ():
Num_pushes + = 1
Timer. Stop ()
Else:
Timer. Stop ()
Now refer to the above Code and try it. The above code inspired me (posted on the Forum)
Is there a problem with draw_handler_text? Must be a string?
Canvas. draw_text (STR (x) + "/" + STR (Y), [200,200], 24, "green") # This is the key. I always wanted this: canvas. draw_text (stop (), [200,200])
I am doing it! Yes! Now!
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
You cannot delete timer. is_running () because after deletion, the timer has stopped. Press stop X, and Y is still in the first place.
Finally, the reset is as follows:
Def reset ():
Global t, x, y
T = 0
X = 0
Y = 0
Timer. Stop ()
The complete code is as follows:
# 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
A sense of accomplishment! Well!
Coursera-miniproject stopwatch task Summary