简体   繁体   English

如何让 Pygame 1.9.2 与 Python 3.3 一起工作?

[英]How do I get Pygame 1.9.2 to work with Python 3.3?

I just installed Pygame 1.9.2 and am running Python 3.3.我刚刚安装了 Pygame 1.9.2 并且正在运行 Python 3.3。 I'm having trouble figuring out how to make it so that when I click the 'X' in my pygame window, the program closes.我无法弄清楚如何制作它,以便当我单击 pygame 窗口中的“X”时,程序会关闭。 I believe the following code works with Pygame 1.9.2 and Python 3.2, but is there any way to get it to work on Python 3.3?我相信以下代码适用于 Pygame 1.9.2 和 Python 3.2,但有什么方法可以让它在 Python 3.3 上工作? And what do I have to change to get the right effect?我必须改变什么才能获得正确的效果? Here is some simple code that illustrates what I mean:这是一些简单的代码,说明了我的意思:

# Drawing Lines

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Lines")

while True:
    for event in pygame.event.get():
        if event.type in (QUIT, KEYDOWN):
            sys.exit()

    screen.fill((0,0,200))

    #draw the line
    color = 255,255,0
    width = 8
    pygame.draw.line(screen, color, (100,100), (500,400), width)

    pygame.display.update()

When I run this, a screen appears with a diagonal line on it, just like it's supposed to, but when I click the 'X' in the top-right corner of the window to close the window, I get the following error message:当我运行它时,屏幕上会出现一条对角线,就像它应该的那样,但是当我单击窗口右上角的“X”关闭窗口时,我收到以下错误消息:

Traceback (most recent call last):
  File "C:\Python33\Exercise Programs\test_code\code\chap02\DrawingLines.py", line 13, in <module>
    sys.exit()
NameError: name 'sys' is not defined

I guess the line that says sys.exit() is supposed to close the window if this is run in mython 3.2, but it isn't doing this in Python 3.3.如果在 mython 3.2 中运行,我猜说 sys.exit() 的那行应该关闭窗口,但它在 Python 3.3 中没有这样做。 The window just freezes and I get that error message.窗口只是冻结,我收到该错误消息。 How do I achieve the desired effect in Python 3.3?如何在 Python 3.3 中达到预期效果? What line do I need to enter?我需要输入哪一行?

Yo didn't import sys.哟没有导入系统。 Just add只需添加

import sys

to your imports, then it should work.到您的进口,那么它应该工作。

The fact that you see a window with the drawing at all suggests that the program is running fine otherwise.您看到一个带有绘图的窗口的事实表明该程序运行良好,否则。

用于关闭项目窗口的实际命令是pygame.quit()

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

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