我目前正在阅读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
我很肯定我问的是基本的东西,但我的大脑和谷歌技能都不是那么好。你知道吗
这件事:自我纠正= self.image.get\r(). get rect()不是pygame.屏幕模块?如果是这样的话,难道不应该这样使用吗pygame.Screen.get\r\n()?
你知道吗self.rect.centerx= 自屏幕.centerx文件太多点了. 是rect类实例,它是由get\ rect()方法创建的,centerx是该实例的属性?另外,屏幕变量(或类实例?)定义如下:screen=pygame.display.set\模式((1200,800))
提前谢谢!你知道吗
编辑:谢谢你的回答!你知道吗
在PyGame中,get\ rect()函数是在表示某种曲面的类中定义的。在这种情况下,屏幕和图像都有这个方法,因为它们都有get\rect()方法。
这个代码:
self.rect.centerx = self.screen_rect.centerx
可以翻译为将我自己的矩形的centerx设置为我自己屏幕的centerx。换句话说,它将对象移动到屏幕的中心。self关键字表示当前对象,每个点表示该对象的属性。get_rect() is a method of Surface。屏幕和图像都是一个曲面(它们实现了曲面接口)。屏幕只是一个特殊的表面,它指的是实际映射到屏幕上某个位置的表面,而像Image这样的其他表面则是内存中的屏幕外对象,可以在不影响屏幕内容的情况下进行绘制或绘制。你知道吗
你知道吗自我纠正是一个Rect instance,pygame使用它来跟踪曲面的位置和尺寸
相关问题 更多 >
编程相关推荐