【发布时间】:2015-04-16 21:59:31
【问题描述】:
所以我一直在寻找如何在 windows 上运行 python 脚本,但我无法在虚拟机上使用 windows xp 64 位运行 pygame 脚本。我试过用不同版本的 python、pyinstaller、py2exe 和 cx_freeze 简单地运行它。
我的主要问题是我不确定问题到底是什么。例如:当我像平常一样运行脚本时(使用 python 而不是别的),脚本会打开一个 cmd 窗口,但会立即再次关闭它,但是当我尝试第一次将其转换为 .exe 文件时,我收到一条错误消息,指出该文件是有效的,但适用于另一种机器类型(很确定这与 32 位和 64 位 python 有关,但我不知道如何修复它),当我从 cmd 运行它时,我收到一个错误,说没有名为 pygame 的模块。 我还注意到,当简单地传输原始的 game.py 文件时,一些代码似乎会变得混乱并变成一条直线(我忘记了导致此错误的名称是什么)。
这是我尝试运行的游戏的完整代码:
import sys, pygame, pygame.mixer, time
from pygame.locals import *
pygame.init()
pygame.mouse.set_visible(0)
w = 1
while w == 1:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE:
sys.exit()
elif event.type == pygame.QUIT:
sys.exit()
shotgunsound = pygame.mixer.Sound("shotgun.wav")
size = width, height = 600,400
screen = pygame.display.set_mode(size)
bird = pygame.image.load("bird.png")
shotgun = pygame.image.load("fps_sprite_shotgun.png")
post = pygame.image.load("post.png")
ground = pygame.image.load("ground.png")
text2 = pygame.image.load("text2.png")
pygame.display.flip()
skyblue = 0,125,200
x = 0
y = 100
screen.blit(bird,(x,y))
z = 1
while z == 1:
mx,my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
sys.exit()
elif event.type == MOUSEBUTTONDOWN and mx > 400 and mx < 560 and my >100 and my <300:
shotgunsound.play()
z = 0
elif event.type == MOUSEBUTTONDOWN:
shotgunsound.play()
screen.fill(skyblue)
screen.blit(bird,(x,y))
screen.blit(shotgun,(mx-300,my-200))
screen.blit(post,(400,100))
screen.blit(ground,(0,0))
pygame.display.flip()
x = x+1
time.sleep(.01)
if x == 400:
screen.blit(text2,(0,0))
pygame.display.flip()
time.sleep(3)
sys.exit()
text1 = pygame.image.load("text1.png")
screen.blit(text1, (0,0))
pygame.display.flip()
n = 1
while n == 1:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE:
sys.exit()
elif event.type == pygame.QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
n = 0
x = 0
rpg = pygame.image.load("fps_sprite_rpg.png")
w = 0
mx,my = pygame.mouse.get_pos()
t = 1
mx,my = pygame.mouse.get_pos()
green = 0,100,0
building = pygame.image.load("building.png")
rpgsound = pygame.mixer.Sound("rpg.wav")
while t ==1:
mx,my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE:
sys.exit()
elif event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN and mx > 400 and mx < 600 and my > 100 and my < 300:
rpgsound.play()
t = 0
elif event.type == MOUSEBUTTONDOWN:
rpgsound.play()
screen.fill(green)
screen.blit(bird,(x,y))
screen.blit(building,(400,50))
screen.blit(rpg,(mx-300,my-200))
x = x+1
pygame.display.flip()
if x == 400:
screen.blit(text2,(0,0))
pygame.display.flip()
time.sleep(3)
sys.exit()
final = pygame.image.load("FINAL.png")
screen.blit(final,(0,0))
pygame.display.flip()
time.sleep(3)
sys.exit()
我们将不胜感激任何发现问题甚至帮助我解决问题的帮助。
【问题讨论】:
-
你的电脑上真的安装了
pygame吗? -
可能是因为你有几个
while循环和事件循环。 -
我在安装 pygame 时遇到了问题,但我认为它已安装。当我运行转换后的 exe 文件时也没关系,因为即使没有安装 python,它们也应该可以工作
-
不,不是因为while循环,脚本在linux上完美运行
标签: python windows pygame packaging