Python Pygame2Exe错误,我可以';不固定

Python Pygame2Exe错误,我可以';不固定,python,python-2.7,pygame,runtime-error,py2exe,Python,Python 2.7,Pygame,Runtime Error,Py2exe,我做了一个“游戏”。我喜欢玩它,我想把它分发给我的朋友,而不必在他们的计算机上安装Python和Pygame 我对Py2Exe和Pyinstaller做了很多研究。我看了很多教程、修复和错误,但似乎没有一个对我有帮助 Pyinstaller是无用的,因为它不喜欢Pygame中的字体,并且Py2exe不会编译内置模块,所以我发现Pygame2exe只是一个用于Py2exe的预制安装脚本,其中包括Pygame和字体。它应该构建良好,但exe无法使用。。。我得到一个错误: “微软Visual C++运

我做了一个“游戏”。我喜欢玩它,我想把它分发给我的朋友,而不必在他们的计算机上安装Python和Pygame

我对Py2Exe和Pyinstaller做了很多研究。我看了很多教程、修复和错误,但似乎没有一个对我有帮助

Pyinstaller是无用的,因为它不喜欢Pygame中的字体,并且Py2exe不会编译内置模块,所以我发现Pygame2exe只是一个用于Py2exe的预制安装脚本,其中包括Pygame和字体。它应该构建良好,但exe无法使用。。。我得到一个错误:

“微软Visual C++运行库库< /P> 运行时错误

程序C:…\dist\Worm Game.exe

此应用程序已请求运行时在异常情况下终止 方式。请联系应用程序的支持团队以获取更多信息 信息。”

我就是不明白。。。为什么我不能编译这个游戏

以下是使用Python 2.7制作的游戏代码:

import pygame
import random
import os

pygame.init()

class Worm:
    def __init__(self, surface):
        self.surface = surface
        self.x = surface.get_width() / 2
        self.y = surface.get_height() / 2
        self.length = 1
        self.grow_to = 50
        self.vx = 0
        self.vy = -1
        self.body = []
        self.crashed = False
        self.color = 255, 255, 0

    def event(self, event):
        if event.key == pygame.K_UP:
            if self.vy != 1:
                self.vx = 0
                self.vy = -1
            else:
                a = 1
        elif event.key == pygame.K_DOWN:
            if self.vy != -1:
                self.vx = 0
                self.vy = 1
            else:
                a = 1
        elif event.key == pygame.K_LEFT:
            if self.vx != 1:
                self.vx = -1
                self.vy = 0
            else:
                a = 1
        elif event.key == pygame.K_RIGHT:
            if self.vx != -1:
                self.vx = 1
                self.vy = 0
            else:
                a = 1

    def move(self):
        self.x += self.vx
        self.y += self.vy
        if (self.x, self.y) in self.body:
            self.crashed = True
        self.body.insert(0, (self.x, self.y))
        if (self.grow_to > self.length):
            self.length += 1
        if len(self.body) > self.length:
            self.body.pop()

    def draw(self):
        x, y = self.body[0]
        self.surface.set_at((x, y), self.color)
        x, y = self.body[-1]
        self.surface.set_at((x, y), (0, 0, 0))

    def position(self):
        return self.x, self.y

    def eat(self):
        self.grow_to += 25

class Food:
    def __init__(self, surface):
        self.surface = surface
        self.x = random.randint(10, surface.get_width()-10)
        self.y = random.randint(10, surface.get_height()-10)
        self.color = 255, 255, 255

    def draw(self):
        pygame.draw.rect(self.surface, self.color, (self.x, self.y, 3, 3), 0)

    def erase(self):
        pygame.draw.rect(self.surface, (0, 0, 0), (self.x, self.y, 3, 3), 0)

    def check(self, x, y):
        if x < self.x or x > self.x +3:
            return False
        elif y < self.y or y > self.y +3:
            return False
        else:
            return True

    def position(self):
        return self.x, self.y

font = pygame.font.Font(None, 25)
GameName = font.render("Worm Eats Dots", True, (255, 255, 0))
GameStart = font.render("Press Any Key to Play", True, (255, 255, 0))

w = 500
h = 500
screen = pygame.display.set_mode((w, h))


GameLoop = True
while GameLoop:
    MenuLoop = True
    while MenuLoop:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.KEYDOWN:
                MenuLoop = False
        screen.blit(GameName, (180, 100))
        screen.blit(GameStart, (155, 225))
        pygame.display.flip()

    screen.fill((0, 0, 0))
    clock = pygame.time.Clock()
    score = 0
    worm = Worm(screen)
    food = Food(screen)
    running = True

    while running:
        worm.move()
        worm.draw()
        food.draw()

        if worm.crashed:
            running = False
        elif worm.x <= 0 or worm.x >= w-1:
            running = False
        elif worm.y <= 0 or worm.y >= h-1:
            running = False
        elif food.check(worm.x, worm.y):
            score += 1
            worm.eat()
            print "Score %d" % score
            food.erase()
            food = Food(screen)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.KEYDOWN:
                worm.event(event)

        pygame.display.flip()
        clock.tick(200)

    if not os.path.exists("High Score.txt"):
        fileObject = open("High Score.txt", "w+", 0)
        highscore = 0
    else:
        fileObject = open("High Score.txt", "r+", 0)
        fileObject.seek(0, 0)
        highscore = int(fileObject.read(2))
    if highscore > score:
        a = 1
    else:
        fileObject.seek(0, 0)
        if score < 10:
            fileObject.write("0"+str(score))
        else:
            fileObject.write(str(score))
        highscore = score
    fileObject.close()
    screen.fill((0, 0, 0))
    ScoreBoarda = font.render(("You Scored: "+str(score)), True, (255, 255, 0))
    if highscore == score:
        ScoreBoardb = font.render("NEW HIGHSCORE!", True, (255, 255, 0))
        newscore = 1
    else:
        ScoreBoardb = font.render(("High Score: "+str(highscore)), True, (255, 255, 0))
        newscore = 0
    Again = font.render("Again?", True, (255, 255, 0))
    GameOver = font.render("Game Over!", True, (255, 255, 0))
    screen.blit(GameName, (180, 100))
    screen.blit(GameOver, (200, 137))
    screen.blit(ScoreBoarda, (190, 205))
    if newscore == 0:
        screen.blit(ScoreBoardb, (190, 235))
    elif newscore == 1:
        screen.blit(ScoreBoardb, (175, 235))
    screen.blit(Again, (220, 365))
    pygame.draw.rect(screen, (0, 255, 0), (200, 400, 40, 40), 0)
    pygame.draw.rect(screen, (255, 0, 0), (260, 400, 40, 40), 0)
    LEFT = font.render("L", True, (0, 0, 0))
    RIGHT = font.render("R", True, (0, 0, 0))
    screen.blit(LEFT, (215, 415))
    screen.blit(RIGHT, (275, 415))
    pygame.display.flip()
    loop = True
    while loop:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if x > 200 and x < 240 and y > 400 and y < 440:
                    loop = False
                elif x > 260 and x < 300 and y > 400 and y < 440:
                    GameLoop = False
                    loop = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    loop = False
                elif event.key == pygame.K_RIGHT:
                    GameLoop = False
                    loop = False

    screen.fill((0, 0, 0))
pygame.quit()
导入pygame
随机输入
导入操作系统
pygame.init()
类蠕虫:
def u u初始(自,表面):
self.surface=曲面
self.x=surface.get_width()/2
self.y=surface.get_height()/2
self.length=1
自我成长到=50
self.vx=0
self.vy=-1
self.body=[]
self.crash=False
self.color=255、255、0
def事件(自身、事件):
如果event.key==pygame.K_UP:
如果self.vy!=1:
self.vx=0
self.vy=-1
其他:
a=1
elif event.key==pygame.K_向下:
如果self.vy!=-1:
self.vx=0
self.vy=1
其他:
a=1
elif event.key==pygame.K_左:
如果self.vx!=1:
self.vx=-1
self.vy=0
其他:
a=1
elif event.key==pygame.K_RIGHT:
如果self.vx!=-1:
self.vx=1
self.vy=0
其他:
a=1
def移动(自我):
self.x+=self.vx
self.y+=self.vy
如果self.body中有(self.x,self.y):
self.crash=True
self.body.insert(0,(self.x,self.y))
如果(self.grow_to>self.length):
自我长度+=1
如果len(self.body)>self.length:
self.body.pop()
def牵引(自):
x、 y=自我主体[0]
self.surface.set_在((x,y),self.color)
x、 y=自身身体[-1]
自曲面设置_在((x,y),(0,0,0))
def位置(自身):
返回self.x,self.y
def eat(self):
自我成长到+=25
类别食物:
def u u初始(自,表面):
self.surface=曲面
self.x=random.randint(10,surface.get_width()-10)
self.y=random.randint(10,surface.get_height()-10)
self.color=255、255、255
def牵引(自):
pygame.draw.rect(self.surface,self.color,(self.x,self.y,3,3),0)
def擦除(自):
pygame.draw.rect(self.surface,(0,0,0),(self.x,self.y,3,3),0)
def检查(自身、x、y):
如果xself.x+3:
返回错误
如果yself.y+3:
返回错误
其他:
返回真值
def位置(自身):
返回self.x,self.y
font=pygame.font.font(无,25)
GameName=font.render(“蠕虫吃点”,True,(255,255,0))
GameStart=font.render(“按任意键播放”,True,(255,255,0))
w=500
h=500
screen=pygame.display.set_模式((w,h))
GameLoop=True
而GameLoop:
MenuLoop=True
而Menulop:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
elif event.type==pygame.KEYDOWN:
MenuLoop=False
屏幕。blit(游戏名,(180100))
屏幕。blit(游戏开始,(155225))
pygame.display.flip()
屏幕填充((0,0,0))
clock=pygame.time.clock()
分数=0
worm=worm(屏幕)
食物=食物(屏幕)
运行=真
运行时:
worm.move()
worm.draw()
食物
如果worm.0崩溃:
运行=错误
elif worm.x=w-1:
运行=错误
elif worm.y=h-1:
运行=错误
elif食品检查(worm.x、worm.y):
分数+=1
虫子吃
打印“分数%d”%Score
食物
食物=食物(屏幕)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
elif event.type==pygame.KEYDOWN:
蠕虫事件(事件)
pygame.display.flip()
时钟滴答(200)
如果不存在os.path.exists(“High Score.txt”):
fileObject=open(“High Score.txt”,“w+”,0)
高分=0
其他:
fileObject=open(“High Score.txt”,“r+”,0)
fileObject.seek(0,0)
highscore=int(fileObject.read(2))
如果highscore>score:
a=1
其他:
fileObject.seek(0,0)
如果分数<10:
fileObject.write(“0”+str(分数))
其他:
fileObject.write(str(score))
高分
fileObject.close()
屏幕填充((0,0,0))
ScoreBoarda=font.render((“您的分数:+str(分数)),True,(255,255,0))
如果highscore==分数:
ScoreBoardb=font.render(“新高分!”,True,(255,255,0))
新闻核心=1
其他:
ScoreBoardb=font.render((“高分:+str(高分)),True,(255,255,0))
新闻核心=0
reach=font.render(“reach?”,True,(255,255,0))
GameOver=font.render(“游戏结束!”,True,(255,255,0))
屏幕。blit(游戏名,(180100))
屏幕。blit(GameOver,(200137))
银幕布利特(记分牌A,(190205))
如果newscore==0:
屏幕。blit(记分板B,(190235))
elif新闻核心==1:
from distutils.core import setup
import py2exe
setup(windows=['source_static.py'], options={
          "py2exe": {
              "excludes": ["OpenGL.GL", "Numeric", "copyreg", "itertools.imap", "numpy", "pkg_resources", "queue", "winreg", "pygame.SRCALPHA", "pygame.sdlmain_osx"],
              }
          }
      )
font1 = pygame.font.SysFont(None, 13)
font1 = pygame.font.SysFont("Arial", 13)