简体   繁体   English

Pygame:“pygame.Surface.fill()”后无法在屏幕上绘制任何内容

[英]Pygame: Can't draw anythin on screen after “pygame.Surface.fill()”

I'm trying to make a chess game, and I've encountered a problem: I'm trying to update the display for every 'tick' by using pygame.Surface.fill(black_sc).我正在尝试制作国际象棋游戏,但遇到了一个问题:我正在尝试使用 pygame.Surface.fill(black_sc) 更新每个“刻度”的显示。 But as a result, it seems I'm not able to draw anything on top of the now black screen:但结果,似乎我无法在现在的黑屏上绘制任何东西:

#GAME LOOP
while True:
    screen.fill(black_sc)
    
    #SQUARE DRAWING LOOP
    s_draw_loop()
    
    #DRAW THE PIECES
    draw_pieces()
    
    m_pos = pygame.mouse.get_pos()
    
    for x in pygame.event.get():
        if x.type == pygame.QUIT or pygame.key.get_pressed()[pygame.K_ESCAPE] == True:
            exit()
        
        if x.type == pygame.MOUSEBUTTONDOWN:
            for space in range(len(rect_database)):
                if rect_database[space].collidepoint(m_pos) == True:
                    print(space)
    
    pygame.display.flip()

Here's the s_draw_loop():这是 s_draw_loop():

class s_draw_loop():
    s_posx = 0
    s_posy = 0
    s_w = size[0] / 8
    s_h = size[1] / 8
    row = 0
    
    i = 0
    while i < 64:
        if i % 2 == 0:
            if row % 2 == 0:
                current_rect = pygame.Rect(s_posx, s_posy, s_w, s_h)
                screen.fill(white, current_rect)
            else:
                current_rect = pygame.Rect(s_posx, s_posy, s_w, s_h)
                screen.fill(black,current_rect)
            s_posx += s_w
            i += 1
        
        else:
            if row % 2 == 0:
                current_rect = pygame.Rect(s_posx, s_posy, s_w, s_h)
                screen.fill(black,current_rect)
            else:
                current_rect = pygame.Rect(s_posx, s_posy, s_w, s_h)
                screen.fill(white,current_rect)
            s_posx += s_w
            i += 1
        
        if i % 8 == 0:
            s_posx = 0
            s_posy += s_h
            row += 1

I'll spare you from the entire function drawing the pieces, unless you need it, but it's basically just using pygame.Surface.blit() to draw the pictures onto the display.除非你需要它,否则我将把你从整个 function 中绘制出来,但它基本上只是使用 pygame.Surface.blit() 将图片绘制到显示器上。

I've tried including the 'drawing functions' into the game-loop itself, but it seems to make no difference.我尝试将“绘图功能”包含在游戏循环本身中,但似乎没有什么区别。

Here's the output when including 'screen.fill(black_sc)'这是包含“screen.fill(black_sc)”时的 output

And here it is when commenting 'screen.fill(black_sc)' out这是在评论'screen.fill(black_sc)'时

It's because s_draw_loop is a class, not a function.这是因为s_draw_loop是 class,而不是 function。

The drawing code inside s_draw_loop is only executed once, before entering the game loop, when the python runtime reads the code of the class. s_draw_loop里面的绘图代码只执行一次,在进入游戏循环之前,当python运行时读取class的代码时。

Calling s_draw_loop() inside your game loop does actually nothing (except creating an useless instace of that class that does nothing).在你的游戏循环中调用s_draw_loop()实际上什么都不做(除了创建一个什么都不做的 class 的无用实例)。


Simply change简单地改变

class s_draw_loop():
    ...

to

def s_draw_loop():
    ...

so the code inside s_draw_loop gets executed every frame.因此s_draw_loop中的代码每帧都会执行。

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

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