简体   繁体   English

使用Macports安装pygame

[英]Installing pygame using Macports

I am installing pygame via macports. 我正在通过macports安装pygame。 I followed the instructions as such, 我遵循了这样的指示

sudo port install py26-game

Wrote the pygame code as such: 这样编写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()

I am using sublime text 2 to build the game, but I have this error: 我使用崇高文字2构建游戏,但出现此错误:

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

In my terminal, tried the same and have the same error: 在我的终端中,尝试相同并且有相同的错误:

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

How to solve this? 如何解决呢?

You need to ensure that you're using the MacPorts version of Python, and not the built-in one. 您需要确保使用的是MacPorts版本的Python,而不是内置版本。 Assuming your MacPorts installation is in /opt/local , you'll need to do the following: 假设您的MacPorts安装位于/opt/local ,则需要执行以下操作:

  1. Edit your ~/.profile and include this line at the very bottom: 编辑您的~/.profile并在最底部添加以下行:

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

    Restart your Terminal session, run python on the command line, and see if you can import pygame. 重新启动您的终端会话,在命令行上运行python ,然后查看是否可以导入pygame。

  2. If that works, open a new file in Sublime with JSON syntax and paste in the following contents: 如果可行,请使用JSON语法在Sublime中打开一个新文件,然后粘贴以下内容:

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

    Save the file in your Packages/User directory as Python2.sublime-build where Packages is the folder opened by selecting Sublime Text 2 -> Preferences -> Browse Packages... - it should be ~/Library/Application Support/Sublime Text 2/Packages . 将文件保存为Packages/User目录中的Python2.sublime-build ,其中Packages是通过选择Sublime Text 2 -> Preferences -> Browse Packages...打开的文件夹,它应为~/Library/Application Support/Sublime Text 2/Packages Then, when you want to build a Python project, select Tools -> Build System -> Python2 and hit B to build. 然后,当你想建立一个Python项目,选择Tools -> Build System -> Python2和打击⌘B来构建。

    Alternatively, if you want to use the Tools -> Build System -> Automatic setting, you can edit the original Python.sublime-build file. 另外,如果要使用Tools -> Build System -> Automatic设置,则可以编辑原始Python.sublime-build文件。 Open your Packages folder as above, go to the Python directory, and open Python.sublime-build in Sublime. 如上打开您的Packages文件夹,转到Python目录,然后在Sublime中打开Python.sublime-build Change the contents to those above (basically, just change "cmd": ["python", ... to "cmd": ["/opt/local/bin/python", ... and save. Please note that this only works with Sublime Text 2; if you're using ST3 you'll need to install PackageResourceViewer to extract the Python.sublime-build file from the zipped Python.sublime-package file. 将内容更改为以上内容(基本上,只需将"cmd": ["python", ...更改为"cmd": ["/opt/local/bin/python", ...并保存。请注意,仅适用于Sublime Text 2;如果使用的是ST3,则需要安装PackageResourceViewer才能从压缩的Python.sublime-package文件中提取Python.sublime-build文件。

Check if you have pygame on the "site-packages" folder in the directory : 检查目录中“ site-packages”文件夹上是否有pygame:

Python27/lib/site-packages

Sometimes that name of the file that contains it is different from "pygame", if you find something like py26-game 如果您发现类似py26-game ,则有时包含该文件的文件的名称与“ pygame”不同

try changing the import to that name. 尝试将导入更改为该名称。

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

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