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

مهن*_*ليل 5 python pygame

pygame.surface.scroll()我刚刚从 pygame 文档中发现和理解的内容scroll()是用于移动表面,而不需要再次重建背景以覆盖旧表面,就像pygame.rect.move_ip()表面一样。

无论如何,我不知道如何使用它,而且 pygame 文档中的示例对我来说很难理解,只要我是初学者,经过很长时间的搜索,我找不到任何有用的东西来理解如何使用它。

这是我的代码。

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))
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)
    screen.blit(image,(boxx,boxy))
    pygame.display.update()
    clock.tick(60)
Run Code Online (Sandbox Code Playgroud)

Kev*_*lch 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)
Run Code Online (Sandbox Code Playgroud)

我不能确定滚动功能是否有效,请学习使用图像作为背景,以便您可以首先看到它移动。

  • @mønder Seed 如果这个答案对您有帮助,您应该接受它。 (2认同)