Pygame - 只能看到一个滚动背景

时间:2017-04-21 19:40:50

标签: python python-2.7 animation pygame

所以我尝试使用pygame添加滚动背景,我已经完成了here在另一个问题上回答的内容。 不幸的是,我只能看到其中一个背景滚动。 这是它的样子:
this guy

主文件(存在bg类的地方)

#!/usr/bin/python
VERSION = "0.1"
import os, sys, player
from os import path

try:
    import pygame
    from pygame.locals import *
except ImportError, err:
    print 'Could not load module %s' % (err)
    sys.exit(2)

# main variables
WIDTH, HEIGHT, FPS = 700, 400, 30

# initialize game
pygame.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("FlappyTroll - Python2.7")

# background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))

img_dir = path.join(path.dirname(__file__), 'img')
class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)
        self.width = WIDTH
        self.height = HEIGHT
        self.image = pygame.image.load(path.join(img_dir,image_file)).convert_alpha()
        self.image = pygame.transform.scale(self.image,(self.width,self.height))
        self.rect = self.image.get_rect()
        #self.rect.left, self.rect.top = location
        self.rect.x, self.rect.y = location
        self.speedx = 5
    def update(self):
        self.rect.x -= self.speedx
        if self.rect.x <= -self.width:
            self.rect.x = WIDTH

bg = Background('FlappyTrollbg.jpg', [0,0])
bg2 = Background('FlappyTrollbg.jpg', [WIDTH,0])

# blitting
screen.blit(background,(0,0))
pygame.display.flip()

# clock for FPS settings
clock = pygame.time.Clock()


def main():
    all_sprites = pygame.sprite.Group()
    creature = pygame.sprite.Group()
    creature.add(player.FlappyTroll())
    # variable for main loop
    running = True
    # init umbrella
    # event loop
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False


    #    gets_hit = pygame.sprite.spritecollide(umb, all_sprites, True)
    #    if gets_hit:
    #        newDrop()

        #screen.blit(background,(0,0))
        bg.update()
        bg2.update()
        screen.fill([255, 255, 255])
        screen.blit(bg.image, bg.rect)
        screen.blit(bg2.image, bg.rect)
        # clear

        # update
        creature.update()

        # draw
        creature.draw(screen)

        # flip the table
        pygame.display.flip()
    pygame.quit()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:-1)

问题解决了

好吧,在考虑了.blit()函数的作用之后,我面对如此努力以至于创建了一个新维度...... 我所要做的就是将两张图片都放在bgs = pygame.sprite.Group()然后bgs.update() + bgs.draw(screen)中。 enter image description here