如何在 PyGame 中滚动背景表面? [英] How to scroll the background surface in PyGame?

查看:25
本文介绍了如何在 PyGame 中滚动背景表面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在基于 pyGame 的随机地图生成器中设置滚动.但是,我无法弄清楚如何防止显示器涂抹"背景,而不仅仅是滚动背景.我为 Stack Overflow 制作了一个仅显示彩色方块的 MCV 示例.它有同样的问题.

如何让屏幕滚动而不拖尾?

这是我滚动一点后地图生成器显示的内容:

MCV 示例:

""" 这个例子只显示彩色方块."""导入pygame导入系统定义主():pygame.init()屏幕 = pygame.display.set_mode((800, 800))x_pos = 0y_pos = 0黄色 = (255, 255, 0)蓝色 = (0, 0, 255)# 绘制黄色和蓝色方块的图案对于范围内的 y(100):对于范围内的 x(100):如果 (y + x) % 2 == 0:pygame.draw.rect(屏幕,黄色,(x_pos,y_pos,50, 50))别的:pygame.draw.rect(屏幕,蓝色,(x_pos,y_pos,50, 50))x_pos += 50y_pos += 50x_pos = 0while True: # <-- pyGame 循环事件 = pygame.event.poll()按下 = pygame.key.get_pressed()# 处理窗口关闭如果 event.type == pygame.QUIT:休息# 处理滚动如果按下[pygame.K_UP]:screen.scroll(0, 2)elif 按下 [pygame.K_DOWN]:屏幕滚动(0,-2)elif 按下 [pygame.K_LEFT]:screen.scroll(2, 0)elif 按下 [pygame.K_RIGHT]:screen.scroll(-2, 0)# 更新窗口显示的内容pygame.display.update()pygame.quit()系统退出(0)如果 __name__ == "__main__":# 运行 pyGame 循环主要的()

初始 MCVE 图像

滚动后的 MCVE 显示:

解决方案

I am trying to set up scrolling in my pyGame-based random map generator. However, I'm having trouble figuring out how to keep the display from "smearing" the background instead of just scrolling across it. I have made a MCV Example just for Stack Overflow that only displays colored squares. It has the same problem.

How can I make the display scroll without smearing the screen?

This is what the map generator displays after I scroll around a bit:

MCV Example:

""" This example just displays colored squares. """

import pygame
import sys


def main():

    pygame.init()

    screen = pygame.display.set_mode((800, 800))

    x_pos = 0
    y_pos = 0
    yellow = (255, 255, 0)
    blue = (0, 0, 255)

    # draw pattern of yellow and blue squares
    for y in range(100):
        for x in range(100):

            if (y + x) % 2 == 0:
                pygame.draw.rect(screen, yellow, (x_pos, y_pos, 50, 50))
            else:
                pygame.draw.rect(screen, blue, (x_pos, y_pos, 50, 50))

            x_pos += 50

        y_pos += 50
        x_pos = 0

    while True:  # <-- the pyGame loop

        event = pygame.event.poll()
        pressed = pygame.key.get_pressed()

        # handle window closing
        if event.type == pygame.QUIT:
            break

        # handle scrolling
        if pressed[pygame.K_UP]:
            screen.scroll(0, 2)
        elif pressed[pygame.K_DOWN]:
            screen.scroll(0, -2)
        elif pressed[pygame.K_LEFT]:
            screen.scroll(2, 0)
        elif pressed[pygame.K_RIGHT]:
            screen.scroll(-2, 0)

        # updates what the window displays
        pygame.display.update()

    pygame.quit()
    sys.exit(0)


if __name__ == "__main__":
    # runs the pyGame loop
    main()

Initial MCVE image

MCVE Display After Scrolling:

解决方案

pygame.Surface.scroll() doesn't do what you expect. It doesn't seamless roll the surface. See the documentation:

scroll() Shift the surface image in place

[...] Areas of the surface that are not overwritten retain their original pixel values. [...]

You've to write your own scroll functions, which place the part of the surface, which was shifted out, at the other side of thee surface.

e.g. write 2 functions (scrollX and scrollY), which can roll along an axis:

def scrollX(screenSurf, offsetX):
    width, height = screenSurf.get_size()
    copySurf = screenSurf.copy()
    screenSurf.blit(copySurf, (offsetX, 0))
    if offsetX < 0:
        screenSurf.blit(copySurf, (width + offsetX, 0), (0, 0, -offsetX, height))
    else:
        screenSurf.blit(copySurf, (0, 0), (width - offsetX, 0, offsetX, height))

def scrollY(screenSurf, offsetY):
    width, height = screenSurf.get_size()
    copySurf = screenSurf.copy()
    screenSurf.blit(copySurf, (0, offsetY))
    if offsetY < 0:
        screenSurf.blit(copySurf, (0, height + offsetY), (0, 0, width, -offsetY))
    else:
        screenSurf.blit(copySurf, (0, 0), (0, height - offsetY, width, offsetY))

if pressed[pygame.K_UP]:
    scrollY(screen, 2)
elif pressed[pygame.K_DOWN]:
    scrollY(screen, -2)
elif pressed[pygame.K_LEFT]:
    scrollX(screen, 2)
elif pressed[pygame.K_RIGHT]:
    scrollX(screen, -2)

这篇关于如何在 PyGame 中滚动背景表面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆