本文介绍了使用cx-freeze为pygame制作.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我用Python 3.2创建了一个游戏,并试图将其转换为带有cx-freeze的.exe,但是当我尝试运行.exe时,却出现了错误。I've created a game in Python 3.2 and tried to turn it into a .exe with cx-freeze but when I try to run the .exe, I get an error.游戏import pygame, sys, randomfrom pygame.locals import *pygame.init()mainClock = pygame.time.Clock()windowSurface = pygame.display.set_mode((800, 800))pygame.display.set_caption("Dodger")def game(): WIDTH = 800 HEIGHT = 800 PLAYERSIZE = 20 PLAYERSPEED = 4 ENEMYSIZE = 10 ENEMYSPEED = 3 score = 0 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) GREY = (100, 100, 100) BLUE = (0, 0, 255) Up = False Down = False Left = False Right = False titleFont = pygame.font.SysFont(None, 100) titleText = titleFont.render('Dodger', True, WHITE) titleRect = titleText.get_rect() titleRect.centerx = windowSurface.get_rect().centerx titleRect.centery = 50 menuFont = pygame.font.SysFont(None, 70) startText = menuFont.render('Start', True, WHITE) startRect = startText.get_rect() startRect.centerx = windowSurface.get_rect().centerx startRect.centery = 213 difficultyText = menuFont.render('Difficulty', True, WHITE) difficultyRect = difficultyText.get_rect() difficultyRect.centerx = windowSurface.get_rect().centerx difficultyRect.top = 320 quitText = menuFont.render('Quit', True, WHITE) quitRect = quitText.get_rect() quitRect.centerx = windowSurface.get_rect().centerx quitRect.centery = 582 oneText = menuFont.render('1', True, WHITE) oneRect = oneText.get_rect() oneRect.centerx = 250 oneRect.centery = 421 twoText = menuFont.render('2', True, WHITE) twoRect = twoText.get_rect() twoRect.centerx = 350 twoRect.centery = 421 threeText = menuFont.render('3', True, WHITE) threeRect = threeText.get_rect() threeRect.centerx = 450 threeRect.centery = 421 fourText = menuFont.render('4', True, WHITE) fourRect = fourText.get_rect() fourRect.centerx = 550 fourRect.centery = 421 difficulty = 1 loop = 1 while loop: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() posX, posY = pos if posY > 390 and posY < 452: if posX > 200 and posX < 300: difficulty = 1 elif posX > 300 and posX < 400: difficulty = 2 elif posX > 400 and posX < 500: difficulty = 3 elif posX > 500 and posX < 600: difficulty = 4 elif posY > 120 and posY < 306 and posX > 120 and posX < WIDTH - 120: loop = 0 elif posY > 492 and posY < WIDTH - 120 and posX > 120 and posX < WIDTH - 120: pygame.quit() sys.exit() if event.type == KEYUP: if event.key == ord('1'): difficulty = 1 elif event.key == ord('2'): difficulty = 2 elif event.key == ord('3'): difficulty = 3 elif event.key == ord('4'): difficulty = 4 elif event.key == K_RETURN: loop = 0 if difficulty == 1: choiceRect = pygame.Rect(201, 391, 99, 61) elif difficulty == 2: choiceRect = pygame.Rect(301, 391, 99, 61) elif difficulty == 3: choiceRect = pygame.Rect(401, 391, 99, 61) elif difficulty == 4: choiceRect = pygame.Rect(501, 391, 99, 61) windowSurface.fill(BLACK) #draw main menu box pygame.draw.line(windowSurface, WHITE, (120, 120), (WIDTH - 120, 120)) pygame.draw.line(windowSurface, WHITE, (120, 120), (120, HEIGHT - 120)) pygame.draw.line(windowSurface, WHITE, (WIDTH - 120, HEIGHT - 120), (120, HEIGHT - 120)) pygame.draw.line(windowSurface, WHITE, (WIDTH - 120, HEIGHT - 120), (WIDTH - 120, 120)) #draw lines to divide menu pygame.draw.line(windowSurface, WHITE, (120, 306), (WIDTH - 120, 306)) pygame.draw.line(windowSurface, WHITE, (120, 492), (WIDTH - 120, 492)) pygame.draw.line(windowSurface, WHITE, (200, 390), (WIDTH - 200, 390)) pygame.draw.line(windowSurface, WHITE, (200, 452), (WIDTH - 200, 452)) pygame.draw.line(windowSurface, WHITE, (200, 390), (200, 452)) pygame.draw.line(windowSurface, WHITE, (WIDTH - 200, 390), (WIDTH - 200, 452)) pygame.draw.line(windowSurface, WHITE, (300, 390), (300, 452)) pygame.draw.line(windowSurface, WHITE, (400, 390), (400, 452)) pygame.draw.line(windowSurface, WHITE, (500, 390), (500, 452)) pygame.draw.rect(windowSurface, GREY, choiceRect) windowSurface.blit(titleText, titleRect) windowSurface.blit(startText, startRect) windowSurface.blit(difficultyText, difficultyRect) windowSurface.blit(quitText, quitRect) windowSurface.blit(oneText, oneRect) windowSurface.blit(twoText, twoRect) windowSurface.blit(threeText, threeRect) windowSurface.blit(fourText, fourRect) pygame.display.update() difficulty += 1 Player = pygame.Rect((WIDTH/2)-(PLAYERSIZE/2),(HEIGHT/2) - (PLAYERSIZE/2), PLAYERSIZE, PLAYERSIZE) enemies = [] for enemy in range(0, difficulty): if enemy == 1: enemies.append(pygame.Rect(1, 1, ENEMYSIZE, ENEMYSIZE)) elif enemy == 2: enemies.append(pygame.Rect(WIDTH - PLAYERSIZE, 1, ENEMYSIZE, ENEMYSIZE)) elif enemy == 3: enemies.append(pygame.Rect(1, HEIGHT - PLAYERSIZE, ENEMYSIZE, ENEMYSIZE)) elif enemy == 4: enemies.append(pygame.Rect(WIDTH - PLAYERSIZE, HEIGHT - PLAYERSIZE, ENEMYSIZE, ENEMYSIZE)) loop = 1 while loop: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_UP or event.key == ord('w'): Up = True Down = False if event.key == K_DOWN or event.key == ord('s'): Down = True Up = False if event.key == K_LEFT or event.key == ord('a'): Left = True Right = False if event.key == K_RIGHT or event.key == ord('d'): Right = True Left = False if event.type == KEYUP: if event.key == K_UP or event.key == ord('w'): Up = False if event.key == K_DOWN or event.key == ord('s'): Down = False if event.key == K_LEFT or event.key == ord('a'): Left = False if event.key == K_RIGHT or event.key == ord('d'): Right = False if event.key == K_ESCAPE: pygame.quit() pygame.exit() if Up and Player.top >= 0: Player.top -= PLAYERSPEED if Down and Player.bottom <= HEIGHT: Player.bottom += PLAYERSPEED if Left and Player.left >= 0: Player.left -= PLAYERSPEED if Right and Player.right <= WIDTH: Player.right += PLAYERSPEED i = 0 for enemy in enemies: if enemies[i].top > Player.top: enemies[i].top -= ENEMYSPEED if enemies[i].top < Player.top: enemies[i].top += ENEMYSPEED if enemies[i].left > Player.left: enemies[i].left -= ENEMYSPEED if enemies[i].left < Player.left: enemies[i].left += ENEMYSPEED i += 1 windowSurface.fill(BLACK) pygame.draw.rect(windowSurface, GREEN, Player) i = 0 for enemy in enemies: pygame.draw.rect(windowSurface, RED, enemies[i]) if Player.colliderect(enemies[i]): loop = 0 i += 1 pygame.display.update() mainClock.tick(40) score += 1 score = str(score) endRect = pygame.Rect(windowSurface.get_rect().centerx - 250, windowSurface.get_rect().centery - 75, 500, 150) scoreText = menuFont.render('Score: ' + score, True, RED) scoreRect = scoreText.get_rect() scoreRect.centerx = windowSurface.get_rect().centerx scoreRect.centery = windowSurface.get_rect().centery pygame.draw.rect(windowSurface, BLUE, endRect) windowSurface.blit(scoreText, scoreRect) pygame.display.update() while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYUP: if event.key == K_RETURN: game()game()我的setup.pyMy setup.pyimport sysfrom cx_Freeze import setup, Executablesetup( name = "Dodger", version = "3.2", description = "My Game", executables = [Executable("dodger.py", base = "Win32GUI")])当我尝试运行它时,会弹出一个窗口,并显示以下 错误。When I try to run it, a window pops up and I get the followingerror.编辑:我添加了 import pygame._view到dodger.py,现在我得到这个错误。推荐答案我相信您的问题是您没有使用系统字体。一个cx_freeze exe不知道如何读取 None 时,我将字体从 None 更改为系统字体。使用以下脚本可以找到系统上已安装哪些字体的方法。I believe your problem is the fact that you do not use a system font. A cx_freeze exe does not know how to read None When I had that problem I changed the font from None to a system font. The way you could find what fonts are installed on your system is using the script below.import pygameprint(pygame.font.get_fonts())input() pygame.font.get_fonts()会弹出已安装的所有系统字体的列表。您要做的就是将您的 titleFont = pygame.font.SysFont(None,100)更改为 titleFont = pygame.font.SysFont('miriam ',100)或类似的东西( miriam是我系统上安装的字体)。The pygame.font.get_fonts() will bring up a list of all the system fonts installed. All you have to do is change your titleFont = pygame.font.SysFont(None, 100) to titleFont = pygame.font.SysFont('miriam', 100) or something like that ('miriam' is a font installed on my system). 这篇关于使用cx-freeze为pygame制作.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 10:26