简体   繁体   中英

Running a game which uses Pygame doesn't work in pycharm, but will work when run from terminal

When running a basic game, such as the one below, the screen will not update when ran in pycharm. Not even the background colour will load. However when run from terminal the program works fine. Why is this?

Note: I have installed Pygame using the pycharm package manager

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()

Looks like interpreter problem, go to File->Settings->Project Interpreter->add(+ symbol) and install PyGame, or choose default(one on which it's running) enviroment. Should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM