如何使用Pygame连续滚动背景?

时间:2013-06-21 16:51:04

标签: python pygame

我打算创建一个太空射击游戏,我希望我的背景与星星不断向下移动。你可以在下面看到我的代码。图片http://tinypic.com/r/9a8tj4/5

import pygame
import sys
import pygame.sprite as sprite

theClock = pygame.time.Clock()

background = pygame.image.load('background.gif')

background_size = background.get_size()
background_rect = background.get_rect()
screen = pygame.display.set_mode(background_size)
x = 0
y = 0
w,h = background_size
running = True

while running:
    screen.blit(background,background_rect)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    if(y > h):
        y = 0
    else:
        y += 5
    screen.blit(background,(x,y))
    pygame.display.flip()
    pygame.display.update()
    theClock.tick(10)

1 个答案:

答案 0 :(得分:4)

这就是我要做的事情:

使用背景图像两次一次在(0,0)处以及另一个在(0, - img.height)处对表面进行Blit然后将它们向下移动,当它们中的任何一个处于pos(0)时,img.heigth)再次将它放在pos(0, - img.height)。

import pygame
import sys
import pygame.sprite as sprite

theClock = pygame.time.Clock()

background = pygame.image.load('background.gif')

background_size = background.get_size()
background_rect = background.get_rect()
screen = pygame.display.set_mode(background_size)
w,h = background_size
x = 0
y = 0

x1 = 0
y1 = -h

running = True

while running:
    screen.blit(background,background_rect)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    y1 += 5
    y += 5
    screen.blit(background,(x,y))
    screen.blit(background,(x1,y1))
    if y > h:
        y = -h
    if y1 > h:
        y1 = -h
    pygame.display.flip()
    pygame.display.update()
    theClock.tick(10)