[英]Pygame screen shows black, using Mac
I know there are several questions already about this topic, but unfortunately I haven't found the answer(s) working for me...我知道关于这个话题已经有几个问题了,但不幸的是我还没有找到适合我的答案......
I'm trying to do a tic tac toe and even it is running, the screen shows black or black with broken green lines...我正在尝试进行井字游戏,即使它正在运行,屏幕也显示黑色或黑色并带有断绿线...
I already tried a different pygame version, didn't help.我已经尝试了不同的 pygame 版本,没有帮助。 At the moment I am using: Pygame 2.0.3 (SDL 2.0.16, Python 3.8.3)
目前我正在使用:Pygame 2.0.3(SDL 2.0.16,Python 3.8.3)
My code:我的代码:
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()
You never call draw_grid that fills you screen你永远不会调用填满你屏幕的 draw_grid
Try:尝试:
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()
The black screen isn't a problem with your pygame version.黑屏不是你的 pygame 版本的问题。 You aren't actually calling the function anywhere.
您实际上并未在任何地方调用该函数。 This means that the
draw_grid()
function isn't executing it's code.这意味着
draw_grid()
函数没有执行它的代码。
To fix this, either type draw_grid()
above the while run:
loop to execute it once before the game starts.要解决此问题,请在
while run:
循环上方键入draw_grid()
以在游戏开始前执行一次。 Alternatively, you can type draw_grid()
in the while run:
loop to draw the tic tac toe screen every frame.或者,您可以在
while run:
循环中键入draw_grid()
以每帧绘制井字游戏屏幕。
The draw_grid()
command is what executes the code in the draw_grid()
function. draw_grid()
命令用于执行draw_grid()
函数中的代码。
For more info on functions: https://www.w3schools.com/python/python_functions.asp有关函数的更多信息: https : //www.w3schools.com/python/python_functions.asp
I hope this helped.我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.