【问题标题】:Running a game which uses Pygame doesn't work in pycharm, but will work when run from terminal运行使用 Pygame 的游戏在 pycharm 中不起作用,但从终端运行时会起作用
【发布时间】:2020-01-08 20:03:18
【问题描述】:

运行基本游戏时,如下图,在pycharm中运行时屏幕不会更新。甚至不会加载背景颜色。但是,当从终端运行时,程序运行良好。为什么是这样?

注意:我已经使用 pycharm 包管理器安装了 Pygame

import sys, pygame
pygame.init()

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("ship.bmp")
ballrect = ball.get_rect()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    ballrect = ballrect.move(speed)
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()

【问题讨论】:

标签: python pygame pycharm


【解决方案1】:

看起来像解释器问题,转到文件->设置->项目解释器->添加(+符号)并安装 PyGame,或选择默认(运行它的环境)环境。应该工作

【讨论】:

    猜你喜欢
    • 2014-12-20
    • 2023-03-24
    • 1970-01-01
    • 2016-05-16
    • 2011-09-19
    • 2021-06-12
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多