课程全名:An Introduction to Interactive Programming in Python,来自 Rice University

授课教授:Joe Warren, Scott Rixner, John Greiner, Stephen Wong

工具:http://www.codeskulptor.org/, simplegui 模块

这是第三次作业,前面两次主要是熟悉Python动手做起来都很简单,就不记录了。

作业需要完成一个关于手表的游戏,估计很多人也都玩过。初中和高中考试做完卷子,但离收卷还有很久,就折腾手上的电子表用来掐秒,锻炼反应力。这里要实现的游戏是一个道理,就是酱紫。

这个Mini project开始采用事件驱动(event-driven)的交互方式,所以先明确一些问题。

1. 界面canvas的drawing速率大概是 60 frams/sec

2. 每一个应用程序需要注册一个特殊的事件句柄叫做 draw handler

3. 在simplegui 模块中,创建和注册draw handler最后的绘制是在canvas中

4. 绘制的操作有很多种,例如线、圆、多边形等,这些都在simplegui模块里面定义,具体查看doc

canvas的布局如下,原点在左上角,第一个坐标是宽度,第二个坐标是高度

Stopwatch完成的结果如下:

Start开始游戏,Stop暂停,Reset重置。右上角记录 成功停在整数秒次数/尝试次数

需要的元素:

frame:主界面

start button, stop button, reset button.

timer: 计时

逻辑设计:

start button事件: 开始计时

def start_handler():
global is_running
if is_running is False:
timer.start()
is_running = True

stop button事件:停止计时,更新右上角记录

def stop_handler():
global num_stop, num_succ_stop, is_running
if is_running is True:
timer.stop()
num_stop += 1
if tick % 10 == 0 and tick != 0:
num_succ_stop += 1
is_running = False

reset button事件:停止计时,重置相关参数

def reset_handler():
global tick, num_stop, num_succ_stop, is_running
timer.stop()
num_succ_stop, num_stop, tick = 0, 0, 0
is_running = False

timer 事件:负责计时不断增加,每次0.1s

def timer_handler():
global tick
tick += 1

draw事件:界面元素绘制

def draw_handler(canvas):
canvas.draw_text(format(tick), [100, 100], 30, "Green")
canvas.draw_text(str(num_succ_stop) + "/" + str(num_stop), [250, 20], 20, "Green")

定义了一个辅助的方法format用来转换当前时间t为00:00.0格式

def format(t):
D = str(t % 10)
BC = t / 10 % 60
if BC < 10:
BC = '0'+str(BC)
else:
BC = str(BC)
A = str(t / 10 / 60)
return A + ":" + BC + "." + D

整体完整代码如下:

# template for "Stopwatch: The Game"

import simplegui

# define global variables
tick = 0
num_stop = 0
num_succ_stop = 0
is_running = False # define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
D = str(t % 10)
BC = t / 10 % 60
if BC < 10:
BC = '0'+str(BC)
else:
BC = str(BC)
A = str(t / 10 / 60)
return A + ":" + BC + "." + D # define event handlers for buttons; "Start", "Stop", "Reset"
def start_handler():
global is_running
if is_running is False:
timer.start()
is_running = True def stop_handler():
global num_stop, num_succ_stop, is_running
if is_running is True:
timer.stop()
num_stop += 1
if tick % 10 == 0 and tick != 0:
num_succ_stop += 1
is_running = False def reset_handler():
global tick, num_stop, num_succ_stop, is_running
timer.stop()
num_succ_stop, num_stop, tick = 0, 0, 0
is_running = False # define event handler for timer with 0.1 sec interval
def timer_handler():
global tick
tick += 1 # define draw handler
def draw_handler(canvas):
canvas.draw_text(format(tick), [100, 100], 30, "Green")
canvas.draw_text(str(num_succ_stop) + "/" + str(num_stop), [250, 20], 20, "Green") # create frame
frame = simplegui.create_frame('Stopwatch: The Game', 300, 200) # register event handlers
start_btn = frame.add_button('Start', start_handler, 200)
stop_btn = frame.add_button('Stop', stop_handler, 200)
reset_btn = frame.add_button('Reset', reset_handler, 200)
frame.set_draw_handler(draw_handler)
timer = simplegui.create_timer(100, timer_handler) # start frame
frame.start() # Please remember to review the grading rubric

其中is_running用来记录当前timer是否在运行,防止重复点击stop出现计数错误的问题。

整体逻辑也比较简单,课程挺有意思,声音听着超棒,锻炼听力挺不错。每个老师相当的负责和认真。感叹自己本科那时初学C的时候,授课的差距是那么大,也感慨现在有这么多好的资源可以利用。学无止境。

Mini projects #3 ---- Stopwatch: The Game的更多相关文章

  1. Mini projects #8–RiceRocks

    课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...

  2. Mini projects #7 ---- Spaceship

    课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...

  3. Mini projects #6 ---- Blackjack

    课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...

  4. Mini projects #5 ---- Memory

    课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...

  5. Mini projects #4 ---- Pong

    课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...

  6. A Complete List of .NET Open Source Developer Projects

    http://scottge.net/2015/07/08/a-complete-list-of-net-open-source-developer-projects/?utm_source=tuic ...

  7. Building Xcode iOS projects and creating *.ipa file from the command line

    For our development process of iOS applications, we are using Jenkins set up on the Mac Mini Server, ...

  8. All the Apache Streaming Projects: An Exploratory Guide

    The speed at which data is generated, consumed, processed, and analyzed is increasing at an unbeliev ...

  9. Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目

    Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...

随机推荐

  1. Pureftpd

    在lnmp目录下运行./pureftpd.sh 如下:[root@cloud lnmp1.3-full]# 执行./pureftpd.sh 使用命令 Install Pure-FTPd complet ...

  2. Appium学习路—脚本篇(启动app)

    启动之前的准备   1.脚本执行前,需要先启动appium的server端, 启动server方法: 打开appium客户端,点击右上角的Launch     2.iOS的测试只能在mac本上做   ...

  3. linux命令(7):mv命令

    mv命令 mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] ...

  4. eclipse error pages打红X的解决方法

    当我在关闭eclipse时,漫长的等待进度条,我情急之下强关了系统.悲催的是再打开eclipse时新建动态web项目时,总是出现error pages打红X问题,程序执行等都不受影响,就是看着不爽.网 ...

  5. maven的pom.xml配置

    添加tomcat插件配置: <!-- tomcat plugin --> <plugin> <groupId>org.apache.tomcat.maven< ...

  6. 【Android端 APP 启动时长获取】启动时长获取方案及具体实施

    一.什么是启动时长? 1.启动时长一般包括三种场景,分别是:新装包的首次启动时长,冷启动时长.热启动时长 冷启动 和 热启动 : (1)冷启动:当启动应用时,后台没有该程序的进程,此时启动的话系统会分 ...

  7. 【接口测试】jmeter的使用

    学习地址: https://ke.qq.com/webcourse/index.html#course_id=89002&term_id=100088372&taid=15345918 ...

  8. 关于MySQL的CRUD操作

    一.清除mysql表中数据 delete from 表名; truncate table 表名; 不带where参数的delete语句可以删除mysql表中所有内容,使用truncate table也 ...

  9. CSS3径向渐变----大鱼吃小鱼之孤单的大鱼

    最近迷恋上了钓鱼,可是总钓不到大鱼,所以就画条大鱼来安慰一下我这柔弱的心灵. 先上图: 上面这个就是今晚上我要跟大家分享的小DEMO,我给他起名字就“大鱼吃小鱼之孤单的大鱼”. 转入正题,这条大鱼分为 ...

  10. html5调取手机摄像头或相册

    html5调用手机摄像头或者相册 由于input的type=file 格式的文件的界面并不是我们所希望的界面,所以在此我隐藏input,自定义样式,这个样式就在<a>中自己定义,这里我就不 ...