模块 'pygame.locals' 没有属性 'SCREEN_WIDTH'
Module 'pygame.locals' has no attribute 'SCREEN_WIDTH'
我不确定为什么会这样,我理解问题所在,但仔细检查了我的代码,但无法找出问题所在。我对 python 很陌生,尤其是 pygame,所以我可能犯了一个愚蠢的错误。该游戏是一款赛车游戏,您必须避开从上方驶来的汽车。
这是有错误的代码片段:
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("car_image.png")
self.rect = self.image.get_rect()
self.rect.center=(random.randint(40,pygame.locals.SCREEN_WIDTH-40),0)
def move(self):
self.rect.move_ip(0,10)
if (self.rect.top > 600):
self.rect.top = 0
self.rect.center = (random.randint(30, 370), 0)
def draw(self, surface):
surface.blit(self.image, self.rect)
完整代码如下:https://pastebin.com/0XLucRVm
我们将不胜感激。
pygame.locals.SCREEN_WIDTH
不存在。你发明了那个。您可以从显示表面获取 window 的宽度。例如:
screen_width = pygame.display.get_surface().get_width()
(参见 pygame.display
and pygame.Surface
)
我不确定为什么会这样,我理解问题所在,但仔细检查了我的代码,但无法找出问题所在。我对 python 很陌生,尤其是 pygame,所以我可能犯了一个愚蠢的错误。该游戏是一款赛车游戏,您必须避开从上方驶来的汽车。
这是有错误的代码片段:
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("car_image.png")
self.rect = self.image.get_rect()
self.rect.center=(random.randint(40,pygame.locals.SCREEN_WIDTH-40),0)
def move(self):
self.rect.move_ip(0,10)
if (self.rect.top > 600):
self.rect.top = 0
self.rect.center = (random.randint(30, 370), 0)
def draw(self, surface):
surface.blit(self.image, self.rect)
完整代码如下:https://pastebin.com/0XLucRVm
我们将不胜感激。
pygame.locals.SCREEN_WIDTH
不存在。你发明了那个。您可以从显示表面获取 window 的宽度。例如:
screen_width = pygame.display.get_surface().get_width()
(参见 pygame.display
and pygame.Surface
)