生成没有Pygame Sprite类的平台

时间:2015-08-06 00:48:51

标签: python pygame

如何在没有 level课程的情况下为Sprite 的相应“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()

1 个答案:

答案 0 :(得分:0)

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

#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,而应增加“platform.platx”。

此外,您需要在绘制每一行后重置platform.platx