给定一张图片,如何将其“包裹”在屏幕上?
例如,如果您将图像的 rect 样式对象设置在屏幕边缘下方 - 不可见的一半会 blit 到屏幕顶部。
imageRect.top=800 #Below screen edge
screen.blit(image,imageRect) #Blit on both sides of the screen
这有可能吗(在 pygame 中)?
请您参考如下方法:
当我尝试侧边掉落和收集风格的游戏时,我遇到了同样的问题。经过进一步检查,我找到了一种用两张图片实现这一壮举的方法。下面是我使用的代码。
首先创建两个变量 display_width 和 display_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_x 和img_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
