简体   繁体   English

Pycharm 在运行 pygame 代码时显示异常

[英]Pycharm displays an anomally when running a pygame code

I am having a weird display when I run a simple Pygame.当我运行一个简单的 Pygame 时,我的显示很奇怪。 The game runs perfectly but the screen is weird.游戏运行完美,但画面诡异。

Here's the code:这是代码:

import pygame

pygame.init()

window = pygame.display.set_mode((500, 500))
pygame.display.set_caption('First Game')
pygame.display.update()


x = 50
y = 50
width = 40
height = 60
vel = 5

run = True

while run:
    pygame.time.delay(100)

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

    pygame.draw.rect(window, (255, 0, 0), (x, y, width, height))
pygame.quit()

在此处输入图像描述

You need to update the screen every frame.您需要每帧更新屏幕。 Put pygame.display.update() inside your main_loop , and remove it from original positon:pygame.display.update()放入您的main_loop中,并将其从原始位置中删除:

while run:
    pygame.time.delay(100)

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

    pygame.draw.rect(window, (255, 0, 0), (x, y, width, height))
    pygame.display.update()

This should work这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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