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

2024-10-02 10:27:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚了解了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)

Tags: 文档imageimporteventdisplayfillscreensurface
1条回答
网友
1楼 · 发布于 2024-10-02 10:27:44

编辑:您的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)

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

相关问题 更多 >

    热门问题