Python,Pygame Blitting to screen(参数1必须是pygame.Surface)

时间:2015-07-20 02:34:08

标签: python pygame blit

我正在创建一个块列表,我想循环使用pygame.blit函数在pygame中打印到屏幕。但是会抛出一个错误(参数1必须是pygame.Surface,而不是阻止)。 我已经将pygame打印对象中的其他游戏制作到屏幕,但从未将它们放在列表中。任何帮助将不胜感激。

    import pygame
import time



class block:
    def __init__(self, image, posX, posY, name):

        #Take passed image and assign to block#
        self.image = image

        #Create a rect for block for collisions#
#        self.rect = self.image.get_rect()

        #Create a positio from passed integers#
        self.posX = posX
        self.posY = posY

        #Set id of the block#
        self.name = name

    def draw(self, screen):
        screen.blit(self.image, (self.posX, self.posY))

    def getName(self):
        return self.name

class worldGeneration():
    def __init__(self):
        self.blocks = []

    def worldGen(self):
        grassImg = pygame.image.load("grass.png")
        dirtImg = pygame.image.load("dirt.png")


        for x in range(0, 640, 32):
            block1 = block(grassImg, 0 + x ,416, x)
            self. blocks.append(block1)
            for i in range(416, 480, 32):
                block1 = block(dirtImg, 0 + x ,448, x)
                self.blocks.append(block1)
    def getBlocks(self, x):
        return self.blocks[x]
    def draw(self, x, screen):
        screen.blit(self.blocks[x])


def gameUpdate(screen, world):

    screen.fill((147,200,250))
    for x in range (0, len(world.blocks)):
        world.draw(x, screen)
    pygame.display.flip()


def gameLoop():

    height = 480
    width = 640

    screen = pygame.display.set_mode((width, height))
    clock = pygame.time.Clock()

    world = worldGeneration()
    world.worldGen()

    running = True

    while running:
        clock.tick(60)
        pygame.display.set_caption(str(clock.get_fps()))

        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                running = False

            if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
                running = False


        gameUpdate(screen, world)


gameLoop()

2 个答案:

答案 0 :(得分:0)

这样做:

  1. 创建一个曲面()

  2. 逐块进行Blit,相应地更改坐标

  3. 获取屏幕表面()并在结果中进行blit

  4. 翻转()显示!

答案 1 :(得分:0)

而不是

class worldGeneration():
    def __init__(self):
        self.blocks = []

    def worldGen(self):
        grassImg = pygame.image.load("grass.png")
        dirtImg = pygame.image.load("dirt.png")


        for x in range(0, 640, 32):
            block1 = block(grassImg, 0 + x ,416, x)
            self. blocks.append(block1)
            for i in range(416, 480, 32):
                block1 = block(dirtImg, 0 + x ,448, x)
                self.blocks.append(block1)
    def getBlocks(self, x):
        return self.blocks[x]
    def draw(self, x, screen):
        screen.blit(self.blocks[x])

你可以做到

class worldGeneration():
    def __init__(self):
        self.blocks = []

    def worldGen(self):
        grassImg = pygame.image.load("grass.png")
        dirtImg = pygame.image.load("dirt.png")


        for x in range(0, 640, 32):
            block1 = block(grassImg, 0 + x ,416, x)
            self. blocks.append(block1)
            for i in range(416, 480, 32):
                block1 = block(dirtImg, 0 + x ,448, x)
                self.blocks.append(block1)
    def getBlocks(self, x):
        return self.blocks[x]
    def draw(self, x, screen):
        self.blocks[x].draw(screen)

blitSurface作为第一个参数,您提供了block个实例。这是错误消息告诉您的内容。但是,由于您已经在draw上实施了block方法,因此只需使用它,因为它可以满足您的需求。

此外,您可以进一步改变

def gameUpdate(screen, world):

    screen.fill((147,200,250))
    for x in range (0, len(world.blocks)):
        world.draw(x, screen)
    pygame.display.flip()

简单地

def gameUpdate(screen, world):

    screen.fill((147,200,250))
    world.draw(screen)
    pygame.display.flip()

并将worldGeneration.draw更改为

def draw(self, screen):
    for block in self.blocks:
        block.draw(screen)

无需获取len的{​​{1}},循环遍历blocks,并在您只需使用{range进行迭代时将索引传递给该函数{1}}循环。

相关问题