如何使用pygame.surface.scroll()?

时间:2017-06-17 23:42:24

标签: python pygame

我刚刚发现了pygame.surface.scroll()以及我从pygame文档中了解到的scroll()用于移动曲面而无需再次重建背景以覆盖旧曲面,就像{{1}但是对于表面。

无论如何,我不知道如何使用它,只要我是初学者就很难理解pygame文档中的例子,经过很长一段时间的搜索后,我无法找到任何有用的东西,以了解如何使用它。

这是我的代码。

pygame.rect.move_ip()

1 个答案:

答案 0 :(得分:2)

编辑:您的imagescreen变量是向后的。这也让你有些困惑我确定......

您的问题可能是您尝试滚动全黑背景。它可能是滚动的,你只是不知道它,因为你用blit()在屏幕上绘制的白框是静止的。

尝试使用可以看到滚动的内容,例如图像文件。如果要移动白框,可以添加计数器作为速度变量。阅读本文,然后运行它。

import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
speed = 5   # larger values will move objects faster
while True :
    screen.fill((0,0,0))
    for event in pygame.event.get():
        if event.type==pygame.QUIT :
            pygame.quit()
            quit()
    image.scroll(10,10)
    # I did modulus 720, the surface width, so it doesn't go off screen
    screen.blit(image,((boxx + speed) % 720, (boxy + speed) % 720))
    pygame.display.update()
    clock.tick(60)

我无法确定滚动功能是否正常,请学习使用图像作为背景,以便您可以先看到它移动。