python - 为什么我的 pygame 在 Mac 上加载到空白屏幕?

标签 python macos pygame

出于某种原因,当我在 netbeans 中运行代码时,我只得到一个空白屏幕

import pygame
import random

WIDTH = 480
HEIGHT = 600
FPS = 60

# define colours
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((40, 50))
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT - 10
        self.speedx = 0

    def update(self):
        self.rect.x += self.speedx

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
# Game loop
running = True
while running:
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False

    # Update
all_sprites.update()

    # Draw / render
screen.fill(BLACK)
all_sprites.draw(screen)
# *after* drawing everything, flip the display
pygame.display.flip()

pygame.quit()

请帮助我。当我在netbeans中运行代码时,它可以工作,但是游戏所在的屏幕应该是空白的,当我关闭屏幕时,在关闭窗口之前会出现一个彩色框架。我正在使用 OSX Sierra

最佳答案

在 while 循环中未进行 Sprite 更新和渲染调用。

正如评论中指出的,您需要缩进# Update下面的七行。试试这个:

while running:
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False

    # Update
    all_sprites.update()

    # Draw / render
    screen.fill(BLACK)
    all_sprites.draw(screen)
    # *after* drawing everything, flip the display
    pygame.display.flip()

pygame.quit()

关于python - 为什么我的 pygame 在 Mac 上加载到空白屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45704570/

相关文章:

python - 如何在 Python/Plotly 等值线图中放大 map ?

python - 如何以编程方式检测 Scikit-learn 警告

JavaFX - javafx.print.PrinterJob.createPrinterJob() 在 mac 上始终为 null

css - 升级到山狮与全新安装后的滚动条问题

python - Pygame 显示位置

python - 为什么 event.type 函数返回名称错误?

python - 如何创建一个能够根据假设做出决策的深度神经网络?

python 映射/减少 : emit multiple keys values from single map lambda

Mavericks 升级后 OS X 上的 C++ 编译问题

python - Anaconda 中的物理模拟器