[英]Python, confused by dot notation
我目前正在阅读Python速成课程,并且有以下代码:
import pygame
class Ship():
def __init__(self, screen):
"""Initialize the ship, and set its starting position."""
self.screen = screen
# Load the ship image, and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
我很确定我要问的是基本知识,但是我的大脑和Google技能都不是那么好。
这件事:self.rect = self.image.get_rect()。 pygame.Screen模块的get_rect()部分不是吗? 如果是这样,不应该像pygame.Screen.get_rect()一样使用它吗?
self.rect.centerx = self.screen_rect.centerx太多。 是由get_rect()方法创建的rect类实例,而centerx是该实例的属性吗? 此外,屏幕变量(或类实例?)的定义如下:screen = pygame.display.set_mode((1200,800))
提前致谢!
编辑:感谢您的答案!
在PyGame中,get_rect()函数在表示某种表面的类中定义。 在这种情况下,屏幕和图像都具有该方法,因为两个子类表面都具有get_rect()方法。
此代码: self.rect.centerx = self.screen_rect.centerx
可以翻译为将自己矩形的centerx设置为自己屏幕的centerx。 换句话说,它将对象移动到屏幕的中心。 self关键字表示当前对象,每个点表示该对象的属性。
get_rect()是Surface的方法 。 屏幕和图像都是表面(它们实现了Surface接口)。 屏幕只是特殊的表面,是指实际上已映射到屏幕上某个位置的表面,而其他表面(例如图像)是内存中的屏幕外对象,可以在不影响屏幕内容的情况下将其绘制或绘制到其中。
self.rect是一个Rect实例 ,pygame使用它来跟踪Surface / s的位置和尺寸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.