简体   繁体   English

Python,点符号引起混淆

[英]Python, confused by dot notation

I'm currently reading Python Crash Course, and there is that code: 我目前正在阅读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

I'm pretty sure i'm asking basic stuff, but nor my brain, nor my google skills aren't that good. 我很确定我要问的是基本知识,但是我的大脑和Google技能都不是那么好。

  1. This thing: self.rect = self.image.get_rect(). 这件事:self.rect = self.image.get_rect()。 Aren't get_rect() part of pygame.Screen module? pygame.Screen模块的get_rect()部分不是吗? If so, shouldn't it be used like pygame.Screen.get_rect()? 如果是这样,不应该像pygame.Screen.get_rect()一样使用它吗?

  2. self.rect.centerx = self.screen_rect.centerx Too.many.dots. self.rect.centerx = self.screen_rect.centerx太多。 Is rect class instance, that is being created by get_rect() method, and centerx is attribute of that instance? 是由get_rect()方法创建的rect类实例,而centerx是该实例的属性吗? Also, screen variable (or class instance?) is defined like that: screen = pygame.display.set_mode((1200, 800)) 此外,屏幕变量(或类实例?)的定义如下:screen = pygame.display.set_mode((1200,800))

Thanks in advance! 提前致谢!

Edit: Thanks for answers! 编辑:感谢您的答案!

  1. In PyGame, the get_rect() function is defined in a class that represents some sort of surface. 在PyGame中,get_rect()函数在表示某种表面的类中定义。 In this case, both the screen and the image have that method because both subclass surfaces and so they both have the get_rect() method. 在这种情况下,屏幕和图像都具有该方法,因为两个子类表面都具有get_rect()方法。

  2. This code: self.rect.centerx = self.screen_rect.centerx can be translated as setting the centerx of the rect of myself to the centerx of the screen of myself. 此代码: self.rect.centerx = self.screen_rect.centerx可以翻译为将自己矩形的centerx设置为自己屏幕的centerx。 In other words, it moves the object to the center of the screen. 换句话说,它将对象移动到屏幕的中心。 The self keyword represents the current object and each dot represents a property of that object. self关键字表示当前对象,每个点表示该对象的属性。

get_rect() is a method of Surface . get_rect()是Surface的方法 Both Screen and Image are a Surface (they implement the Surface interface). 屏幕和图像都是表面(它们实现了Surface接口)。 Screen is just special Surface that refers to the Surface that's actually mapped to a location on-screen, while other Surfaces like Image are off-screen object in the memory that can be drawn or drawn into without affecting what's on the screen. 屏幕只是特殊的表面,是指实际上已映射到屏幕上某个位置的表面,而其他表面(例如图像)是内存中的屏幕外对象,可以在不影响屏幕内容的情况下将其绘制或绘制到其中。

self.rect is a Rect instance , which is used by pygame to track the position and dimension of Surface/s. self.rect是一个Rect实例 ,pygame使用它来跟踪Surface / s的位置和尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM