【问题标题】:why does my pygame load into a blank screen on mac?为什么我的 pygame 在 mac 上加载到空白屏幕?
【发布时间】:2017-08-16 03:16:59
【问题描述】:

由于某种原因,当我在 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

【问题讨论】:

  • 缩进# Update下面的七行。

标签: python macos pygame


【解决方案1】:

在您的 while 循环中没有进行精灵更新和渲染调用。

正如 cmets 中所指出的,您需要缩进 # 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()

【讨论】:

    猜你喜欢
    • 2022-10-18
    • 2015-05-02
    • 2022-01-14
    • 2015-06-21
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2021-04-26
    相关资源
    最近更新 更多