Interactive Programming in Python Week7 Code style

 1 def draw(c):
 2     global paddle1_pos, paddle2_pos
 3
 4     paddle_width = 80
 5
 6     if paddle_width/2 <= paddle1_pos + paddle1_vel <= width - paddle_width/2:
 7         paddle1_pos += paddle1_vel
 8     if paddle_width/2 <= paddle2_pos + paddle2_vel <= width - paddle_width/2:
 9         paddle2_pos += paddle2_vel
10
11     c.draw_line([width/2, 0],[width/2, height], 1, "White")
12
13     c.draw_line([4, paddle1_pos-paddle_width/2], [4, paddle1_pos+paddle_width/2], 4, "White")
14     c.draw_line([width-4, paddle2_pos-paddle_width/2], [width-4, paddle2_pos+paddle_width/2], 4, "White")

week 4 项目pong中绘制滑板以及模拟其移动的语句,注意if语句(控制滑板不移动出画布范围)里的变量是pos+vel,即要保持更新后的状态在画布内。

现在通过引入paddle class 来使代码风格更为简练。

 1 class Paddle:
 2     def __init__(self, loc, pos, vel):
 3         self.loc = loc
 4         self.pos = pos
 5         self.vel = vel
 6         self.width = 80
 7
 8     def move(self):
 9         if self.width/2 <= self.pos + self.vel <= width - self.width/2:
10             self.pos += self.vel
11
12     def draw(c, self):
13         c.draw_line([self.loc, self.pos-self.width / 2], PADDLE_THICKNESS, "White")
14
15 def draw(c):
16     paddle1.move()
17     paddle2.move()
18
19     c.draw_line([width / 2, 0],[width / 2, height], 1, "White")
20
21     paddle1.draw(c)
22     paddle2.draw(c)

对于原来的key handler(响应键盘的function),原来的code

 1 def keydown(key):
 2     global paddle1_vel, paddle2_vel
 3     if key == simplegui.KEY_MAP["up"]:
 4         paddle2_vel -= 2
 5     elif key == simplegui.KEY_MAP["down"]:
 6         paddle2_vel += 2
 7     elif key == simplegui.KEY_MAP["w"]:
 8         paddle1_vel -= 2
 9     elif key == simplegui.KEY_MAP["s"]:
10         paddle1_vel += 2

引入Dictionary 和一个iteration,即

1 inputs = {"up": [1, -2],
2           "down": [1, 2],
3           "w": [0, -2],
4           "s": [0, 2]}
5
6 def keydown(key):
7     for i in inputs:
8         if key == simplegui.KEY_MAP[i]:
9             paddle_vel[inputs[i][0]] += inputs[i][1]

初看起来有些复杂 ,但是若看清inputs[i]的含义就瞬间通畅,比如当i是up时,inputs[i]代表[1,-2],所以inputs[i][0]就是1,input[i][1]则是-2

所以最后一条语句 代表 paddle_vel[1] += -2 即paddle1的速度变成朝上的2.

最后一条 ,尽量用通过变量间的关系来简化代码,尽量不用magic costant(满篇都是用手算出来的数字)

时间: 2025-01-02 01:28:39

Interactive Programming in Python Week7 Code style的相关文章

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project — “Guess the number” game

Mini-project description — “Guess the number” game One of the simplest two-player games is “Guess the number”. The first player thinks of a secret number in some known range while the second player attempts to guess the number. After each guess, the

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project— Rock-paper-scissors-lizard-Spock

Mini-project description — Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously "throw” one of three hand signals that correspond to rock, paper or s

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

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

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —&quot;Pong&quot;

In this project, we will build a version of Pong, one of the first arcade video games (1972). While Pong is not particularly exciting compared to today's video games, Pong is relatively simple to build and provides a nice opportunity to work on the s

【python】An Introduction to Interactive Programming in Python(week two)

This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.event-drvien programing 4 event types: Input: button, textbox Keyborad: key up, key down Mouse: click, drag Timer example: # Example of a simple event-

Python&#39;s Coding Style

一个程序员良好的素养可以从他的代码风格里看出. Python官方的开发者指南,PEP8中,列举了Style Guide for Python Code. 摘录The Python's Tutorial中的依依列出. 1.用4空格缩进,而不是tab键. 2.使每行不超过79个字符,目的是小屏幕用户也能很好的阅读. 3.用空一行的方式去把函数,类,和函数中大的代码块分开. 4.如果可能的话,尽量把注释写在一行里. 5.Use docstrings.使用文档字符串.(Ps.这个可得利用好了) 6.在操

IDEA学习系列之剖析IDEA里的Code Style(适合各种语言)(不断更新)(图文详解)

不多说,直接上干货! File  -> Settings ->  Editor  ->   Code Style   (1)HOCON 分为: Tabs  and Indents . Spaces . Wrapping and Braces 和  Blank Lines (2)Scala 分为:Tabs and Indents.Spaces.Wrapping and Braces.Blank Lines.ScalaDoc.Imports.Multi-line strings.Type A

ios code style

注释 建议使用VVDocumenter插件 多行注释 格式: /** 注释内容 */ 单行注释 格式: ///在对文件.类.函数进行注释时推荐使用多行注释,在函数体内对代码块进行注释时,使用单行注释 函数的注释 函数注释的格式为 /** * @brief * @param * @return **/ 在brief中需要写明函数的主要功能.注意事项 在param中需要写明函数的变量类型.变量的作用 在return中需要写明函数的返回类型.返回值的作用 如有其他需要说明的地方,可以在@return后

AWS s3 python sdk code examples

Yet another easy-to-understand, easy-to-use aws s3 python sdk code examples. github地址:https://github.com/garyelephant/aws-s3-python-sdk-examples. """ Yet another s3 python sdk example. based on boto 2.27.0 """ import time imp