0

I am using pyinstaller with VSCode and pygame I can run the command and get an EXE file that won't run no error messages nothing. this is the code:

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'), True,(0,255,0))
PVP = font.render(('PVP'),True,(0,255,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,0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
    pygame.draw.rect(win,(0,255,0),(ButtonX1,ButtonY1,200,50))
    pygame.draw.rect(win,(0,255,0),(ButtonX1,ButtonY2,200,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),((winWidth//2-98,(winHeight//2)-23,196,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,(50,100,50),((winWidth//2-98,(winHeight//2)+52,196,46)))
        if pygame.mouse.get_pressed() == (True, False, False):
            pygame.mixer.music.load('Assets\Sounds\Click.mp3')
            pygame.mixer.music.play()
            AlivePVP = True
    else:
        pygame.draw.rect(win,(20,20,20),((winWidth//2-98,(winHeight//2)+52,196,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, (0,255,0), (PreX,PreY,Size,Size))
        Snack = pygame.draw.rect(win, (255,0,0), (sX*20,sY*20,Size,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!!!'), True, (255,0,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,255,BT), (PreX,PreY,Size,Size))
            Snack = pygame.draw.rect(win, (255,BT,BT), (sX*Size,sY*Size,Size,Size))
            ScoreText = font.render(('Score: '+ str(Length)), True, (0,255,0))
            HighScoreText = font.render(('High Score: '+str(HighScore)), True,(0,255,0))
            win.blit(ScoreText, (10,10))
            win.blit(HighScoreText, (10,30))
            SnakeBody = []
            for _ in range(Length):
                Cords = Body[Index]
                CordX = int(Cords[0])
                CordY = int(Cords[1])
                SnakeBody.append((pygame.draw.rect(win, (BT,255,BT), (CordX,CordY,Size,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,(0,0,0),(Pause_X-27,Pause_Y-27,54,54))
                pygame.draw.rect(win, (0,255,0), (Pause_X-25,Pause_Y-25,50,50))
                pygame.draw.rect(win, (0,0,0), (Pause_X-12,Pause_Y-27,24,54))
                pygame.draw.rect(win, (BT/2,BT/2,BT/2), (Pause_X-10,Pause_Y-27,20,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,49)
        sY = random.randint(0,49)
        font = pygame.font.SysFont("Arial",25)
        Snake1 = pygame.draw.rect(win, (0,255,0), (PreX1,PreY1,Size,Size))
        Snake2 = pygame.draw.rect(win, (0,0,255), (PreX2,PreY2,Size,Size))
        Snack = pygame.draw.rect(win, (255,0,0), (sX*20,sY*20,Size,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_X)
                sY = 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_X)
                sY = 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,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,(BT,255,BT), (PreX1,PreY1,Size,Size))
            Snake2 = pygame.draw.rect(win,(BT,BT,255), (PreX2,PreY2,Size,Size))
            Snack = pygame.draw.rect(win, (255,BT,BT), (sX*Size,sY*Size,Size,Size))
            SnakeBody1 = []
            for _ in range(Length1):
                Cords = Body1[Index1]
                CordX = int(Cords[0])
                CordY = int(Cords[1])
                SnakeBody1.append((pygame.draw.rect(win, (BT,255,BT), (CordX,CordY,Size,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_X)
                        sY = 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, (BT,BT,255), (CordX,CordY,Size,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,SnackMax_X)
                        sY = random.randint(0,SnackMax_Y) 
            if Paused:
                pygame.draw.rect(win,(0,0,0),(Pause_X-27,Pause_Y-27,54,54))
                pygame.draw.rect(win, (0,255,0), (Pause_X-25,Pause_Y-25,50,50))
                pygame.draw.rect(win, (0,0,0), (Pause_X-12,Pause_Y-27,24,54))
                pygame.draw.rect(win, (BT/2,BT/2,BT/2), (Pause_X-10,Pause_Y-27,20,54))
            if Win1:
                P1Win = Font.render(('P1 WINS'),True,(0,255,0))
                win.blit(P1Win, (winWidth//2-(Slogo_Size[2]//2),winHeight//2-Slogo_Size[3]//2))
                Alive = False
            if Win2:
                P2Win = Font.render(('P2 WINS'),True,(0,0,255))
                win.blit(P2Win, (winWidth//2-(Slogo_Size[2]//2),winHeight//2-Slogo_Size[3]//2))
                Alive = False
            pygame.display.update()
        time.sleep(1)
        AlivePVP = False
pygame.quit()

and this is the spec file for it:

# -*- 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')

and this is the command I am using :

pyinstaller --onefile -w -iSnake_icon.ico Snake_Code.py

I tried to delete any files that may cause problems but I can always recheck. I have all of my sounds and things in a different folder but the ico file is in the same folder as the code

JakeTDP
  • 31
  • 2
  • Your problem is likely due to file paths. If you run your executable from the command line, you may see a relevant error traceback that will help diagnose the issue. This [answer](https://stackoverflow.com/a/48696953/7675174) might give some help, or the [question](https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile) it links. – import random Nov 11 '20 at 02:26
  • so what would my command be exactly because I am very new to all this and its a bit confusing – JakeTDP Nov 11 '20 at 03:26
  • @JakeTDP -Is the result executable and shows the icon when you execute this python script in VScode? Please check if the file path in the code is correct and the icon size is available. – Jill Cheng Nov 11 '20 at 09:26
  • This [answer](https://stackoverflow.com/a/44438174/7675174) has detailed steps to accomplish your goal. Now that you have a `.spec` file, you just run `pyinstaller your_spec_file.spec` to build your executable (step 6 from the linked answer). – import random Nov 12 '20 at 01:52

0 Answers0