Python文件.exe会立即出现,然后立即关闭

Minhdepzai

我是pygame的新手。当互联网中断时,我已经编写了与恐龙游戏类似的游戏代码。但是,当我从.py转换为.exe时,它会立即打开并立即关闭,尽管主脚本运行良好。我一直在使用的命令是pyinstaller file.py --onefile,它使可执行文件在运行时会立即关闭。我已经检查过Wheather,但没有错误(我在外面没有任何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()

抱歉,如果我的英语不好并且我的代码知识太有限

cactus_car

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

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

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

在这些情况下,Arial几乎总是“安全赌注”。同样在使用pyinstaller转换之前,将文件名更改为main.py另外不要忘记更新pygame2.0.0.dev6或更高版本。现在应该可以工作了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Process.Start()使.exe文件闪现打开,然后立即再次关闭

来自分类Dev

为什么从 Windows 批处理文件启动的 ffmpeg.exe 会立即关闭而不等待用户输入?

来自分类Dev

关闭Python脚本,然后立即重新启动

来自分类Dev

启动时随机出现10个字母的.exe文件会占用150MB的RAM,然后立即杀死自己

来自分类Dev

Outlook说“您的配置文件未配置”,然后立即关闭

来自分类Dev

Python可执行文件立即关闭

来自分类Dev

Python 2.7:立即写入文件

来自分类Dev

Eclipse打开,然后在出现图形后立即关闭

来自分类Dev

'Workbook.open'错误-打开文件后立即关闭文件

来自分类Dev

无法理解以下内容:malloc后立即释放;打开后立即关闭文件

来自分类Dev

打开PDF的意图会在显示文件之前立即关闭

来自分类Dev

FileStream.Close()不会立即关闭文件句柄

来自分类Dev

允许立即关闭电源的非日志文件系统?

来自分类Dev

Nautilus 文件管理器立即关闭我打开它

来自分类Dev

批处理文件似乎不运行并立即关闭

来自分类Dev

立即将Python stdout写入文件

来自分类Dev

mmap:映射文件会立即加载到内存中吗?

来自分类Dev

上传到heroku的文件会立即删除吗?

来自分类Dev

为什么文件夹会立即返回?

来自分类Dev

Python(PyQT)中的GUI窗口立即闪烁并关闭?

来自分类Dev

Python3 urllib.request不会立即关闭连接

来自分类Dev

Python,Pygame程序在尝试打开时立即关闭

来自分类Dev

python装饰器会立即调用返回的回调吗?

来自分类Dev

定制PC会立即断电并立即关闭

来自分类Dev

cronjob打开,然后立即关闭ssh隧道

来自分类Dev

双击启动后,.exe将立即关闭

来自分类Dev

播放文件管理器中访问的文件时,VLC 立即关闭

来自分类Dev

Python 脚本可执行文件立即崩溃

来自分类Dev

加载外部js文件,然后立即引用创建的对象[Vuejs2]

Related 相关文章

  1. 1

    Process.Start()使.exe文件闪现打开,然后立即再次关闭

  2. 2

    为什么从 Windows 批处理文件启动的 ffmpeg.exe 会立即关闭而不等待用户输入?

  3. 3

    关闭Python脚本,然后立即重新启动

  4. 4

    启动时随机出现10个字母的.exe文件会占用150MB的RAM,然后立即杀死自己

  5. 5

    Outlook说“您的配置文件未配置”,然后立即关闭

  6. 6

    Python可执行文件立即关闭

  7. 7

    Python 2.7:立即写入文件

  8. 8

    Eclipse打开,然后在出现图形后立即关闭

  9. 9

    'Workbook.open'错误-打开文件后立即关闭文件

  10. 10

    无法理解以下内容:malloc后立即释放;打开后立即关闭文件

  11. 11

    打开PDF的意图会在显示文件之前立即关闭

  12. 12

    FileStream.Close()不会立即关闭文件句柄

  13. 13

    允许立即关闭电源的非日志文件系统?

  14. 14

    Nautilus 文件管理器立即关闭我打开它

  15. 15

    批处理文件似乎不运行并立即关闭

  16. 16

    立即将Python stdout写入文件

  17. 17

    mmap:映射文件会立即加载到内存中吗?

  18. 18

    上传到heroku的文件会立即删除吗?

  19. 19

    为什么文件夹会立即返回?

  20. 20

    Python(PyQT)中的GUI窗口立即闪烁并关闭?

  21. 21

    Python3 urllib.request不会立即关闭连接

  22. 22

    Python,Pygame程序在尝试打开时立即关闭

  23. 23

    python装饰器会立即调用返回的回调吗?

  24. 24

    定制PC会立即断电并立即关闭

  25. 25

    cronjob打开,然后立即关闭ssh隧道

  26. 26

    双击启动后,.exe将立即关闭

  27. 27

    播放文件管理器中访问的文件时,VLC 立即关闭

  28. 28

    Python 脚本可执行文件立即崩溃

  29. 29

    加载外部js文件,然后立即引用创建的对象[Vuejs2]

热门标签

归档