我制作了一个简单的pong游戏,并希望使用pyinstaller将其转换为exe。
我在命令提示符下使用命令“ pip install pyinstaller”安装pyinstaller,并且安装正确,然后使用命令“ pyinstaller pong.py”在我的pong游戏中运行它,并且转换后没有任何错误。
但是,当我尝试运行exe时,会打开正确的大小窗口,但只有黑屏,几秒钟后,它表明程序已停止运行,并关闭了它。
这是我的代码:
import pygame
pygame.init()
clock = pygame.time.Clock()
window = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("PONG")
font = pygame.font.SysFont("comicsans", 30, True)
title_font = pygame.font.SysFont("comicsans", 100, True)
def intro():
global bot_speed
global ball_speed
intro = True
while intro:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(20)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
window.fill((0,0,0))
title = title_font.render("PONG", 1, (255,255,255))
window.blit(title, (490, 200))
pygame.draw.rect(window, (255,255,255), (500, 400, 200, 50))
pygame.draw.rect(window, (0,0,0), (510, 410, 180, 30))
easy = font.render("Easy", 1, (255,255,255))
window.blit(easy, (575, 415))
pygame.draw.rect(window, (255,255,255), (500, 500, 200, 50))
pygame.draw.rect(window, (0,0,0), (510, 510, 180, 30))
medium = font.render("Medium", 1, (255,255,255))
window.blit(medium, (555, 515))
pygame.draw.rect(window, (255,255,255), (500, 600, 200, 50))
pygame.draw.rect(window, (0,0,0), (510, 610, 180, 30))
hard = font.render("Hard", 1, (255,255,255))
window.blit(hard, (575, 615))
if 700 > mouse[0] > 500 and 450 > mouse[1] > 400:
if click[0] == 1:
bot_speed = 0.5
ball_speed = 1
intro = False
game_loop()
if 700 > mouse[0] > 500 and 550 > mouse[1] > 500:
if click[0] == 1:
bot_speed = 1
ball_speed = 1.5
intro = False
game_loop()
if 700 > mouse[0] > 500 and 650 > mouse[1] > 600:
if click[0] == 1:
bot_speed = 1.9
ball_speed = 2
intro = False
game_loop()
pygame.display.update()
def game_loop():
global bot_speed
global ball_speed
player_score = 0
bot_score = 0
direction = "playerup"
game = True
ball_x = 600
ball_y = 390
player_y = 350
bot_y = 350
while game:
window.fill((0,0,0))
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(200)
mouse = pygame.mouse.get_pos()
pygame.draw.rect(window, (255,255,255), (1150, player_y, 20, 100))
pygame.draw.rect(window, (255,255,255), (50, bot_y, 20, 100))
pygame.draw.rect(window, (255,255,255), (ball_x, ball_y, 20, 20))
if mouse[1] > player_y:
if player_y < 700:
player_y += 1
elif mouse[1] < player_y:
if player_y > 0:
player_y -= 1
if ball_x >= 1200:
bot_score += 1
ball_x = 600
ball_y = 390
player_y = 350
bot_y = 350
elif ball_x <= 0:
player_score += 1
ball_x = 600
ball_y = 390
player_y = 350
bot_y = 350
if direction == "playerup":
if ball_y > 0:
ball_x += ball_speed
ball_y -= ball_speed
if ball_y + 20 > bot_y:
if bot_y < 700:
bot_y += bot_speed
if ball_y - 20 < bot_y:
if bot_y > 0:
bot_y -= bot_speed
else:
direction = "playdown"
elif direction == "playdown":
if ball_y < 780:
ball_x += ball_speed
ball_y += ball_speed
if ball_y + 20 > bot_y:
if bot_y < 700:
bot_y += bot_speed
if ball_y - 20 < bot_y:
if bot_y > 0:
bot_y -= bot_speed
else:
direction = "playerup"
elif direction == "botup":
if ball_y > 0:
ball_x -= ball_speed
ball_y -= ball_speed
if ball_y + 20 > bot_y:
if bot_y < 700:
bot_y += bot_speed
if ball_y - 20 < bot_y:
if bot_y > 0:
bot_y -= bot_speed
else:
direction = "botdown"
elif direction == "botdown":
if ball_y < 780:
ball_x -= ball_speed
ball_y += ball_speed
if ball_y + 20 > bot_y:
if bot_y < 700:
bot_y += bot_speed
if ball_y - 20 < bot_y:
if bot_y > 0:
bot_y -= bot_speed
else:
direction = "botup"
if 1135 >= ball_x >= 1130 and ball_y + 20 >= player_y >= ball_y - 80:
if direction == "playerup":
direction = "botup"
if direction == "playdown":
direction = "botdown"
if 65 <= ball_x <= 70 and ball_y + 20 >= bot_y >= ball_y - 80:
if direction == "botup":
direction = "playerup"
if direction == "botdown":
direction = "playdown"
player = font.render("PLAYER", 1, (255,255,255))
window.blit(player, (1050, 50))
player = font.render("BOT", 1, (255,255,255))
window.blit(player, (50, 50))
score = str(player_score)
play_score = font.render(score, 1, (255,255,255))
score = str(bot_score)
botscore = font.render(score, 1,(255,255,255))
window.blit(botscore, (100, 700))
window.blit(play_score, (1100, 700))
intro()
我尝试了其他一些程序,它们做同样的事情。