简体   繁体   English

PyGame-无法将一个表面涂抹到另一个表面

[英]PyGame - Unable to blit one surface onto another

I am simply trying to create two Surfaces, fill them and then blit one onto the other. 我只是试图创建两个曲面,填充它们,然后将其中一个蒙上另一个。 However, the second Surface never renders on top of the first. 但是,第二个Surface永远不会在第一个Surface之上渲染。 If I blit the second surface onto the display Surface, it renders fine. 如果将第二个曲面涂抹到显示器的Surface上,它将呈现良好的效果。 Not sure if there is a restriction on layering surfaces (other than the display) on top of each other. 不确定在彼此顶部的分层表面(显示器除外)是否有限制。 Here is my code: 这是我的代码:

import pygame, sys
from pygame.locals import *

pygame.init()

windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('Hello world!')
windowSurface.fill((255,255,255))

layer1 = pygame.Surface((100,100))
layer1.fill((0,255,0))

layer2 = pygame.Surface((50,50))
layer2.fill((255, 0, 0))

windowSurface.blit(layer1, (0,0))
layer1.blit(layer2, (0,0))

pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
  • I've tried using both the update() and flip() 我试过同时使用update()和flip()
  • When I write layer2 to the the display (windowSurface) it works just fine. 当我将layer2写入显示(windowSurface)时,它工作得很好。

Thanks for any suggestions! 感谢您的任何建议!

Your problem is that you have layer1.blit(layer2, (0,0)) after windowSurface.blit(layer1, (0,0)) which means you're blitting layer2 to layer1 after layer1 is already done blitting to the window. 您的问题是,在windowSurface.blit(layer1, (0,0)) layer1.blit(layer2, (0,0))之后有layer1.blit(layer2, (0,0)) windowSurface.blit(layer1, (0,0))这意味着您已经在将layer1拖到窗口之后将layer2拖到layer1。 All you need to do is cut layer1.blit(layer2, (0,0)) and paste it ABOVE windowSurface.blit(layer1, (0,0)) so that it executes first. 您需要做的就是剪切layer1.blit(layer2, (0,0))并将其粘贴到windowSurface.blit(layer1, (0,0))以便首先执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM