如何将Python脚本转换为exe

如何将Python脚本转换为exe,python,pyinstaller,executable,Python,Pyinstaller,Executable,我一整天都在努力把我的python脚本变成一个可执行文件。我尝试过使用cx Freeze和PyInstaller,但都不起作用。我现在对PyInstaller感兴趣,已经放弃了cx冻结。这是我的代码,我需要有人告诉我是否有可能将其转换为可执行文件: # Importing libraries import pygame import random import time # Initializing PyGame pygame.init() # Setting a window name p

我一整天都在努力把我的python脚本变成一个可执行文件。我尝试过使用cx Freeze和PyInstaller,但都不起作用。我现在对PyInstaller感兴趣,已经放弃了cx冻结。这是我的代码,我需要有人告诉我是否有可能将其转换为可执行文件:

# Importing libraries
import pygame
import random
import time

# Initializing PyGame
pygame.init()

# Setting a window name
pygame.display.set_caption("Ping Pong")

# Creating a font
pygame.font.init()
font = pygame.font.SysFont(None, 30)
pong_font = pygame.font.SysFont("comicsansms", 75)
winner_font = pygame.font.SysFont("consolas", 50)

# Set the height and width of the screen
window_width = 700
window_height = 500
size = [window_width, window_height]
game_win = pygame.display.set_mode(size)
game_win2 = pygame.display.set_mode(size)


# Creating a messaging system
def message(sentence, color, x, y, font_type, display):
    sentence = font_type.render(sentence, True, color)
    display.blit(sentence, [x, y])


# Creating colors
white = (225, 225, 225)
black = (0, 0, 0)
gray = (100, 100, 100)

# Setting up ball
ball_size = 25


class Ball:
    """
    Class to keep track of a ball's location and vector.
    """

    def __init__(self):
        self.x = 0
        self.y = 0
        self.change_x = 0
        self.change_y = 0


def make_ball():
    ball = Ball()
    # Starting position of the ball.
    ball.x = 350
    ball.y = 250

    # Speed and direction of rectangle
    ball.change_x = 5
    ball.change_y = 5

    return ball


def main():
    # Scores
    left_score = 0
    right_score = 0

    pygame.init()

    # Loop until the user clicks the close button.
    done = False

    ball_list = []

    ball = make_ball()
    ball_list.append(ball)

    # Right paddle coordinates
    y = 200
    y_change = 0
    x = 50
    # Left paddle coordinates
    y1 = 200
    y1_change = 0
    x1 = 650

    while not done:
        
        # --- Event Processing
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    y_change = -7

                elif event.key == pygame.K_s:
                    y_change = 7

                elif event.key == pygame.K_UP:
                    y1_change = -7

                elif event.key == pygame.K_DOWN:
                    y1_change = 7

            elif event.type == pygame.KEYUP:
                y_change = 0
                y1_change = 0

        y += y_change
        y1 += y1_change

        # Preventing from letting the paddle go off screen
        if y > window_height - 100:
            y -= 10
        if y < 50:
            y += 10
        if y1 > window_height - 100:
            y1 -= 10
        if y1 < 50:
            y1 += 10

        # Logic
        for ball in ball_list:
            # Move the ball's center
            ball.x += ball.change_x
            ball.y += ball.change_y

            # Bounce the ball if needed
            if ball.y > 500 - ball_size or ball.y < ball_size:
                ball.change_y *= -1
            if ball.x > window_width - ball_size:
                ball.change_x *= -1
                left_score += 1
            if ball.x < ball_size:
                ball.change_x *= -1
                right_score += 1

            ball_rect = pygame.Rect(ball.x - ball_size, ball.y - ball_size, ball_size * 2, ball_size * 2)

            left_paddle_rect = pygame.Rect(x, y, 25, 75)
            if ball.change_x < 0 and ball_rect.colliderect(left_paddle_rect):
                ball.change_x = abs(ball.change_x)

            right_paddle_rect = pygame.Rect(x1, y1, 25, 75)
            if ball.change_x > 0 and ball_rect.colliderect(right_paddle_rect):
                ball.change_x = -abs(ball.change_x)

            if right_score == 10:
                message("RIGHT PLAYER HAS WON!!", white, 60, 250, winner_font, game_win)
                pygame.display.flip()
                pygame.event.poll()
                time.sleep(5)
                pygame.quit()
                quit()
            elif left_score == 10:
                message("LEFT PLAYER HAS WON!!", white, 60, 250, winner_font, game_win)
                pygame.display.flip()
                pygame.event.poll()
                time.sleep(5)
                pygame.quit()
                quit()

        # Drawing
        # Set the screen background
        game_win.fill(black)

        # Draw the balls
        for ball in ball_list:
            pygame.draw.circle(game_win, white, [ball.x, ball.y], ball_size)

        # Creating Scoreboard
        message("Left player score: " + str(left_score), white, 10, 10, font, game_win)
        message("Right player score: " + str(right_score), white, 490, 10, font, game_win)

        # Drawing a left paddle
        pygame.draw.rect(game_win, white, [x, y, 25, 100])
        # Drawing a right paddle
        pygame.draw.rect(game_win, white, [x1, y1, 25, 100])

        # Setting FPS
        FPS = pygame.time.Clock()
        FPS.tick(60)

        # Updating so actions take place
        pygame.display.flip()


while True:
    game_win2.fill(black)
    pygame.event.poll()     
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    message("Pong", white, 280, 100, pong_font, game_win2)
    if 150 + 100 > mouse[0] > 150 and 350 + 50 > mouse[1] > 350:
        pygame.draw.rect(game_win, gray, [150, 350, 100, 50])
        if click[0] == 1:
            break
    else:
        pygame.draw.rect(game_win, white, [150, 350, 100, 50])

    if 450 + 100 > mouse[0] > 450 and 350 + 50 > mouse[1] > 350:
        pygame.draw.rect(game_win, gray, [450, 350, 100, 50])
        if click[0] == 1:
            pygame.quit()
            quit()
    else:
        pygame.draw.rect(game_win, white, [450, 350, 100, 50])

    message("Start", black, 175, 367, font, game_win2)
    message("Quit", black, 475, 367, font, game_win2)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # Wrap-up
    # Limit to 60 frames per second
    clock = pygame.time.Clock()
    clock.tick(60)

if __name__ == "__main__":
    main()
#导入库
导入pygame
随机输入
导入时间
#初始化PyGame
pygame.init()
#设置窗口名
pygame.display.set_标题(“乒乓球”)
#创建字体
pygame.font.init()
font=pygame.font.SysFont(无,30)
pong_font=pygame.font.SysFont(“comicsansms”,75)
winner\u font=pygame.font.SysFont(“控制台”,50)
#设置屏幕的高度和宽度
窗宽=700
窗户高度=500
大小=[窗宽,窗高]
game\u win=pygame.display.set\u模式(大小)
game_win2=pygame.display.set_模式(大小)
#创建消息传递系统
def消息(句子、颜色、x、y、字体类型、显示):
句子=font\u type.render(句子、真、彩色)
display.blit(句子[x,y])
#创造色彩
白色=(225225225)
黑色=(0,0,0)
灰色=(100100100)
#摆球
钢球尺寸=25
班级舞会:
"""
类来跟踪球的位置和向量。
"""
定义初始化(自):
self.x=0
self.y=0
self.change_x=0
self.change_y=0
def make_ball():
ball=ball()
#球的起始位置。
球x=350
球y=250
#矩形的速度和方向
ball.change_x=5
ball.change_y=5
回击球
def main():
#得分
左_分数=0
右(右)分=0
pygame.init()
#循环,直到用户单击关闭按钮。
完成=错误
ball_list=[]
ball=制造球()
ball\u list.append(ball)
#右桨坐标
y=200
y_变化=0
x=50
#左桨坐标
y1=200
y1_变化=0
x1=650
虽然没有这样做:
#---事件处理
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_w:
y_变化=-7
elif event.key==pygame.K_:
y_变化=7
elif event.key==pygame.K_UP:
y1_变化=-7
elif event.key==pygame.K_向下:
y1_变化=7
elif event.type==pygame.KEYUP:
y_变化=0
y1_变化=0
y+=y_变化
y1+=y1_变化
#防止挡板离开屏幕
如果y>窗高-100:
y-=10
如果y<50:
y+=10
如果y1>窗高-100:
y1-=10
如果y1<50:
y1+=10
#逻辑
对于“球”列表中的球:
#移动球的中心
ball.x+=ball.change\u x
ball.y+=ball.change\u y
#如果需要,可以弹起球
如果ball.y>500-ball\u尺寸或ball.y窗口宽度-球大小:
ball.change_x*=-1
左_分数+=1
如果ball.x0和ball\u rect.Collide rect(右桨):
ball.change\ux=-abs(ball.change\ux)
如果右_分数==10:
消息(“正确的玩家赢了!!”,怀特,60,250,赢家,游戏赢了)
pygame.display.flip()
pygame.event.poll()
时间。睡眠(5)
pygame.quit()
退出
elif left_分数==10:
消息(“左边的玩家赢了!!”,怀特,60,250,赢了,游戏赢了)
pygame.display.flip()
pygame.event.poll()
时间。睡眠(5)
pygame.quit()
退出
#绘图
#设置屏幕背景
游戏胜利填充(黑色)
#抽签
对于“球”列表中的球:
pygame.draw.circle(游戏胜利,白色,[ball.x,ball.y],球大小)
#创建记分板
消息(“左玩家分数:+str(左分数),白色,10,10,字体,游戏胜利)
消息(“右玩家分数:+str(右分数),白色,490,10,字体,游戏胜利)
#划左桨
pygame.draw.rect(游戏胜利,白色,[x,y,25100])
#划右桨
pygame.draw.rect(游戏胜利,白色,[x1,y1,25100])
#设置FPS
FPS=pygame.time.Clock()
FPS.勾选(60)
#更新,以便采取行动
pygame.display.flip()
尽管如此:
游戏2.填充(黑色)
pygame.event.poll()
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
信息(“Pong”,白色,280,100,Pong_字体,game_win2)
如果150+100>鼠标[0]>150和350+50>鼠标[1]>350:
pygame.draw.rect(game_win,gray,[15035010050])
如果单击[0]==1:
打破
其他:
pygame.draw.rect(游戏胜利,白色,[15035010050])
如果450+100>鼠标[0]>450和350+50>鼠标[1]>350:
pygame.draw.rect(game_win,gray,[45035010050])
如果单击[0]==1:
pygame.quit()
退出
其他:
pygame.draw.rect(游戏胜利,白色,[45035010050])
信息(“开始”,黑色,175,367,字体,游戏2)
信息(