首先,我在macOS Mojave 10.14.1上使用pygame 1.9.4。
此外,我正在使用PyCharm IDE并使用python 3.7.1。
我认为尝试pygame会很有趣,但绘制一个简单的矩形或图像也行不通。
这是我尝试用于绘制矩形的代码,绘制与文件位于同一目录中的图像,并用白色填充窗口。这些都不起作用。
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Snake")
image = pygame.image.load("java_logo.png")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
win.fill((0, 255, 0))
pygame.draw.rect(win, (255, 0, 0), (50, 50, 50, 50))
win.blit(image, (200, 200))
pygame.display.flip()
pygame.quit()
编辑:
这是使用的图像:https://i.imgur.com/OoSHkXX.png
这是已知的问题Pygame not compatible with MacOS Mojave #555
根据问题的评论,它仅在pygame 1.9.5中修复
UPD:它应该适用于python 3.6.5
Pygame仍然没有在OS X Mojave 10.14.3上运行Python 3.7.3,pygame 1.9.5
因此pygame 1.9.5无法解决问题。
在一个简单的测试程序(见下文)窗口打开时,窗口标题有效,但没有绘制任何内容。
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((612,792))
pygame.display.set_caption("This works!")
red = (255,0,0)
screen.fill(red)
# point (0,0) is at upper left
pygame.draw.lines(screen,black,False,[(100,100),(150,200),(200,100)], 1)
pygame.display.update() #nothing is drawn
while (True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit();
我想你忘记了pygame.display.update()来更新屏幕。此外,对于pygame.draw.rect(win,(255,0,0),(50,50,50,50)),这意味着您绘制一个左上角为(50,50)的矩形(前两部分)宽度和长度各50像素。 (最后两部分)。