这是我用来创建构建文件的代码:
import cx_Freeze
executables = [cx_Freeze.Executable("SpeedRacer.py")]
cx_Freeze.setup(
name="SpeedRacer",
options={"build_exe": {"packages": ["pygame"],"include_files": ["SpeedRacerDefaultCar.png"]}},
executables = executables
)
这是我为游戏启动.exe文件时遇到的错误:
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: cHRM chunk does not match sRGB
Fatal Python error: (pygame parachute) Segmentation Fault
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pygame\pkgdata.py". line 67. in getResourc
e
return open(os.path.normpath(path), 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Jordan\\Desk
top\\Python Projects\\Game Ideas\\SpeedRacerGame\\build\\exe.win-amd64-3.4\\libr
ary.zip\\pygame\\freesansbold.ttf'
游戏代码如下:
import pygame
import time
import random
pygame.init()
display_width = 1440
display_height = 900
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (50,126,31)
blue = (34,101,183)
bright_green = (72,185,45)
bright_red = (255,0,0)
block_color = green
car_width = 75 # Image width in Pixels.
gameDisplay = pygame.display.set_mode((1440, 900))
pygame.display.set_caption('SpeedRacer')
clock = pygame.time.Clock()
carImg = pygame.image.load('SpeedRacerDefaultCar.png')
def quitgame():
pygame.quit()
quit()
def things_dodged(count):
font = pygame.font.SysFont(None, 25)
text = font.render("Dodged: "+str(count), True, bright_red)
gameDisplay.blit(text, (0, 0))
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def car(x,y):
gameDisplay.blit(carImg,(x,y))
def text_objects(text, font):
textSurface = font.render(text, True, blue)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.SysFont(None,115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('You Crashed')
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
smallText = pygame.font.SysFont(None,20)
textSurf, textRect = text_objects(msg, smallText,)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
largeText = pygame.font.SysFont(None,115)
TextSurf, TextRect = text_objects("SpeedRacer", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
button("Start",550,575,100,50,green,bright_green,game_loop)
button("Quit",750,575,100,50,red,bright_red,quitgame)
pygame.display.update()
clock.tick(15)
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 4
thing_width = 100
thing_height = 100
Dodged = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
elif event.key == pygame.K_RIGHT:
x_change = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(black)
# things(thingx, thingy, thingw, thingh, color)
things(thing_startx, thing_starty, thing_width, thing_height, block_color)
thing_starty += thing_speed
car(x,y)
things_dodged(Dodged)
if x > display_width - car_width or x < 0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0, display_width)
Dodged += 1
thing_speed += 0.4
thing_width += (Dodged * 0.4)
if y < thing_starty+thing_height:
print('y crossover')
if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width:
print('x crossover')
crash()
pygame.display.update()
clock.tick(120)
game_intro()
game_loop()
pygame.quit()
quit()
我使用cx_Freeze转换为.exe。
答案 0 :(得分:-1)
您好,请尝试查看以下网页,其中解释了如何解决问题,实际上我遇到了同样的问题,我做了他们建议的事情,现在正在运作:
首先我做了:
原始行代码
font = pygame.font.SysFont(None, 48)
按此更改行代码
font = pygame.font.SysFont("Arial", 48)