首页 > 解决方案 > 当我运行 Pygame 时,它​​什么也没做

问题描述

我有一个 MacOS Catalina 10.15.4 和 Python 3.8.1。我终于通过终端安装了 pygame 1.9.6,下载了 homebrew、xcode 和 xquartz 并将其导入我的程序中。我是一名学生,并按照项目说明进行操作,因此所有代码都已提供给我,并说应该显示一个 pygame 窗口。当我在我的 Dock 上运行 Python 图标时,它会上下弹跳,并在 shell 中打印“来自 pygame 社区的游戏控制台已初始化 pygame 1.9.6 Hello。https : //www.pygame.org/contribute.html ”。我试图阅读和弄乱哪些版本可以使用什么,但我对我应该做的其他事情感到很迷茫。谢谢您的帮助!这是我的代码:

print("Game console initialized")

import pygame

pygame.init()

screenSize = width,height = 240,180
display = pygame.display.set_mode(screenSize)

starImage = pygame.image.load("star.gif")
starBox = starImage.get_rect()

speed = [3,5]

keepPlaying = True
while keepPlaying:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepPlaying = False


    starBox = starBox.move(speed)

    if starBox.left<0 or starBox.right > width:
        speed[0] = - speed[0]


    if starBox.top <0 or starBox.bottom > height:
        speed[1] = - speed[1]

    black = 0,0,0
    display.fill(black)

    display.blit(starImage, starBox)
    pygame.display.flip()

    pygame.time.delay(100)
pygame.display.quit()
pygame.quit()

标签: pythonpygame

解决方案


推荐阅读