如何在屏幕边缘包裹 blit?

How to wrap blit around screen edge?

给定一个图像,如何 "wrap" 那个图像环绕屏幕?

例如,如果您将图像的矩形样式对象设置在屏幕边缘下方 - 不可见的一半会​​ blit 到屏幕顶部。

imageRect.top=800 #Below screen edge
screen.blit(image,imageRect) #Blit on both sides of the screen

这是否有可能(在 pygame 中)?

我认为没有内置任何东西;只需弄清楚图像中的垂直和水平划分在哪里,然后执行多个 blits。

我在尝试侧边掉落和收集风格的游戏时遇到了同样的问题。经过进一步检查,我找到了一种用两张图片实现这一壮举的方法。下面是我使用的代码。

首先创建两个变量display_widthdisplay_height它们是你的游戏的宽度和高度window。

display_width = [width of window]
display_height = [height of window]

接下来为第一张和第二张图片的 'x' 位置再创建两个变量。然后在 obj_width 变量下声明以像素为单位的图像宽度。

img_x = ['x' position of first image]
img2_x = ['x' position of second image, works best with same 'x' as 'img_x' above]
obj_width = [width of img]

现在制作一个采用 img_ximg_y 的图像函数。 img_x 是局部变量,不要与其他 img_x.

混淆
def img(img_x,img_y):
    screen.blit([image file here], (img_x,img_y))

这是我在程序中使用的条件。随意复制和粘贴。

if img_x + img_width > display_width:
    img(img2_x, display_height * 0.8)
    img2_x = img_x - display_width
if img_x < 0:
    img(img2_x, display_height * 0.8)
    img2_x = img_x + display_width

if img2_x + bag_width > display_width:
    img(img_x, [where you want the image to appear, ex. display_height * 0.8])
    img_x = img2_x - display_width
if img2_x < 0:
    img(img_x, display_height * 0.8) #same as above
    img_x = img2_x + display_width

在此示例中,图像水平移动,如果您想要垂直移动,只需根据自己的喜好更改变量即可。我希望这能回答您的问题,如果有什么地方不能正常工作或者有任何拼写错误,请随时在下面发表评论。我知道这是大约一年前发布的问题,如果为时已晚,我很抱歉。

祝你好运,JC