繁体   English   中英

使用Macports安装pygame

[英]Installing pygame using Macports

我正在通过macports安装pygame。 我遵循了这样的指示

sudo port install py26-game

这样编写pygame代码:

#image settings
bif = "bg.jpg"
mif = "ball.jpg"

#Basic Settings for pygame
import pygame, sys
from pygame.locals import *

pygame.init()

#Create a screen and load the images
screen = pygame.display.set_mode((640,360),0,32) #size,flag,bit

background = pygame.image.load(bif).convert()
mouse_c = pygame.image.load(mif).convert_alpha()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(background,(0,0))

    x,y = pygame.mouse.get_pos()
    x -= mouse_c.get_width()/2
    y -= mouse_c.get_height()/2

    screen.blit(mouse_c,(x,y))

    pygame.display.update()

我使用崇高文字2构建游戏,但出现此错误:

Traceback (most recent call last):
  File "PygameTutorial.py", line 6, in <module>
    import pygame, sys
ImportError: No module named pygame

在我的终端中,尝试相同并且有相同的错误:

>>> import pygame
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pygame

如何解决呢?

您需要确保使用的是MacPorts版本的Python,而不是内置版本。 假设您的MacPorts安装位于/opt/local ,则需要执行以下操作:

  1. 编辑您的~/.profile并在最底部添加以下行:

     export PATH=/opt/local/bin:$PATH 

    重新启动您的终端会话,在命令行上运行python ,然后查看是否可以导入pygame。

  2. 如果可行,请使用JSON语法在Sublime中打开一个新文件,然后粘贴以下内容:

     { "cmd": ["/opt/local/bin/python", "-u", "$file"], "file_regex": "^[ ]*File \\"(...*?)\\", line ([0-9]*)", "selector": "source.python" } 

    将文件保存为Packages/User目录中的Python2.sublime-build ,其中Packages是通过选择Sublime Text 2 -> Preferences -> Browse Packages...打开的文件夹,它应为~/Library/Application Support/Sublime Text 2/Packages 然后,当你想建立一个Python项目,选择Tools -> Build System -> Python2和打击⌘B来构建。

    另外,如果要使用Tools -> Build System -> Automatic设置,则可以编辑原始Python.sublime-build文件。 如上打开您的Packages文件夹,转到Python目录,然后在Sublime中打开Python.sublime-build 将内容更改为以上内容(基本上,只需将"cmd": ["python", ...更改为"cmd": ["/opt/local/bin/python", ...并保存。请注意,仅适用于Sublime Text 2;如果使用的是ST3,则需要安装PackageResourceViewer才能从压缩的Python.sublime-package文件中提取Python.sublime-build文件。

检查目录中“ site-packages”文件夹上是否有pygame:

Python27/lib/site-packages

如果您发现类似py26-game ,则有时包含该文件的文件的名称与“ pygame”不同

尝试将导入更改为该名称。

暂无
暂无

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

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