• Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #3 —"Stopwatch: The Game"


    Mini-project description - "Stopwatch: The Game"


    Our mini-project for this week will focus on combining text drawing in the canvas with timers to build a simple digital stopwatch that keeps track of the time in tenths of a second. The stopwatch should contain "Start", "Stop" and "Reset" buttons. To help guide you through this project, we suggest that you download the provided program template for this mini-project and build your stopwatch program as follows:

    Mini-project development process

      1. Construct a timer with an associated interval of 0.1 seconds whose event handler increments a global integer. (Remember that create_timertakes the interval specified in milliseconds.) This integer will keep track of the time in tenths of seconds. Test your timer by printing this global integer to the console. Use the CodeSkulptor reset button in the blue menu bar to terminate your program and stop the timer and its print statements. Important: Do not use floating point numbers to keep track of tenths of a second! While it's certainly possible to get it working, the imprecision of floating point can make your life miserable. Use an integer instead, i.e., 12 represents 1.2 seconds.
      2. Write the event handler function for the canvas that draws the current time (simply as an integer, you should not worry about formatting it yet) in the middle of the canvas. Remember that you will need to convert the current time into a string using str before drawing it.
      3. Add "Start" and "Stop" buttons whose event handlers start and stop the timer. Next, add a "Reset" button that stops the timer and reset the current time to zero. The stopwatch should be stopped when the frame opens.
      4. Next, write a helper function format(t) that returns a string of the form A:BC.D where A, C and D are digits in the range 0-9 and B is in the range 0-5. Test this function independent of your project using this testing template http://www.codeskulptor.org/#examples-format_template.py. (Just cut and paste your definition of  format into the template.) Note that the string returned by your helper function format should always correctly include leading zeros. For example
        • format(0) = 0:00.0
        • format(11) = 0:01.1
        • format(321) = 0:32.1
        • format(613) = 1:01.3
        Hint: Use integer division and remainder (modular arithmetic) to extract various digits for the formatted time from the global integer timer.
      5. Insert a call to the format function into your draw handler to complete the stopwatch. (Note that the stopwatch need only work correctly up to 10 minutes, beyond that its behavior is your choice.)
      6. Finally, to turn your stopwatch into a test of reflexes, add to two numerical counters that keep track of the number of times that you have stopped the watch and how many times you manage to stop the watch on a whole second (1.0, 2.0, 3.0, etc.). These counters should be drawn in the upper right-hand part of the stopwatch canvas in the form "x/y" where x is the number of successful stops and y is number of total stops. My best effort at this simple game is around a 25% success rate.
      7. Add code to ensure that hitting the "Stop" button when the timer is already stopped does not change your score. We suggest that you add a global Boolean variable that is True when the stopwatch is running and False when the stopwatch is stopped. You can then use this value to determine whether to update the score when the "Stop" button is pressed.
      8. Modify "Reset" so as to set these counters back to zero when clicked
      9. # template for "Stopwatch: The Game"
        import simplegui
        # define global variables
        t = 0
        t_str = "0:00.0"
        
        position = [60, 100]
        width = 200
        height = 200
        interval = 100
        
        flag = False
        stop_num = 0
        win_num = 0
        score_str = str(win_num) + "/" + str(stop_num)
        
        # define helper function format that converts time
        # in tenths of seconds into formatted string A:BC.D
        def format(t):
            tens_second = t % 10
            t = t // 10
            second = t % 60
            minute = t // 60
            second_str = str(second)
            if second < 10:
                second_str = "0" + str(second)
            format_t = str(minute) + ":" + second_str + "." + str(tens_second)
            return format_t
        
        def update_score():
            global stop_num
            global win_num
            global score_str
            global flag
            
            if not timer.is_running():
                if flag:
                    flag = False
                    stop_num += 1
                    if t % 10 == 0:
                        win_num += 1
            else:
                flag = True
            score_str = str(win_num) + "/" + str(stop_num)
        
        def reset_score():
            global flag
            global stop_num
            global win_num
            global score_str
            
            flag = False
            stop_num = 0
            win_num = 0
            score_str = str(win_num) + "/" + str(stop_num)
            
        # define event handlers for buttons; "Start", "Stop", "Reset"
        def start_handler():
            timer.start()
        
        def stop_handler():
            timer.stop()
        
        def reset_handler():
            timer.stop()
            global t
            global t_str
            
            reset_score()
            
        # define event handler for timer with 0.1 sec interval
        def timer_handler():
            global t 
            global t_str
            t = t + 1
            t_str = format(t)
            
        def timer_score_handler():
            update_score()
            
        
        # define draw handler
        def draw_handler(canvas):
            canvas.draw_text(t_str, position, 36, "White")
            canvas.draw_text(score_str, [160, 20], 16, "green")
            
        # create frame
        frame = simplegui.create_frame("Stopwatch", width, height)
        
        # register event handlers
        frame.add_button("Start", start_handler)
        frame.add_button("Stop", stop_handler)
        frame.add_button("Reset", reset_handler)
        frame.set_draw_handler(draw_handler)
        timer = simplegui.create_timer(interval, timer_handler)
        timer_score = simplegui.create_timer(interval, timer_score_handler)
        # start frame
        frame.start()
        timer_score.start()
        
        # Please remember to review the grading rubric
        http://www.codeskulptor.org/#user40_VuvumcScXK0k0FO.py
  • 相关阅读:
    Windows平台下Android应用抓包挖掘漏洞方法
    长城宽带核心系统存严重漏洞,数十万用户、账单信息存泄露风险
    互联网公司站点通病之弱口令
    【Python】python-一个class继承的小case
    【Python】Python-skier游戏[摘自.与孩子一起学编程]
    【Python】一个python实例:给重要的文件创建备份.摘自crossin-python简明教程
    【JMeter】JMeter完成一个java请求的压测
    【Tcpcopy】离线回放功能
    【JMeter】Jmeter-完成一个http压力测试
    Apache-AB压力测试实例
  • 原文地址:https://www.cnblogs.com/tonony1/p/4795651.html
Copyright © 2020-2023  润新知