Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

python - Pygame2Exe Errors that I can't fix

I made a "Game". I love playing it, and I would like to distribute it to my friends without having to install Python and Pygame on their computers.

I did a lot of research on Py2Exe and Pyinstaller. I looked through many tutorials, fixes, errors, but none of them seem to help me.

Pyinstaller is useless because it doesn't like fonts in Pygame, and Py2exe wouldn't compile the built in modules, so I found Pygame2exe which is just a premade setup script for use with py2exe that includes pygame and fonts. It supposedly builds fine, but the exe is unusable... I get the error:

"Microsoft Visual C++ Runtime Library

Runtime Error!

Program C:...distWorm Game.exe

This application has requested the Runtime to terminate in an unusual way. Please contact the application's support team for more information."

I just don't get it... Why can't I compile this game!!!

Here is the game code, made with 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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I met this problem, too. After investigation, I found that the runtime error is caused by the font. I noticed that you also used None as the font name, too. Please remember that there's a notice for using the pygame2exe about the font, just below "Changes by arit:" at pygame2exe page, that we should use a "fontname.ttf" to replace None and place that fontname.ttf under the correct folder that the exe can find. For example, you can use "freesansbold.ttf" to replace None when creating font, and place freesansbold.ttf under the folder the exe file exists. Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...