2014-09-12 106 views
0

我正在通過macports安裝pygame。我跟着instructions因爲如此,使用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 

如何解決這個問題?

回答

1

您需要確保您使用的是MacPorts版本的Python,而不是內置版本。假設你的MacPorts安裝在/opt/local,你需要做到以下幾點:

  1. 編輯您的~/.profile,包括這條線在最底層:

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

    重新啓動終端會話,運行python在命令行上,看看你是否可以導入pygame。

  2. 如果這樣的作品,在崇高打開一個新的文件,JSON語法並粘貼到下面的內容:

    { 
        "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文件

0

檢查,如果你有在目錄中的「站點包」文件夾pygame的:

Python27/lib/site-packages 

有時包含它是由「pygame的」不同,如果你發現類似py26-game文件的該名

嘗試將導入更改爲該名稱。

+0

不可以。它被安裝爲'pygame'。 – MattDMo 2014-09-12 15:07:41