我有一个项目,我必须使用pygame创建一个4路分屏。在这个屏幕上我必须在每个屏幕上绘制相同的图像,只是有不同的图像视图。我只是无法弄清楚如何使用pygame创建这个4路分屏。
我需要像上面那样分割我的屏幕,这样我就可以在每个部分上绘制我的点。
我一直在四处寻找,我找不到这样的东西,所以任何帮助都会很棒 感谢
答案 0 :(得分:1)
没有拆分屏幕的功能。但是你可以直接在屏幕上绘制4个视图,或者你可以在4个表面(pygame.Surface)和屏幕上的blit表面上绘制。
答案 1 :(得分:1)
除了渲染到显示器的表面(可能称为screen
之外),您还应创建另一个表面,其中所有“动作”都被绘制。然后,您可以为屏幕的每个象限使用Rect
对象,该对象代表“相机”(假设每个象限不一定需要显示完全相同的图像)。当您回退到screen
时,您可以使用每个相机Rect
对象来选择要绘制到特定象限的游戏空间的一部分。
# canvas will be a surface that captures the entirety of the "action"
canvas = pygame.Surface((800, 600))
# the following are your "camera" objects
# right now they are taking up discrete and even portions of the canvas,
# but the idea is that they can move and possibly cover overlapping sections
# of the canvas
p1_camera = pygame.Rect(0,0,400,300)
p2_camera = pygame.Rect(400,0,400,300)
p3_camera = pygame.Rect(0,300,400,300)
p4_camera = pygame.Rect(400,300,400,300)
在每次更新时,您将使用这些“相机”对象将画布的各个部分blit返回到screen
曲面。
# draw player 1's view to the top left corner
screen.blit(canvas, (0,0), p1_camera)
# player 2's view is in the top right corner
screen.blit(canvas, (400, 0), p2_camera)
# player 3's view is in the bottom left corner
screen.blit(canvas, (0, 300), p3_camera)
# player 4's view is in the bottom right corner
screen.blit(canvas, (400, 300), p4_camera)
# then you update the display
# this can be done with either display.flip() or display.update(), the
# uses of each are beyond this question
display.flip()
答案 2 :(得分:0)
由于您正在寻找一种方法将屏幕分成4个部分并在其上绘制一些点,我建议创建原始“画布”图像的4个subsurface
表面以方便使用。
这些表面将充当您的播放器(分屏)画布,可以轻松修改。
这样就可以使用标准化坐标来进行玩家特定的绘图。
假设您设置了screen
曲面
# Image(Surface) which will be refrenced
canvas = pygame.Surface((800, 600))
# Camera rectangles for sections of the canvas
p1_camera = pygame.Rect(0,0,400,300)
p2_camera = pygame.Rect(400,0,400,300)
p3_camera = pygame.Rect(0,300,400,300)
p4_camera = pygame.Rect(400,300,400,300)
# subsurfaces of canvas
# Note that subx needs refreshing when px_camera changes.
sub1 = canvas.subsurface(p1_camera)
sub2 = canvas.subsurface(p2_camera)
sub3 = canvas.subsurface(p3_camera)
sub4 = canvas.subsurface(p4_camera)
现在使用这些标准化坐标绘制任何地下
# Drawing a line on each split "screen"
pygame.draw.line(sub2, (255,255,255), (0,0), (0,300), 10)
pygame.draw.line(sub4, (255,255,255), (0,0), (0,300), 10)
pygame.draw.line(sub3, (255,255,255), (0,0), (400,0), 10)
pygame.draw.line(sub4, (255,255,255), (0,0), (400,0), 10)
# draw player 1's view to the top left corner
screen.blit(sub1, (0,0))
# player 2's view is in the top right corner
screen.blit(sub2, (400, 0))
# player 3's view is in the bottom left corner
screen.blit(sub3, (0, 300))
# player 4's view is in the bottom right corner
screen.blit(sub4, (400, 300))
# Update the screen
pygame.display.update()
请注意,对次表面像素的修改也会影响画布。我建议您阅读subsurfaces上的完整文档。