cx_Freeze不会与pygame一起工作(已经尝试过“导入pygame._view”)

时间:2022-12-12 18:03:21

I'm trying to use the following setup.py:

我试着使用下面的setup.py:

from cx_Freeze import setup, Executable
exe = Executable(
script="stars.pyw",
)

setup(
    executables = [exe]
    )

To build the following Pygame example:

建立如下的Pygame示例:

import random, math, pygame
import pygame._view
from pygame.locals import *

#constants
WINSIZE = [640, 480]
WINCENTER = [320, 240]
NUMSTARS = 150


def init_star():
    "creates new star values"
    dir = random.randrange(100000)
    velmult = random.random()*.6+.4
    vel = [math.sin(dir) * velmult, math.cos(dir) * velmult]
    return vel, WINCENTER[:]


def initialize_stars():
    "creates a new starfield"
    stars = []
    for x in range(NUMSTARS):
        star = init_star()
        vel, pos = star
        steps = random.randint(0, WINCENTER[0])
        pos[0] = pos[0] + (vel[0] * steps)
        pos[1] = pos[1] + (vel[1] * steps)
        vel[0] = vel[0] * (steps * .09)
        vel[1] = vel[1] * (steps * .09)
        stars.append(star)
    move_stars(stars)
    return stars


def draw_stars(surface, stars, color):
    "used to draw (and clear) the stars"
    for vel, pos in stars:
        pos = (int(pos[0]), int(pos[1]))
        surface.set_at(pos, color)


def move_stars(stars):
    "animate the star values"
    for vel, pos in stars:
        pos[0] = pos[0] + vel[0]
        pos[1] = pos[1] + vel[1]
        if not 0 <= pos[0] <= WINSIZE[0] or not 0 <= pos[1] <= WINSIZE[1]:
            vel[:], pos[:] = init_star()
        else:
            vel[0] = vel[0] * 1.05
            vel[1] = vel[1] * 1.05


def main():
    "This is the starfield code"
    #create our starfield
    random.seed()
    stars = initialize_stars()
    clock = pygame.time.Clock()
    #initialize and prepare screen
    pygame.init()
    screen = pygame.display.set_mode(WINSIZE)
    pygame.display.set_caption('Stars')
    white = 255, 240, 200
    black = 20, 20, 40
    screen.fill(black)

    #main game loop
    done = 0
    while not done:
        draw_stars(screen, stars, black)
        move_stars(stars)
        draw_stars(screen, stars, white)
        pygame.display.update()
        for e in pygame.event.get():
            if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
                done = 1
                break
            elif e.type == MOUSEBUTTONDOWN and e.button == 1:
                WINCENTER[:] = list(e.pos)
        clock.tick(50)


# if python says run, then we should run
if __name__ == '__main__':
    main()

It's important to say I'm using a Pygame unoffical release: pygame-1.9.2pre.win-amd64-py3.3.‌exe

说我很重要使用Pygame unoffical释放:pygame-1.9.2pre.win-amd64-py3.3‌exe

But the script works just fine before compiling.

但是在编译之前,脚本可以正常工作。

1 个解决方案

#1


4  

Reposting as an answer:

新任命的回答:

That's an error we see occasionally, but haven't got round to fixing yet. You can work around it by adding a line to the script:

这是我们偶尔看到的错误,但还没有解决。您可以通过在脚本中添加一行来解决这个问题:

import re

#1


4  

Reposting as an answer:

新任命的回答:

That's an error we see occasionally, but haven't got round to fixing yet. You can work around it by adding a line to the script:

这是我们偶尔看到的错误,但还没有解决。您可以通过在脚本中添加一行来解决这个问题:

import re