Pygame2exe字体相关的运行时错误

时间:2015-11-23 07:22:46

标签: python pygame py2exe py2app

我收到了pygame2exe的错误,我无法解决。我已经查看了字体问题,但无法使用该方法解决它。

C:\Users\Quentin\Documents\school\SDD\12\tictactoe\dist\tictactoemain1.exe\zipextimporter.py:82: RuntimeWarning: import display: No module named _view
(ImportError: No module named _view)
C:\Users\Quentin\Documents\school\SDD\12\tictactoe\dist\tictactoemain1.exe\zipextimporter.py:82: RuntimeWarning: import draw: No module named _view
(ImportError: No module named _view)
C:\Users\Quentin\Documents\school\SDD\12\tictactoe\dist\tictactoemain1.exe\zipextimporter.py:82: RuntimeWarning: import image: No module named _view
(ImportError: No module named _view)
C:\Users\Quentin\Documents\school\SDD\12\tictactoe\dist\tictactoemain1.exe\zipextimporter.py:82: RuntimeWarning: import pixelcopy: No module named _view
(ImportError: No module named _view)
C:\Users\Quentin\Documents\school\SDD\12\tictactoe\dist\tictactoemain1.exe\zipextimporter.py:82: RuntimeWarning: import transform: No module named _view
(ImportError: No module named _view)

这是我提出的错误

我的代码

#imports
import py2exe
import random
import pygame
from pygame.locals import *
import sys
import time
from Tkinter import *
import time
#initiating the pygame module
pygame.init()

gameover = True


#sets the variable clock to the pygame.time.Clock function
clock = pygame.time.Clock()
#sets the window title
pygame.display.set_caption("Tic Tac Toe")

#all the stuff for the main menu

p1 = "Player 1"
p2 = "Player 2"
CurrentRounds = "1"
#filehandle = open("player names", "w")
#filehandle.write(p1 + "\n" + p2)

menunames = []

rowlist = []
columnlist = []

player1test = True

computerselect = "False"

#sets the feild, has all of the player moves
boxselect = [["","",""],
             ["","",""],
             ["","",""]]

#all of the positions of the boxes in x and y coordinates
boxpos = [[[268,268],[268,430],[268,592]],
          [[430,268],[430,430],[430,592]],
          [[592,268],[592,430],[592,592]]]

boxXpos = 0
boxYpos = 0

playerturn = "PLAYER1"

"""all of the events for the feild
    initialises the feild
    tests if win
    updates and draws feild ect"""
class MainEvent():#pygame.sprite.Sprite): #not sure what the comented out code does


    #initialising all of the images and text. only test cases atm
    def __init__(self, x, y, rounds):

        self.winner = False
        self.player1score = 0
        self.player2score = 0

        self.CurrentRounds = int(rounds)
        self.MainFont = pygame.font.Font("micross.ttf", 50)

        self.Outline = pygame.Rect((x,y), (500,500))
        self.Inline = pygame.Rect((x,y), (486,486))


        #the boxes in pygame rects
        TL = pygame.Rect((x,y), (160,160))
        TC = pygame.Rect((x,y), (160,160))
        TR = pygame.Rect((x,y), (160,160))
        ML = pygame.Rect((x,y), (160,160))
        MM = pygame.Rect((x,y), (160,160))
        MR = pygame.Rect((x,y), (160,160))
        BL = pygame.Rect((x,y), (160,160))
        BM = pygame.Rect((x,y), (160,160))
        BR = pygame.Rect((x,y), (160,160))

        #assigning the boxes
        self.boxtotal = [[TL,TC,TR],
                         [ML,MM,MR],
                         [BL,BM,BR]]

        #all of the lables, player name, scores, rounds
        self.Lable1 = self.MainFont.render(p1,1, (0,0,0))
        self.Lable2 = self.MainFont.render(p2,1, (0,0,0))
        self.Lable3 = self.MainFont.render(str(CurrentRounds),1, (0,0,0))
        self.Lable4 = self.MainFont.render(str(self.player1score),1, (0,0,0))
        self.Lable5 = self.MainFont.render(str(self.player2score),1, (0,0,0))

        self.x = x
        self.y = y
        self.posaddx = 268
        self.posaddy = 106

    #draws everything that was inited. test cases atm
    def Draw(self, mousex, mousey, l):

        #the playing box
        self.Outline.centerx = 430
        self.Outline.centery = 430

        self.Inline.centerx = 430
        self.Inline.centery = 430

        #puts each of the squares in the game board onto the screen
        self.posaddx = 268
        self.posaddy = 268
        for row in self.boxtotal:
            for i in row:
                i.centerx = self.posaddx
                i.centery = self.posaddy
                self.posaddx += 162
            self.posaddx = 268
            self.posaddy += 162
        self.posaddy += 268

        #writes the lables onto the screen
        screen.blit(self.Lable1, (75,20))
        screen.blit(self.Lable2, (75,80))
        screen.blit(self.Lable3, (500,20))
        screen.blit(self.Lable4, (20,20))
        screen.blit(self.Lable5, (20,80))

        #puts the game board onto the screen
        self.Outline.clamp_ip(screen.get_rect())
        screen.fill((0,0,0),self.Outline)

        self.Inline.clamp_ip(screen.get_rect())
        screen.fill((255,255,255),self.Inline)

        #colours the squares to each players tiles
        countXcolour = 0
        countYcolour = 0
        for row in self.boxtotal:
            countXcolour = 0
            for i in row:
                if boxselect[countYcolour][countXcolour] == "PLAYER1":
                    i.clamp_ip(screen.get_rect())
                    screen.fill((255,0,0),i)
                elif boxselect[countYcolour][countXcolour] == "PLAYER2":
                    i.clamp_ip(screen.get_rect())
                    screen.fill((0,0,255),i)
                elif boxselect[countYcolour][countXcolour] == "Winner":
                    i.clamp_ip(screen.get_rect())
                    screen.fill((0,255,0),i)
                else:
                    i.clamp_ip(screen.get_rect())
                    screen.fill((0,0,0),i)
                countXcolour += 1
            countYcolour += 1

    def update(self, turn, boxcheck):
        countx = 0
        county = 0

        #updates the score data
        self.Lable3 = self.MainFont.render(str(self.CurrentRounds),1, (0,0,0))
        self.Lable4 = self.MainFont.render(str(self.player1score),1, (0,0,0))
        self.Lable5 = self.MainFont.render(str(self.player2score),1, (0,0,0))

        #updates player turn colours
        if turn == "PLAYER1":
            self.Lable1 = self.MainFont.render(p1,1, (255,0,0))
            self.Lable2 = self.MainFont.render(p2,1, (0,0,0))
        else:
            self.Lable2 = self.MainFont.render(p2,1, (0,0,255))
            self.Lable1 = self.MainFont.render(p1,1, (0,0,0))

    def win(self, boxcheck):
        Tie = 0

        Game = False
        #checking if it is a tie. if none of the squares are blank then it is a tie and the variable called Tie will equal 0
        for row in boxselect:
            for i in row:
                if i == "":
                    Tie += 1
        if Tie == 0:
            Game = True


        rowcount = 0
        player1test_column1 = True
        player2test_column1 = True

        player1test_column2 = True
        player2test_column2 = True

        player1test_column3 = True
        player2test_column3 = True
        #testing for the player winning
        for row in boxselect:
            #checking the columns
            if row[0] != "PLAYER1" and player1test_column1 == True:
                player1test_column1 = False
            if row[0] != "PLAYER2" and player2test_column1 == True:
                player2test_column1 = False

            if row[1] != "PLAYER1" and player1test_column2 == True:
                player1test_column2 = False
            if row[1] != "PLAYER2" and player2test_column2 == True:
                player2test_column2 = False

            if row[2] != "PLAYER1" and player1test_column3 == True:
                player1test_column3 = False
            if row[2] != "PLAYER2" and player2test_column3 == True:
                player2test_column3 = False

            player1test = True
            player2test = True

            #checking if all of the items in one row are equal to PLAYER1 or PLAYER2
            for i in row:
                if i != "PLAYER1" and player1test == True:
                    player1test = False
                if i != "PLAYER2" and player2test == True:
                    player2test = False

            #adds the score for the winner
            if player1test == True:
                #print "player 1 wins"
                Game = True
                self.player1score += 1

                for i in self.boxtotal[rowcount]:
                    i.clamp_ip(screen.get_rect())
                    screen.fill((0,255,0),i)
                for i in range(len(boxselect[rowcount])):
                    boxselect[rowcount][i] = "Winner"

            if player2test == True:
                #print "player 2 wins"
                Game = True
                self.player2score += 1

                for i in self.boxtotal[rowcount]:
                    i.clamp_ip(screen.get_rect())
                    screen.fill((0,255,0),i)
                for i in range(len(boxselect[rowcount])):
                    boxselect[rowcount][i] = "Winner"
            #adding scores ect for columns
            rowcount += 1

        if player1test_column1 == True:
            for i in range(len(boxselect)):
                boxselect[i][0] = "Winner"
                #print i
                Game = True
            self.player1score += 1

        if player1test_column2 == True:
            for i in range(len(boxselect)):
                boxselect[i][1] = "Winner"
                #print i
                Game = True
            self.player1score += 1

        if player1test_column3 == True:
            for i in range(len(boxselect)):
                boxselect[i][2] = "Winner"
                #print i
                Game = True
            self.player1score += 1

        if player2test_column1 == True:
            for i in range(len(boxselect)):
                boxselect[i][0] = "Winner"
                #print i
                Game = True
            self.player2score += 1

        if player2test_column2 == True:
            for i in range(len(boxselect)):
                boxselect[i][1] = "Winner"
                #print i
                Game = True
            self.player2score += 1

        if player2test_column3 == True:
            for i in range(len(boxselect)):
                boxselect[i][2] = "Winner"
                #print i
                Game = True
            self.player2score += 1
        #changing the box from the winning player to "Winner"
        if boxselect[0][0] == "PLAYER1" and boxselect[1][1] == "PLAYER1" and boxselect[2][2] == "PLAYER1":
            boxselect[0][0] = "Winner"
            boxselect[1][1] = "Winner"
            boxselect[2][2] = "Winner"

            self.player1score += 1
            Game = True

        if boxselect[0][2] == "PLAYER1" and boxselect[1][1] == "PLAYER1" and boxselect[2][0] == "PLAYER1":
            boxselect[0][2] = "Winner"
            boxselect[1][1] = "Winner"
            boxselect[2][0] = "Winner"
            self.player1score += 1
            Game = True

        if boxselect[0][0] == "PLAYER2" and boxselect[1][1] == "PLAYER2" and boxselect[2][2] == "PLAYER2":
            boxselect[0][0] = "Winner"
            boxselect[1][1] = "Winner"
            boxselect[2][2] = "Winner"
            self.player2score += 1
            Game = True

        if boxselect[0][2] == "PLAYER2" and boxselect[1][1] == "PLAYER2" and boxselect[2][0] == "PLAYER2":
            boxselect[0][2] = "Winner"
            boxselect[1][1] = "Winner"
            boxselect[2][0] = "Winner"
            self.player2score += 1
            Game = True


        #updating rounds, reseting the feild
        if Game == True and self.CurrentRounds > 0:
            #print "Game won"
            self.CurrentRounds -= 1
            for row in range(len(boxselect)):
                for i in range(len(boxselect)):
                    boxselect[row][i] = ""
        if self.CurrentRounds == 0 or self.CurrentRounds == "Game over":
            self.CurrentRounds = "Game over"
            print "game over"
            sys.exit()
            #print "DEBUG 331", gameover


def details():
    #puts the tk data into a file
    writen1 = str(t.get(0.0, END))
    writen2 = str(t2.get(0.0, END))
    comp = str(var.get())
    Rounds = str(n.get())
    filehandle = open("player names", "w")
    filehandle.write(writen1)
    filehandle.write(writen2)
    filehandle.write(comp + "\n")
    filehandle.write(Rounds)
    filehandle.close

    main.destroy()
    main.quit()



""" All of the TK menu stuff is in here text boxes, checkbutton, scroll wheel and lables. the reason for using TK is because the pygame module does not have text boxes
    all of the names are pretty self explanetory"""
main = Tk()
main.title("Tic Tac Toe")


Lframe = Frame(main)
Lframe.pack(side="left")
var = IntVar()
Checkbutton(Lframe,
    text="Vs. computer",
    variable=var,
    ).pack()
t = Text(main,
        width = 40,
        height = 1)
t.pack(side="top")
t2 = Text(main,
        width = 40,
        height = 1)
t2.pack(side="top")

l1 = Label(main, text="number of rounds").pack(side="left", anchor="w")
n = Spinbox(main,
            from_=1,
            to=25,
            )
n.pack(side="left", anchor="e")

#insert numerical dial here. or text box that can only take numbers

Button(Lframe,
       text = "Run",
       width = 15,
       command=details,
       ).pack()

t.insert(INSERT, "Player 1")
t2.insert(INSERT, "Player 2")

main.mainloop()
"""end of all the TK menu stuff"""

filehandle = open("player names", "r")
#gets the data that was in the file and puts it into a list
for line in filehandle:
    menunames.append(line)
filehandle.close

p1 = menunames[0]
p1 = p1[:-1]
p2 = menunames[1]
p2 = p2[:-1]
Vs = menunames[2]
Vs = Vs[:-1]
Round = menunames[3]


#print Vs,p1,p2,Round
x = 250
y = 250

MainEventVar = MainEvent(250,250,Round)

name = []
keyreleased = True


event = pygame.event.poll()


#setting the display at a size of 700 by 700 pixel's and a colour of 255,255,255 rgb
screen = pygame.display.set_mode((700,700), 0, 32)
screen.fill((255,255,255), rect=None)

MainEventVar.update("PLAYER1",boxselect)



while True:
    #refreshes the screen
    screen.fill((255,255,255))

    #sets the refresh rate
    clock.tick(144)

    #gets the mouse position, left center and right mouse presses and asigns the pygame key function to keys for easiy use
    mx,my = pygame.mouse.get_pos()
    l,c,r = pygame.mouse.get_pressed()
    keys = pygame.key.get_pressed()

    x = y
    y += 1

    #runs the draw function
    MainEventVar.Draw(mx, my, l)

    #moves the ai
    if Vs == "1":
        if playerturn == "PLAYER2":
            pos1 = random.randint(0,2)
            pos2 = random.randint(0,2)

            while boxselect[pos1][pos2] != "":
                pos1 = random.randint(0,2)
                pos2 = random.randint(0,2)
            boxselect[pos1][pos2] = "PLAYER2"
            playerturn = "PLAYER1"
            MainEventVar.win(boxselect)

    #l is left mouse press, gets the player move
    #print "DEBUG 463", gameover
    if l and gameover == True:
        yPosTrue = 0
        xPosTrue = 0
        boxXpos = 0
        boxYpos = 0
        over = False
        #looping through the tiles
        for row in boxpos:
            boxXpos = 0
            for i in row:
                if mx in range(i[0]-81,i[0]+81) and my in range(i[1]-81,i[1]+81) and on == False:#on is used for a press release otherwise it would register multiple times
                    #print i[0]
                    over = True#is it over a tile
                    yPosTrue = boxYpos
                    xPosTrue = boxXpos
                    on = True#mouse has been pressed in the range of a box, stops it from being registered multiple times
                boxXpos += 1
            boxYpos += 1
        if over == True and boxselect[xPosTrue][yPosTrue] == "":#is it over a tile and is that tile empty
            boxselect[xPosTrue][yPosTrue] = playerturn#playerturn gives the value of player1 or 2 and is used to set up the turn system

            MainEventVar.win(boxselect)#sends it to the win function. this does more than checking if the game is won but that is its main function
            #swapping turns
            if playerturn == "PLAYER1":
                playerturn = "PLAYER2"
            else:
                playerturn = "PLAYER1"

    else:#linked with if l:
        on = False#if the mouse is not being pressed then on = false, this makes the variable toggle between preses

    #as the name suggests this updates stuff
    MainEventVar.update(playerturn,boxselect)

    #quits if escape key is pressed
    pygame.display.update()
    if keys[pygame.K_ESCAPE]:
        sys.exit()

    #event handle thing for pygame, its needed to quit the window even without pressing the quit button. just a weird pygame thing
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

我设法使用py2exe构建它,它将运行代码的tk部分,但是当加载pygame部分时它会崩溃。它似乎是py2exe给出的错误中的字体相关问题,但是在遵循了人们已经无法解决的其他问题的一些建议之后

2 个答案:

答案 0 :(得分:0)

这是一个相当常见的错误。添加"导入pygame._view"在源文件的顶部。这也适用于Mac OSX上的py2app。

这是处理同一问题的另一个线程: opening-an-exe-of-my-pygame-program-gives-me-import-errors

答案 1 :(得分:0)

旧版本的pygame会出现此问题。如果您从pygame download

更新为最新版本

这个问题将自行解决。 或者,您可以保留现有的pygame安装,并在导入内容的顶部插入import pygame._view