0

我可以通过命令行和所有其他程序导入 pygame,但它会在我的十字准线程序中引发错误:

Traceback(最近一次调用最后一次):文件“C:\Users\Family\Desktop\pys\Crosshairs(2).py”,第 1 行,在 import pygame ImportError: No module named 'pygame'

我不知道它在说什么,但我认为这可能是 windows vista 是一个废话孔(vista 非常有故障),但我不太确定。有谁知道问题是什么?如果你需要它,这里是代码:

import pygame
import math
import sys

WHITE    = (255, 255, 255)
BLACK    = (  0,   0,   0)
RED = (255, 0, 0)
BGCOLOR = WHITE
WINDOWWIDTH = 640
WINDOWHEIGHT = 480

class Control(object):
    def __init__(self):
        self.bullet_holes = []
        self.screen = pg.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
        self.done = False
        self.clock = pg.time.Clock()

    def update(self):
        vis = False
        pygame.mouse.get_visible(vis)
        self.mousex,self.mousey = pg.mouse.get_pos()
        self.screen.fill(BGCOLOR)
        pygame.draw.circle(self.screen, RED, (320,240),50,10)
        pygame.draw.circle(self.screen, WHITE, (320,240),40,10)
        pygame.draw.circle(self.screen, RED, (320,240),30,10)
        pygame.draw.circle(self.screen, WHITE, (320,240),20,10)
        pygame.draw.circle(self.screen, RED, (320,240),10,10)
        pygame.draw.line(self.screen, BLACK, (self.mousex - 2000, self.mousey), 
                                             (self.mousex + 2000, self.mousey))
        pygame.draw.line(self.screen, BLACK, (self.mousex, self.mousey - 2000),
                                             (self.mousex, self.mousey + 2000))
        for bullet_pos in self.bullet_holes:
            pygame.draw.circle(self.screen,BLACK,bullet_pos,5)

    def event_loop(self):
        for event in pg.event.get():
            if event.type ==  pg.QUIT or (event.type == pg.KEYUP and
                                          event.key==pg.K_ESCAPE):
                self.done = True
            elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
                self.bullet_holes.append(event.pos)
                pygame.image.save(self.screen,'Highscores.png')

    def main_loop(self):
        while not self.done:
            self.update()
            self.event_loop()
            pygame.display.flip()
            self.clock.tick(60)


if __name__ == '__main__':
    game = Control()
    game.main_loop()
    pygame.quit()
    sys.exit()
4

2 回答 2

1

您的 pygame 模块不在路径中,因此无法找到。通过移动它或使用 PySys_SetPath() 来纠正它。

于 2013-11-12T20:09:12.977 回答
0

你可以用sys.path.insert命令做到这一点:

import sys
path_to_folder = os.path.abspath("c:\\Your\\destination\\folder")
sys.path.insert(0, path_to_folder)
于 2015-11-18T10:01:05.167 回答