生成没有Pygame Sprite类的平台

2024-10-02 08:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如果没有Sprite类,我如何生成平台来对应level的“E”?我尝试了一些不同的方法,但似乎都不管用。基本上,这个想法是“E”的应该只是在一排50x50块。你知道吗

import pygame
pygame.init()

display_width = 900
display_height = 600
size = (display_width, display_height)


black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)

platx = 350
platy = 150
platw = 50
plath = 50

clock = pygame.time.Clock()


platx = 600
platy = 350
platw = 50
plath = 50


level = ["E","E","E","E","E","E","E","E","E","E"]


class Platform:
    def __init__(self, platx, platy, platw, plath):
        self.platx = platx
        self.platy = platy
        self.platw = platw
        self.plath = plath

    def draw(self):
        pygame.draw.rect(screen, black, [self.platx, self.platy, self.platw, self.plath])

platforms = Platform(platx, platy, platw, plath)

game_exit = False

while not game_exit:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_exit = True
    #why doesnt a loop like this work?
    #for i in level:
    #    if i == "E":
    #        platforms.draw()    
    #    platx += display_width *.1
    #shouldn't it be placing platforms 1/10 of the display_width from the starting platx?

    screen.fill(white)
    pygame.display.update()

Tags: selfgamedisplayexitwidthlevelscreenpygame
1条回答
网友
1楼 · 发布于 2024-10-02 08:30:20

如果我正确理解了您的代码,这就是需要修复的代码:

#why doesnt a loop like this work?
    #for i in level:
    #    if i == "E":
    #        platforms.draw()    
    #    platx += display_width *.1
    #shouldn't it be placing platforms 1/10 of the display_width from the starting platx?

问题是platx已经作为参数传递给了Platform.__init__。由于变量platform.platx不再具有全局platx的弱参数(作为参数传递),因此它们不再匹配。因此,不是递增platx`平台.platx应该递增。你知道吗

另外,您需要在绘制每一行之后重置platform.platx。你知道吗

相关问题 更多 >

    热门问题