[英]Python file .exe just appear in a moment and close immediately after that
I'm new to pygame.我是 pygame 的新手。 I have made a code about the game similar to dinosaur game when the internet is interupt.
当互联网中断时,我制作了一个类似于恐龙游戏的游戏代码。 But when I convert from .py to .exe it just open in a moment and close immediately although main script runs fine.
但是当我从 .py 转换为 .exe 时,它会立即打开并立即关闭,尽管主脚本运行良好。 The command I've been using was pyinstaller file.py --onefile which make a executable that keeps closing immediately when i run it.
我一直在使用的命令是 pyinstaller file.py --onefile 它使一个可执行文件在我运行时立即关闭。 I have checked wheather there is a error but it doesn't( i dont you any image.png outside)
我已经检查过是否有错误,但它没有(我在外面没有任何 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()
sorry if my english is bad and my knowledge of code is too limited对不起,如果我的英语不好,我的代码知识太有限
Fonts are often the issue here, same goes for audio files.字体通常是这里的问题,音频文件也是如此。 Try replacing:
尝试更换:
font_style = pygame.font.SysFont(None, 30)
with和
font_style = pygame.font.SysFont("Arial", 30)
Arial is pretty much always a "safe bet" in these cases.在这些情况下,Arial 几乎总是一个“安全的赌注”。 Also before converting with pyinstaller change your file name to
main.py
.同样在使用 pyinstaller 转换之前,将文件名更改为
main.py
。 Also do not forget to update pygame
to 2.0.0.dev6
or newer.另外不要忘记将
pygame
更新到2.0.0.dev6
或更新版本。 It should work now.它现在应该可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.