Search code examples
pythonpygamecx-freeze

Pygame cx_freeze Syntax Error


I have made a game using 2 python files and pygame, and I am trying to use cx_freeze to turn it into an .exe.

Here are the 2 scripts:

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()

And Enemy.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]

I typed up the setup.py to compile the scripts:

import cx_Freeze

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

I typed in the proper Python console command, but I got an error:

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>

Can someone tell me what is wrong with my files?


Solution

  • You are using Python 3.2:

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

    and yet the nmovie.py file in the pygame package has Python 2.x syntax for print:

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

    print is a function in Python 3.x and needs to be called with parenthesis:

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

    This means that the version of pygame which you are using is for Python 2.x and is therefore incompatible with Python 3.x. You need to do either one of two things:

    1. Rewrite your code in Python 2.x and then run setup.py with the Python 2.x executable.

    2. Use the Python 3.x version of pygame. You can download it over on their website.