【发布时间】:2018-12-20 05:09:45
【问题描述】:
我是 python 新手,我有一个关于类的问题。创建一个类后,当你使用def init(self)时,什么时候需要self后面有参数。在一些 pygame 中,我看到有时它们包含有时不包含在下面的代码中,用于设置游戏(外星人入侵)和绘制一艘船:我很困惑为什么主程序中有一个屏幕参数定义显示,设置中没有参数。请解释一下,谢谢。
class Ship():
def __init__(self, screen):
# initializes the screen and set its starting postion
self.screen = screen
# now we load the image
self.image = pygame.image.load("ship.bmp")
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# starting ship at each bottom of screen
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
# drawing ship at its locations, use blitme
self.screen.blit(self.image, self.rect)
class Settings():
def __init__(self):
self.screen_width = 1000
self.screen_height = 700
self.bg_color = (230, 230, 230)
【问题讨论】:
标签: python python-3.x oop