python刚入门,还需多努力

用 8 周时间完成 Coursera 课程 An Introduction to Interactive Programming in Python (Part 1 & Part 2), 除了第 8 周的第 6 个问题,所有的 Quiz 都是满分。这个问题是这样的:

Question 6

The bonus portion of this week's mini-project defines and uses a Sprite class to support animations. Each animated sprite includes an associated tiled image, each of whose sub-images are drawn in turn during the process of animating the sprite.

What attribute (also known as a field) of this Sprite class can be used to select the appropriate sub-image to draw during this animation process? (If you are stuck, review the bonus phase in the mini-project description.)

Your Answer ScoreExplanation
angle_vel   
angle   
radius   
sound   
image_size   
vel   
age   
image   
pos   
lifespan   
image_center   
animated

5 次答题全都选择错误[汗]。然后找到了这篇文章:http://blog.csdn.net/chjjunking/article/details/6859268

读过之后,发现自己还是对一些基本的概念还是没有掌握(全英文教学,英语不佳的后果~)。attribute, 或者说 field,相当于函数中的变量。然后我们看看实现 bonus 的代码:

    def draw(self, canvas):
        if self.animated:
            index = self.t % (self.image.get_width() // self.image_size[0])
            canvas.draw_image(self.image,
                              [self.image_center[0] + index * self.image_size[0], self.image_center[1]],
                             self.image_size, self.pos, self.image_size, self.angle)
            self.t += 1
            
        else:
            canvas.draw_image(self.image, self.image_center, self.image_size,
                          self.pos, self.image_size, self.angle)

这里面需涉及到的 attribute 有:animated, t, image, image_center, image_size, pos, angle。到底哪个才是正确的呢?再回到题目:

What attribute (also known as a field) of this Sprite class can be used toselect the appropriate sub-image to draw during this animation process?

原来做错的原因不是自己没有搞清楚attribute的概念,因为所有的答案都是attribute。而是自己没有读懂英文[汗][汗]。关键词是during,也就是在animation的过程中,那么答案就肯定不是animated,因为已经判断完成了。另外一个关键词是select sub-image,也就是 tile 下一个 image 需要用到哪个 attribute。相关的代码是:

self.image_center[0] + index * self.image_size[0]

需要依靠 index 和 image_size[0] 来 tile 下一个 image,其中 index 并不是类的 attribute,只是def draw(self, canvas) 的一个本地变量。正确答案应该是 image_size。

从上面的文章中又学到了新的知识,以前如果要在外部给 class attribute 赋值,只知道新建 1 个 class method:set_attribute(),但是现在知道了 可以直接在外部给 class attribute 赋值,甚至不用提前在 class 里面定义 attribute!

另外还有 class attribute和 date attribute 的区别,还没有搞懂,以后再研究吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值