gpt4 book ai didi

python - 如何在屏幕边缘包裹 blit?

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:22 27 4
gpt4 key购买 nike

给定一张图片,如何将其“包裹”在屏幕上?

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

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

这有可能吗(在 pygame 中)?

最佳答案

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

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

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

关于python - 如何在屏幕边缘包裹 blit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28326911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com