gpt4 book ai didi

Python 文件 .exe 只是在一瞬间出现,然后立即关闭

转载 作者:行者123 更新时间:2023-12-04 15:12:26 26 4
gpt4 key购买 nike

我是 pygame 的新手。当互联网中断时,我已经制作了一个类似于恐龙游戏的游戏代码。但是,当我从 .py 转换为 .exe 时,它​​会立即打开并立即关闭,尽管主脚本运行良好。我一直在使用的命令是 pyinstaller file.py --onefile,它生成一个可执行文件,在我运行它时会立即关闭。我已经检查过是否有错误,但它没有(我没有你外面的任何 image.png)

import pygame
import time
import random
import sys

pygame.init()

white = (255,255,255)
yellow = (255,255,102)
black = (0,0,0)
red = (235, 64, 52)
width = 1000
height = 600

clock = pygame.time.Clock()
font_style = pygame.font.SysFont(None, 30)
score_font = pygame.font.SysFont('monospace', 35)
screen = pygame.display.set_mode((width,height))
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, (round(width/3), round(height/3)))
bush_speed = 1


def your_score(score1):
value = score_font.render("Your Score: " + str(score1), True, yellow)
screen.blit(value, [0, 0])


game_over = False
def collision(player_pos,bush_pos):
p_x = player_pos[0]
p_y = player_pos[1]
b_x = bush_pos[0]
b_y = bush_pos[1]
if (b_x >= p_x and b_x < p_x + 50 ) or (b_x == p_x) or (p_x >= b_x and p_x < b_x + 50) :
if (b_y >= p_y and b_y < p_y + 100) or (b_y == p_y) :
return True

return False

def bush(bush_height,bush_pos):
pygame.draw.rect(screen,red, [bush_pos[0],bush_pos[1],50,bush_height])

def speed(bush_speed,score1):
if score1 < 3:
bush_speed = 50
elif score1 < 20:
bush_speed = 4
elif score1 < 30:
bush_speed = 5
else:
bush_speed = 15

def game_loop():
game_close = False
player_pos = [200,500]
player_jump =[0,0]
player_height = 100
player_size = 50
score1 = 0
game_over = False
bush_size = 50
bush_height = (round(random.randint(50,100)/10) *10)
bush_pos = [width - bush_size, height - bush_height]

while not game_over:

while game_close:
screen.fill(white)
message("You Lost! Press C-Play Again or Q-Quit", red)
pygame.display.update()

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
game_loop()
if event.key == pygame.K_q:
game_over = True
game_close = False

screen.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if player_pos[0] > 0:
if event.key == pygame.K_LEFT:
player_pos[0] -= 50
if player_pos[0] < width - player_size:
if event.key == pygame.K_RIGHT:
player_pos[0] +=50
if player_pos[1] == height -100:
if event.key == pygame.K_SPACE:
player_pos[1] -=200
player_jump[0] = 1
if player_pos[1] < height - player_height :
player_pos[1] += player_jump[0]

bush_pos[0] -= bush_speed*2


clock.tick(200)
if bush_pos[0] < 0 :
bush_height = (round(random.randint(50,100)/10) *10)
bush_pos = [width - bush_size, height - bush_height]
score1 +=1
speed(bush_speed,score1)

if collision(player_pos,bush_pos):
game_close = True


bush(bush_height,bush_pos)
your_score(score1)



pygame.draw.rect(screen, red, (player_pos[0],player_pos[1],player_size,player_height))
pygame.display.update()
pygame.display.flip()

game_loop()

对不起,如果我的英语不好而且我对代码的了解太有限

最佳答案

字体通常是这里的问题,音频文件也是如此。尝试替换:

font_style = pygame.font.SysFont(None, 30)

font_style = pygame.font.SysFont("Arial", 30)

在这些情况下,Arial 几乎总是一个“安全的选择”。同样在使用 pyinstaller 转换之前,将文件名更改为 main.py。也不要忘记将 pygame 更新到 2.0.0.dev6 或更新版本。它现在应该可以工作了。

关于Python 文件 .exe 只是在一瞬间出现,然后立即关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64965665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com