【问题标题】:Pygame screen shows black, using MacPygame屏幕显示黑色,使用Mac
【发布时间】:2021-11-06 21:27:58
【问题描述】:

我知道已经有几个关于这个主题的问题,但不幸的是我还没有找到适合我的答案...

我正在尝试进行井字游戏,即使它正在运行,屏幕显示黑色或黑色并带有绿色虚线......

我已经尝试了不同的 pygame 版本,但没有帮助。 目前我正在使用:Pygame 2.0.3 (SDL 2.0.16, Python 3.8.3)

我的代码:

import pygame
from pygame.locals import *

pygame.init()

screen_width = 300
screen_height = 300

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('tic tac toe')

def draw_grid():
    bg = (255, 0, 0)
    grid = (50, 50, 50)
    screen.fill(bg)
    for x in range(1, 3):
        pygame.draw.line(screen, grid, (0, x * 100), (screen_width, x * 100))
        pygame.draw.line(screen, grid, (x * 100, 0), (x * 100, screen_height))

run = True
while run:

    #add event handlers
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.display.update()

pygame.quit()

【问题讨论】:

  • draw_grid 永远不会被调用。但是,当我调用 draw_grid 时,程序运行正常。

标签: python pygame


【解决方案1】:

你永远不会调用填充你屏幕的 draw_grid

试试:

import pygame
from pygame.locals import *

pygame.init()

screen_width = 300
screen_height = 300

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('tic tac toe')

def draw_grid():
    bg = (255, 0, 0)
    grid = (50, 50, 50)
    screen.fill(bg)
    for x in range(1, 3):
        pygame.draw.line(screen, grid, (0, x * 100), (screen_width, x * 100))
        pygame.draw.line(screen, grid, (x * 100, 0), (x * 100, screen_height))

run = True
while run:

    #add event handlers
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    draw_grid()
    pygame.display.update()

pygame.quit()

【讨论】:

  • 建议不要回答错字问题。像这样的问题是题外话,不应该回答而是关闭,因为它们对其他贡献者没有帮助。这些问题将被关闭并最终删除。如果问题被删除,您获得的声望点将会丢失。
【解决方案2】:

黑屏不是你的pygame版本的问题。您实际上并没有在任何地方调用该函数。这意味着draw_grid() 函数没有执行它的代码。

要解决此问题,请在 while run: 循环上方键入 draw_grid() 以在游戏开始前执行一次。 或者,您可以在 while run: 循环中键入 draw_grid() 以在每一帧中绘制井字游戏屏幕。

draw_grid() 命令执行draw_grid() 函数中的代码。

More info on functions

【讨论】:

  • 建议不要回答错字问题。像这样的问题是题外话,不应该回答而是关闭,因为它们对其他贡献者没有帮助。这些问题将被关闭并最终删除。如果问题被删除,您获得的声望点数将会丢失。
猜你喜欢
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多