2017-06-28 200 views
0

我尝试使用一个非常简单的代码来创建一个使用python 3.6.1的矩形形状。我不确定我使用的代码是否会导致程序在每次使用时崩溃,或者是否有人可以给我提供pygame模块,我的cmd和我正在使用的代码之间可能发生的情况的建议。 screenshot of windowsPython 3.6 pygame崩溃

+0

@Alastair假,pygame中要求用户有和无限''while'循环来模拟连续事件循环 – abccd

+1

@jayardot,尽管代码非常简单,但我们希望您将代码复制并粘贴到问题本身(以避免误读,并防止托管的风险现场采取行动图像)。 – abccd

+0

什么崩溃?你为什么认为这里发生了崩溃? – user2357112

回答

0

当我运行你的代码时,窗口加载并显示矩形,但是,我认为你的问题涉及试图退出程序。要做到这一点,你需要有一个事件循环,将杀人游戏,当用户退出了游戏循环:

import pygame 

pygame.init() 

isrunning = True 

window = pygame.display.set_mode((500, 400)) 

while isrunning: 
    for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       isrunning = False 
    pygame.draw.rect(window, (255, 0, 0), (0, 0, 50, 30)) #here, access the rect method 

    pygame.display.update() 
+0

我的歉意,我读错了。请参阅我最近的编辑。 – Ajax1234

+0

这个修复了,谢谢! – jayardot

+0

很高兴帮助。请考虑接受此答案,以便其他用户知道您的问题已得到解答。谢谢。 – Ajax1234

0

您将需要一个背景颜色来填充屏幕。如果你想黑色,你仍然需要手动设置背景颜色为黑色。

import pygame 

pygame.init() 
window = pygame.display.set_mode((500, 400)) 

while True: 
    window.fill(0, 0, 0) 
    pygame.draw.rect(window, (255, 0, 0), (0, 0, 50, 30)) 

    pygame.display.flip() 

如果添加帧频并添加事件循环,会更好。

import pygame 

pygame.init() 
window = pygame.display.set_mode((500, 400)) 
clock = pygame.time.Clock() 

while True: 
    window.fill(0, 0, 0) 
    pygame.event.pump() # use pump if you’re not planning to catch any event 

    pygame.draw.rect(window, (255, 0, 0), (0, 0, 50, 30)) 
    pygame.display.flip() 
    clock.tick(50) # add the frame rate, should always be > 30 

而作为一般的提示,使用flip()如果你没有传递任何矩形参数update()

0
import pygame 
pygame.init() 
window = pygame.display.set_mode((500, 400)) 
while True: 
    pygame.draw.rect(window, (255,0,0), 
     (0, 0, 50, 30)) 
    pygame.display.update() 

继承人的代码

+0

单击编辑按钮编辑您的帖子,不要发布非回答作为答案,但非常适合您粘贴您的代码。请删除这个答案。 – abccd