2015-01-04 3 views
1

2 개의 파이썬 파일과 pygame을 사용하여 게임을 만들었으며 cx_freeze을 사용하여 .exe으로 변환하려고합니다.Pygame cx_freeze 구문 오류

Move.py :

import pygame 
from Enemy import * 

pygame.init() 

gameDisplay = pygame.display.set_mode((800,600)) 

white = [255,255,255] 
black = [0,0,0] 
red = [200,0,0] 
green = [0,200,0] 

gamex = 800 

gamey = 600 

enemynumber = [5,7,10] 

level = 0 

blocksize = 10 

clock = pygame.time.Clock() 

smallfont = pygame.font.SysFont("impactregular", 25) 
medfont = pygame.font.SysFont("impactregular", 50) 
largefont = pygame.font.SysFont("impactregular", 75) 

pygame.display.update() 

FPS = 30 

EnemyList = [] 

def makeEnemy(enemyCount): 

    for i in range(0,enemyCount): 
     EnemyList.append(Enemy()) 
     EnemyList[i].setup(i*100,200,False,0) 

def enemyCalc(blockx,blocky,enemyCount): 
    for i in range(0,enemyCount): 
     pygame.draw.rect(gameDisplay, red, EnemyList[i].calc(blockx,blocky)) 

def GameOver(): 
    gameover = True 
    while gameover: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_r: 
        level = 0 
        gameLoop() 
       if event.key == pygame.K_q: 
        pygame.quit() 
        quit() 
     gameDisplay.fill(white) 
     message_to_screen("GAME OVER", red, -100, "large") 
     message_to_screen("Press R to try again, or Q to quit", black, 100, "medium") 
     pygame.display.update() 

def Win(): 
    win = True 
    while win: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_r: 
        level = 0 
        gameLoop() 
       if event.key == pygame.K_q: 
        pygame.quit() 
        quit() 
     gameDisplay.fill(white) 
     message_to_screen("YOU WIN!", green, -100, "large") 
     message_to_screen("Press R to play again, or Q to quit", black, 100, "medium") 
     pygame.display.update() 

def Intro(): 
    intro = True 
    while intro: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_r: 
        level = 0 
        gameLoop() 
       if event.key == pygame.K_q: 
        pygame.quit() 
        quit() 
     gameDisplay.fill(white) 
     message_to_screen("GO UP: The Game", red, -100, "large") 
     message_to_screen("Get to the top of the screen,", 
          black, 100, "medium") 
     message_to_screen("but avoid the red squares!", 
          black, 150, "medium") 
     message_to_screen("Press R to play, or Q to quit", black, 250, "medium") 
     pygame.display.update() 

def text_objects(text,color,size): 
    if size == "small": 
     textSurface = smallfont.render(text, True, color) 
    elif size == "medium": 
     textSurface = medfont.render(text, True, color) 
    elif size == "large": 
     textSurface = largefont.render(text, True, color) 

    return textSurface, textSurface.get_rect() 

def message_to_screen(msg,color,offset=0, size="small"): 
    textSurf, textRect = text_objects(msg,color,size) 
    textRect.center = (gamex/2), ((gamey/2) + offset) 
    gameDisplay.blit(textSurf, textRect) 

def collision(playerx, playery, enemyCount): 
    for i in range(0,enemyCount): 
     enemyx = EnemyList[i].x 
     enemyy = EnemyList[i].y 
     if playerx == enemyx and playery == enemyy: 
      GameOver() 

def gameLoop(): 
    global EnemyList 
    global level 

    blockx = gamex/2 
    blocky = gamey/2 

    xchange = 0 
    ychange = 0 

    gameExit = False 

    makeEnemy(enemynumber[level]) 

    while not gameExit: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_RIGHT: 
        xchange = 10 
       if event.key == pygame.K_LEFT: 
        xchange = -10 
       if event.key == pygame.K_DOWN: 
        ychange = 10 
       if event.key == pygame.K_UP: 
        ychange = -10 
      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_RIGHT: 
        xchange = 0 
       elif event.key == pygame.K_LEFT: 
        xchange = 0 
       elif event.key == pygame.K_DOWN: 
        ychange = 0 
       elif event.key == pygame.K_UP: 
        ychange = 0 

     collision(blockx,blocky,enemynumber[level]) 
     if xchange == -10 and blockx == 0 or xchange == 10 and blockx == gamex-10: 
      xchange = 0 
     if ychange == 10 and blocky == gamey-10: 
      ychange = 0 
     if ychange == -10 and blocky == 0: 
      if level < 2: 
       level += 1 
       gameLoop() 
      else: 
       Win() 
     blockx += xchange 
     blocky += ychange 
     gameDisplay.fill(white) 
     message_to_screen("Level: "+str(level+1),black,-275) 
     enemyCalc(blockx,blocky,enemynumber[level]) 
     pygame.draw.rect(gameDisplay, black, [blockx,blocky,10,10]) 
     pygame.display.update() 
     clock.tick(FPS) 




Intro() 

그리고 Enemy.py :

: 나는 스크립트를 컴파일 setup.py를 입력

import pygame 

class Enemy: 

    def setup(self,x,y,dead,subclass): 
     if not dead: 
      self.x = x 
      self.y = y 
      self.count = 0 
      self.reverse = False 
      self.subclass = subclass 


    def calc(self, playerx, playery): 

     if self.count < 10 and self.reverse == False: 
      self.count += 1 
      self.x += 10 
     elif self.count == 10: 
      self.reverse = True 
      self.x += -10 
      self.count += -1 
     if self.count > 0 and self.reverse == True: 
      self.count += -1 
      self.x += -10 
     elif self.count == 0: 
      self.reverse = False 
      self.x += 10 
      self.count += 1 
     return [self.x,self.y,10,10] 

여기

는이 스크립트입니다
import cx_Freeze 

cx_Freeze.setup(
    name="Go Up: The Game", 
    options = {"build_exe": {"packages":["pygame","Enemy"]}}, 
    executables = [cx_Freeze.Executable("Move.py")] 
    ) 

적절한 Python 콘솔 명령을 입력했지만 오류가 발생했습니다.

C:\Users\Sean\Documents\Python\Object Test>c:/python32/python setup.py build 
running build 
running build_exe 
Traceback (most recent call last): 
    File "setup.py", line 6, in <module> 
    executables = [cx_Freeze.Executable("Move.py")] 
    File "c:\python32\lib\site-packages\cx_Freeze\dist.py", line 365, in setup 
    distutils.core.setup(**attrs) 
    File "c:\python32\lib\distutils\core.py", line 148, in setup 
    dist.run_commands() 
    File "c:\python32\lib\distutils\dist.py", line 917, in run_commands 
    self.run_command(cmd) 
    File "c:\python32\lib\distutils\dist.py", line 936, in run_command 
    cmd_obj.run() 
    File "c:\python32\lib\distutils\command\build.py", line 126, in run 
    self.run_command(cmd_name) 
    File "c:\python32\lib\distutils\cmd.py", line 313, in run_command 
    self.distribution.run_command(command) 
    File "c:\python32\lib\distutils\dist.py", line 936, in run_command 
    cmd_obj.run() 
    File "c:\python32\lib\site-packages\cx_Freeze\dist.py", line 235, in run 
    freezer.Freeze() 
    File "c:\python32\lib\site-packages\cx_Freeze\freezer.py", line 575, in Freeze 

    self.finder = self._GetModuleFinder() 
    File "c:\python32\lib\site-packages\cx_Freeze\freezer.py", line 330, in _GetMo 
duleFinder 
    finder.IncludePackage(name) 
    File "c:\python32\lib\site-packages\cx_Freeze\finder.py", line 581, in Include 
Package 
    self._ImportAllSubModules(module, deferredImports) 
    File "c:\python32\lib\site-packages\cx_Freeze\finder.py", line 220, in _Import 
AllSubModules 
    deferredImports) 
    File "c:\python32\lib\site-packages\cx_Freeze\finder.py", line 338, in _Intern 
alImportModule 
    parentModule, namespace) 
    File "c:\python32\lib\site-packages\cx_Freeze\finder.py", line 366, in _LoadMo 
dule 
    module.code = compile(codeString, path, "exec") 
    File "c:\python32\lib\site-packages\pygame\nmovie.py", line 15 
    print "Unable to find a working movie backend. Loading the dummy movie class 
..." 

^
SyntaxError: invalid syntax 

C:\Users\Sean\Documents\Python\Object Test> 

누군가 내 파일에 어떤 문제가 있다고 말할 수 있습니까?

+0

영화를 찾을 수 없습니다. 패키지 설치와 관련하여 몇 가지 문제가있을 수 있습니다. – rapvelopment

답변

1

당신은 파이썬 3.2을 사용하고 있습니다 :

C:\Users\Sean\Documents\Python\Object Test>c:/python32/python setup.py build 
               ^^^^^^^^ 

pygame 패키지에 아직 nmovie.py 파일

print 파이썬 2.x에서 구문이 있습니다

print "Unable to find a working movie backend. Loading the dummy movie class..." 
^^^^^^ 

print is a function in Python 3.x을 괄호로 호출 할 필요가 :

print("Unable to find a working movie backend. Loading the dummy movie class...") 
    ^                  ^

는 사용중인 pygame의 버전이 Python 2.x 용이므로 Python 3.x와 호환되지 않는다는 것을 의미합니다. 당신은 두 가지 중 하나를 수행해야합니다

  1. 파이썬 2.x에서의 코드를 다시 작성하고 파이썬 2.x를 실행에 setup.py을 실행

    .

  2. pygame의 Python 3.x 버전을 사용하십시오. website에서 다운로드 할 수 있습니다.