问题描述
我正在将pyinstaller与VSCode和pygame一起使用,我可以运行命令并获取一个EXE文件,该文件不会运行且不会显示任何错误消息。 这是代码:
import pygame
import random
import time
pygame.init()
High = []
Highscores = open('Assets\Highscores.csv',"r")
for row in Highscores:
High.append(int(row))
Highscores.close()
Highscore = max(High)
pygame.init()
Alive = False
Restart = False
AlivePVP = False
font = pygame.font.SysFont("Arial",40)
Font = pygame.font.SysFont("Arisl",200)
play = font.render(('PLAY'),True,(0,255,0))
Slogo = Font.render(('SNAKE'),0))
PVP = font.render(('PVP'),0))
play_Size = play.get_rect()
Slogo_Size = Slogo.get_rect()
PVP_Size = PVP.get_rect()
Size = 10
Image = pygame.image.load("Assets\Snake_Pic.png")
pygame.display.set_icon(Image)
info = pygame.display.Info()
winHeight = info.current_h
winHeight = int((winHeight-(Size*((winHeight/Size)-(winHeight//Size)))))
winWidth = winHeight
win = pygame.display.set_mode((winWidth,winHeight),pygame.FULLSCREEN)
pygame.display.set_caption("Snake")
ButtonX1 = winWidth//2-100
ButtonY1 = winHeight//2-25
ButtonY2 = winHeight//2+50
full = False
StandBY = True
but1 = 0
but2 = 0
while StandBY:
win.fill((0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.draw.rect(win,0),(ButtonX1,ButtonY1,200,50))
pygame.draw.rect(win,ButtonY2,50))
Mouse = pygame.mouse.get_pos()
keys = pygame.key.get_pressed()
if keys [pygame.K_ESCAPE]:
StandBY = False
if Mouse[0] >= ButtonX1 and Mouse[1] >= ButtonY1 and Mouse[0] <= ButtonX1 + 200 and Mouse[1] <= ButtonY1 +50:
if but1 == 0:
but1 =1
pygame.mixer.music.load('Assets\Sounds\Button.mp3')
pygame.mixer.music.play()
pygame.draw.rect(win,(50,100,50),((winWidth//2-98,(winHeight//2)-23,196,46)))
if pygame.mouse.get_pressed() == (True,False,False):
pygame.mixer.music.load('Assets\Sounds\Click.mp3')
pygame.mixer.music.play()
Alive = True
else:
pygame.draw.rect(win,(20,20,20),46)))
Alive = False
but1 = 0
if Mouse[0] >= ButtonX1 and Mouse[1] >= ButtonY2 and Mouse[0] <= ButtonX1 + 200 and Mouse[1] <= ButtonY2 +50:
if but2 == 0:
but2 =1
pygame.mixer.music.load('Assets\Sounds\Button.mp3')
pygame.mixer.music.play()
pygame.draw.rect(win,(winHeight//2)+52,False):
pygame.mixer.music.load('Assets\Sounds\Click.mp3')
pygame.mixer.music.play()
AlivePVP = True
else:
pygame.draw.rect(win,46)))
AlivePVP = False
but2 = 0
win.blit(play,(winWidth//2-(play_Size[2]//2),winHeight//2-play_Size[3]//2))
win.blit(PVP,(winWidth//2-(PVP_Size[2]//2),winHeight//2-(PVP_Size[3]//2-75)))
win.blit(Slogo,(winWidth//2-(Slogo_Size[2]//2),winHeight//2-(Slogo_Size[3]//2)-100))
time.sleep(0.1)
pygame.display.update()
if Alive:
rec = 0
PreX = Pause_X = (int(((winWidth/Size)/2)*Size))
PreY = Pause_Y = (int(((winHeight/Size)/2)*Size))
Vel = Size
X = 1
Y = 0
full = False
Length = 0
Paused = False
BT = 0
Body = []
SnakeBody = []
Index = 0
Growth = 0
Growing = True
Time = 0.07
Shrunk = 0
SnackMax_X = ((winWidth/Size)-1)
SnackMax_Y = ((winHeight/Size)-1)
sX = random.randint(0,49)
sY = random.randint(0,49)
font = pygame.font.SysFont("Arial",25)
Snake = pygame.draw.rect(win,(PreX,PreY,Size,Size))
Snack = pygame.draw.rect(win,(255,(sX*20,sY*20,Size))
pygame.display.update()
Alive = True
while Alive:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Alive = False
time.sleep(Time)
if not Paused:
BodyX = PreX
BodyY = PreY
Body.append((BodyX,BodyY))
PreX = (Vel * X)+PreX
PreY = (Vel * Y)+PreY
Index = (len(Body))-1
if Paused:
Index = Paused_Index
keys = pygame.key.get_pressed()
if keys [pygame.K_LEFT] or keys [pygame.K_a]:
X = -1
Y = 0
Paused = False
if keys [pygame.K_RIGHT] or keys [pygame.K_d]:
X = 1
Y = 0
Paused = False
if keys [pygame.K_UP] or keys [pygame.K_w]:
X = 0
Y = -1
Paused = False
if keys [pygame.K_DOWN] or keys [pygame.K_s]:
X = 0
Y = 1
Paused = False
if keys [pygame.K_SPACE]:
X = 0
Y = 0
Paused = True
Paused_Index = Index
if keys [pygame.K_ESCAPE]:
Alive = False
if keys [pygame.K_LSHIFT]or keys [pygame.K_RSHIFT]:
Shrinking = True
else:
Shrinking = False
if Paused:
BT = 100
else:
BT = 20
if PreX == -Vel or PreX == winWidth :
Alive = False
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if PreY == -Vel or PreY == winHeight:
Alive = False
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if Snack.colliderect(Snake):
sX = random.randint(0,SnackMax_X)
sY = random.randint(0,SnackMax_Y)
pygame.mixer.music.load('Assets\Sounds\Button.mp3')
pygame.mixer.music.play()
Growing = True
win.fill((BT/2,BT/2,BT/2))
if Growing:
Growth += 1
if Growth < 5:
Length += 1
if Growth == 5:
Growth = 0
Length += 1
Growing = False
if Shrinking and Length > 0:
Shrunk += 1
Time = 0.03
if Shrunk == 4:
Length -= 1
Shrunk = 0
else:
Time = 0.07
if Length >= int(Highscore):
NewHigh = font.render(('NEW RECORD!!!'),0))
High_Size = NewHigh.get_rect()
win.blit(NewHigh,(Pause_X-(High_Size[2]//2),10))
rec+=1
if rec == 1:
pygame.mixer.music.load('Assets\Sounds\Click.mp3')
pygame.mixer.music.play()
Highscore = Length
Snake = pygame.draw.rect(win,(BT,BT),Size))
Snack = pygame.draw.rect(win,BT,(sX*Size,sY*Size,Size))
scoreText = font.render(('score: '+ str(Length)),0))
HighscoreText = font.render(('High score: '+str(Highscore)),0))
win.blit(scoreText,(10,10))
win.blit(HighscoreText,30))
SnakeBody = []
for _ in range(Length):
Cords = Body[Index]
CordX = int(Cords[0])
CordY = int(Cords[1])
SnakeBody.append((pygame.draw.rect(win,(CordX,CordY,Size))))
Index -= 1
if Index < 0:
Index = 0
if not Paused:
Cords = []
for CollIN in range(len(SnakeBody)):
if Snake.contains(SnakeBody[CollIN])and not Paused:
if CollIN == 0:
pass
else:
Alive = False
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if Snack.contains(SnakeBody[CollIN]):
sX = random.randint(0,SnackMax_X)
sY = random.randint(0,SnackMax_Y)
if Paused:
pygame.draw.rect(win,(Pause_X-27,Pause_Y-27,54,54))
pygame.draw.rect(win,(Pause_X-25,Pause_Y-25,50,50))
pygame.draw.rect(win,(Pause_X-12,24,(BT/2,BT/2),(Pause_X-10,54))
pygame.display.update()
Highscores2 = open('Assets\Highscores.csv',"a")
Highscores2.write(str(Length))
Highscores2.write("\n")
Highscores2.close()
if AlivePVP:
Win1 = False
Win2 = False
P1score = 0
P2score = 0
PreX2 = PreX1 = Pause_X = (int(((winWidth/Size)/2)*Size))
PreY2 = PreY1 = Pause_Y = (int(((winHeight/Size)/2)*Size))
Vel = Size
X1 = -1
Y1 = 0
X2 = 1
Y2 = 0
full = False
Length2=Length1 = 0
Paused = False
BT = 0
Body2 = []
Body1 = []
SnakeBody2 = []
SnakeBody1 = []
Index2 = Index1 = 0
Growth2 = Growth1 = 0
Growing2 = Growing1 = True
Time = 0.07
SnackMax_X = ((winWidth/Size)-1)
SnackMax_Y = ((winHeight/Size)-1)
sX = random.randint(0,25)
Snake1 = pygame.draw.rect(win,(PreX1,PreY1,Size))
Snake2 = pygame.draw.rect(win,255),(PreX2,PreY2,Size))
pygame.display.update()
Alive = True
while Alive:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Alive = False
time.sleep(Time)
if not Paused:
BodyX1 = PreX1
BodyY1 = PreY1
Body1.append((BodyX1,BodyY1))
PreX1 = (Vel * X1)+PreX1
PreY1 = (Vel * Y1)+PreY1
Index1 = (len(Body1))-1
BodyX2 = PreX2
BodyY2 = PreY2
Body2.append((BodyX2,BodyY2))
PreX2 = (Vel * X2)+PreX2
PreY2 = (Vel * Y2)+PreY2
Index2 = (len(Body2))-1
if Paused:
Index1 = Paused_Index1
Index2 = Paused_Index2
keys = pygame.key.get_pressed()
if keys [pygame.K_a]:
X1 = -1
Y1 = 0
Paused = False
if keys [pygame.K_d]:
X1 = 1
Y1 = 0
Paused = False
if keys [pygame.K_w]:
X1 = 0
Y1 = -1
Paused = False
if keys [pygame.K_s]:
X1 = 0
Y1 = 1
Paused = False
if keys [pygame.K_LEFT]:
X2 = -1
Y2 = 0
Paused = False
if keys [pygame.K_RIGHT]:
X2 = 1
Y2 = 0
Paused = False
if keys [pygame.K_UP]:
X2 = 0
Y2 = -1
Paused = False
if keys [pygame.K_DOWN]:
X2 = 0
Y2 = 1
Paused = False
if keys [pygame.K_SPACE]:
X1 = 0
Y1 = 0
X2 = 0
Y2 = 0
Paused = True
Paused_Index1 = Index1
Paused_Index2 = Index2
if keys [pygame.K_ESCAPE]:
Alive = False
if Paused:
BT = 100
else:
BT = 20
if PreX1 == -Vel or PreX1 == winWidth :
Win2 = True
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if PreY1 == -Vel or PreY1 == winHeight:
Win2 = True
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if PreX2 == -Vel or PreX2 == winWidth :
Win1 = True
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if PreY2 == -Vel or PreY2 == winHeight:
Win1 = True
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if Snack.colliderect(Snake1):
sX = random.randint(0,SnackMax_Y)
pygame.mixer.music.load('Assets\Sounds\Button.mp3')
pygame.mixer.music.play()
Growing1 = True
if Snack.colliderect(Snake2):
sX = random.randint(0,SnackMax_Y)
pygame.mixer.music.load('Assets\Sounds\Button.mp3')
pygame.mixer.music.play()
Growing2 = True
win.fill((BT/2,BT/2))
if Growing1:
Growth1 += 1
if Growth1 < 5:
Length1 += 1
if Growth1 == 5:
Growth1 = 0
Length1 += 1
Growing1 = False
if Growing2:
Growth2 += 1
if Growth2 < 5:
Length2 += 1
if Growth2 == 5:
Growth2 = 0
Length2 += 1
Growing2 = False
Snake1 = pygame.draw.rect(win,Size))
Snake2 = pygame.draw.rect(win,Size))
SnakeBody1 = []
for _ in range(Length1):
Cords = Body1[Index1]
CordX = int(Cords[0])
CordY = int(Cords[1])
SnakeBody1.append((pygame.draw.rect(win,Size))))
Index1 -= 1
if Index1 < 0:
Index1 = 0
if not Paused:
Cords1 = []
for CollIN in range(len(SnakeBody1)):
if CollIN == 0:
pass
elif Snake1.contains(SnakeBody1[CollIN])or Snake1.contains(SnakeBody2[CollIN-1])and not Paused:
if CollIN == 0:
pass
else:
Win2 = True
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if Snack.contains(SnakeBody1[CollIN]):
sX = random.randint(0,SnackMax_Y)
SnakeBody2 = []
for _ in range(Length2):
Cords = Body2[Index2]
CordX = int(Cords[0])
CordY = int(Cords[1])
SnakeBody2.append((pygame.draw.rect(win,Size))))
Index2 -= 1
if Index2 < 0:
Index2 = 0
if not Paused:
Cords2 = []
for CollIN in range(len(SnakeBody2)):
if Snake2.contains(SnakeBody2[CollIN])or Snake2.contains(SnakeBody1[CollIN])and not Paused:
if CollIN == 0:
pass
else:
Win1 = True
pygame.mixer.music.load('Assets\Sounds\Death.mp3')
pygame.mixer.music.play()
if Snack.contains(SnakeBody2[CollIN]):
sX = random.randint(0,54))
if Win1:
P1Win = Font.render(('P1 WINS'),0))
win.blit(P1Win,winHeight//2-Slogo_Size[3]//2))
Alive = False
if Win2:
P2Win = Font.render(('P2 WINS'),255))
win.blit(P2Win,winHeight//2-Slogo_Size[3]//2))
Alive = False
pygame.display.update()
time.sleep(1)
AlivePVP = False
pygame.quit()
这是它的规格文件:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['Snake_Code.py'],pathex=['C:\\Users\\Ashley\\Desktop\\Snake'],binaries=[],datas=[],hiddenimports=[],hookspath=[],runtime_hooks=[],excludes=[],win_no_prefer_redirects=False,win_private_assemblies=False,cipher=block_cipher,noarchive=False)
pyz = PYZ(a.pure,a.zipped_data,cipher=block_cipher)
exe = EXE(pyz,a.scripts,a.binaries,a.zipfiles,a.datas,[],name='Snake_Code',debug=False,bootloader_ignore_signals=False,strip=False,upx=True,upx_exclude=[],runtime_tmpdir=None,console=False,icon='Snake_Icon.ico')
这是我正在使用的命令:
pyinstaller --onefile -w -iSnake_icon.ico Snake_Code.py
我试图删除任何可能引起问题的文件,但是我总是可以重新检查。我所有的声音和东西都放在不同的文件夹中,但ico文件与代码位于同一文件夹中
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)