python - 无法执行脚本 pygame 错误 dist 不起作用
问题描述
所以我想打包我的代码,但我收到了这个错误Failed to execute script
,我不知道为什么,我确保我的所有文件都在 dist 的同一个位置,并多次检查是否一切正常但出于某种原因我不断收到错误。我也多次重新打包我的代码,但没有奏效。I used pyinstaller
我的代码
import pygame
import random
pygame.init()
window = pygame.display.set_mode((500,500))
pygame.display.set_caption(("Noobs First Game"))
# Start screen
MB = pygame.image.load("Main_bg.png")
MB2 = pygame.image.load("Main_bg2.png")
MB3 = pygame.image.load("Main_bg3.png")
MB4 = pygame.image.load("Main_bg4.png")
# For question's
R = pygame.image.load("Rule.png")
R2 = pygame.image.load("Rule_2.png")
# Sound's and Music
MM = pygame.mixer.music.load("Main_music.ogg")
pygame.mixer.music.play(-1)
BG = pygame.image.load("b_g1.png")
# Pause,Mute, and Unmute
M = pygame.image.load("M_1.png")
M2 = pygame.image.load("M_2.png")
P = pygame.image.load("P_1.png")
P2 = pygame.image.load("P_2.png")
UM = pygame.image.load("UM_1.png")
UM2 = pygame.image.load("UM_2.png")
# Game paused
GP = pygame.image.load("GameP1.png")
GP2 = pygame.image.load("GameP2.png")
GP3 = pygame.image.load("GameP3.png")
GP4 = pygame.image.load("GameP4.png")
# When you lose
DD = pygame.image.load("Died1.png")
DD2 = pygame.image.load("Died2.png")
fps = 30
clock = pygame.time.Clock()
white = (255,255,255)
darkred = (200,0,0)
darkgreen = (0,200,0)
green = (0,255,0)
red = (255,0,0)
black = (0,0,0)
blue = (0,0,255)
darkblue = (0,0,200)
# Button class
class button11():
def __init__(self, color, x,y,width,height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
def draw(self,window,outline=None):
#Call this method to draw the button on the screen
if outline:
pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 60)
text = font.render(self.text, 1, (0,0,0))
window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
def isOver(self, pos):
#Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
def playSoundIfMouseIsOver(self, pos, sound):
if self.isOver(pos):
if not self.over:
eat.play()
self.over = True
else:
self.over = False
white = (250,250,250)
greenbutton5 = button11((0,255,0),175,233,150,55, 'Click Me )')
greenbutton1 = button11((0,255,0),185,320,130,50, 'Click Me )')
greenbutton2 = button11((0,255,0),190,400,120,45, 'Click Me )')
greenbutton6 = button11((0,255,0),10,420,170,60, 'Click Me )')
greenbutton7 = button11((0,255,0),10,420,170,60, 'Click Me )')
def quitgame():
pygame.quit()
def Answers():
an = True
while an:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pos = pygame.mouse.get_pos()
window.fill((255,255,255))
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("", largeText)
TextRect.center = ((700/2),(500/2))
window.blit(TextSurf, TextRect)
button("",10,420,170,60,green,darkgreen,game_intro)
window.blit(R,(0,0))
if greenbutton6.isOver(pos):
window.blit(R2,(0,0))
pygame.display.update()
clock.tick(fps)
#######
# Start Screen
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
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(window, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(window, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",40)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
window.blit(textSurf, textRect)
def quitgame():
pygame.quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pos = pygame.mouse.get_pos()
window.fill((255,255,255))
largeText = pygame.font.Font('freesansbold.ttf',75)
TextSurf, TextRect = text_objects("", largeText)
TextRect.center = ((500/2),(500/2))
window.blit(TextSurf, TextRect)
button("",175,233,150,55,green,darkgreen,main_loop)
button("",185,320,130,50,red,darkred,quitgame)
button("",190,400,120,45,blue,darkblue,Answers)
window.blit(MB,(0,0))
if greenbutton5.isOver(pos):
window.blit(MB2,(0,0))
if greenbutton1.isOver(pos):
window.blit(MB3,(0,0))
if greenbutton2.isOver(pos):
window.blit(MB4,(0,0))
pygame.display.update()
clock.tick(fps)
def main_loop():
global pause
global lose
# Player class
class Player:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 6
self.fall = 0
self.idle = [pygame.image.load("Player_idle1.png"),
pygame.image.load("Player_idle2.png"),
pygame.image.load("Player_idle3.png"),
pygame.image.load("Player_idle4.png"),
pygame.image.load("Player_idle5.png"),
pygame.image.load("Player_idle6.png"),
pygame.image.load("Player_idle7.png"),
pygame.image.load("Player_idle8.png"),
pygame.image.load("Player_idle9.png"),
pygame.image.load("Player_idle10.png")]
self.idlel = [pygame.image.load("Player_lidle1.png"),
pygame.image.load("Player_lidle2.png"),
pygame.image.load("Player_lidle3.png"),
pygame.image.load("Player_lidle4.png"),
pygame.image.load("Player_lidle5.png"),
pygame.image.load("Player_lidle6.png"),
pygame.image.load("Player_lidle7.png"),
pygame.image.load("Player_lidle8.png"),
pygame.image.load("Player_lidle9.png"),
pygame.image.load("Player_lidle10.png")]
self.run = [pygame.image.load("Player_run1.png"),
pygame.image.load("Player_run2.png"),
pygame.image.load("Player_run3.png"),
pygame.image.load("Player_run4.png"),
pygame.image.load("Player_run5.png"),
pygame.image.load("Player_run6.png"),
pygame.image.load("Player_run7.png"),
pygame.image.load("Player_run8.png")]
self.jump = [pygame.image.load("Player_Jump.png")]
self.lrun = [pygame.image.load("Player_lrun1.png"),
pygame.image.load("Player_lrun2.png"),
pygame.image.load("Player_lrun3.png"),
pygame.image.load("Player_lrun4.png"),
pygame.image.load("Player_lrun5.png"),
pygame.image.load("Player_lrun6.png"),
pygame.image.load("Player_lrun7.png"),
pygame.image.load("Player_lrun8.png")]
self.ljump = [pygame.image.load("Player_lJump.png")]
self.direction = "run"
self.direction = "jump"
self.direction = "lrun"
self.direction = "ljump"
self.direction = "idle"
self.direction = "idlel"
self.run = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.run]
self.jump = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.jump]
self.lrun = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.lrun]
self.ljump = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.ljump]
self.idle = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.idle]
self.idlel = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.idlel]
self.isJump = False
self.JumpCount = 10
self.rect = pygame.Rect(x,y,width,height)
self.next_frame_time = 0
self.fps = 10
self.clock = pygame.time.Clock()
self.anim_index = 0
self.jump_height = 2
def get_rect(self):
self.rect.topleft = (self.x,self.y)
return self.rect
def draw(self):
if self.direction == "run":
image_list = self.run
if self.direction == "jump":
image_list = self.jump
if self.direction == "lrun":
image_list = self.lrun
if self.direction == "ljump":
image_list = self.ljump
if self.direction == "idle":
image_list = self.idle
if self.direction == "idlel":
image_list = self.idlel
# Is it time to show next frame?
time_now = pygame.time.get_ticks()
if (time_now > self.next_frame_time):
# seconds till next frame
inter_time_delay = 1000 // self.fps
self.next_frame_time = time_now + inter_time_delay
# switch to next frame
self.anim_index += 1
if self.anim_index >= len(image_list):
self.anim_index = 0
if self.anim_index >= len(image_list):
self.anim_index = 0
player_image = image_list[self.anim_index]
pygame.draw.rect( window, self.color, self.get_rect(), 2 )
player_image = image_list[self.anim_index]
player_rect = player_image.get_rect(center = self.get_rect().center)
player_rect.centerx += 3
player_rect.centery -= 17
window.blit(player_image, player_rect)
# Platform class
class Platform:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.log = pygame.image.load("log.png")
self.rect = pygame.Rect(x,y,width,height)
self.log = pygame.transform.scale(self.log,(self.log.get_width()//4,self.log.get_height()//6))
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
platform_rect = self.log.get_rect(center = self.rect.center)
platform_rect.centerx += 60
platform_rect.centery -= 2
window.blit(self.log,platform_rect)
# Platform2 class
class Platform2:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.log = pygame.image.load("Grass_1.png")
self.rect = pygame.Rect(x,y,width,height)
self.log = pygame.transform.scale(self.log,(self.log.get_width()-90,self.log.get_height()-91))
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
platform_rect = self.log.get_rect(center = self.rect.center)
platform_rect.centerx += 2
platform_rect.centery += 0.2
window.blit(self.log,platform_rect)
class Dirt:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.dirt = pygame.image.load("Dirt_1.png")
self.dirt = pygame.transform.scale(self.dirt,(self.dirt.get_width()-90,self.dirt.get_height()-91))
self.rect = pygame.Rect(x,y,width,height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
dirt_rect = self.dirt.get_rect(center = self.rect.center)
dirt_rect.centerx += 2
dirt_rect.centery += 0.2
window.blit(self.dirt,dirt_rect)
class Water:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 1
self.water = pygame.image.load("water_1.png")
self.water = pygame.transform.scale(self.water,(self.water.get_width()*8,self.water.get_height()*6))
self.rect = pygame.Rect(x,y,width,height)
def draw(self):
self.rect.topleft = (self.x,self.y)
water_rect = self.water.get_rect(center = self.rect.center)
water_rect.centerx += 25
water_rect.centery += 80
window.blit(self.water,water_rect)
# Button class
class button111():
def __init__(self, color, x,y,width,height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
def draw(self,window,outline=None):
#Call this method to draw the button on the screen
if outline:
pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 60)
text = font.render(self.text, 1, (0,0,0))
window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
def isOver(self, pos):
#Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
def playSoundIfMouseIsOver(self, pos, sound):
if self.isOver(pos):
if not self.over:
eat.play()
self.over = True
else:
self.over = False
# displaying Color
white = (255,255,255)
# Drawing stuff
# Drawing player
playerman = Player(255,440,40,40,white)
# Drawing Platform
platform1 = Platform(0,388,130,30,white)
# Drawing Platform2
Platform1 = Platform2(7000,470,1,1,white)
# Drawing Dirt
dirt1 = Dirt(7000,255,35,35.1,white)
# Water
water1 = Water(0,800,500,300,white)
# List
# Putting Platform in a list
platforms = [platform1]
# Putting Platform2 in a list
Platforms = [Platform1]
# Putting Dirt in a list
dirts = [dirt1]
platformGroup = pygame.sprite.Group
Level = [
" l",
"l",
"",
" l",
" ",
"l",
" ",
" l",
" ",
"",
"",
"",
"11111111111111111111",
"22222222222222222222",]
for iy, row in enumerate(Level):
for ix, col in enumerate(row):
if col == "1":
new_platform = Platform2(ix*35, iy*36.4, 35,35.1,(255, 255, 255))
Platforms.append(new_platform)
for iy, row in enumerate(Level):
for ix, col in enumerate(row):
if col == "2":
new_dirt = Dirt(ix*35, iy*36.4, 35,35.1,(255, 255, 255))
dirts.append(new_dirt)
for iy, row in enumerate(Level):
for ix, col in enumerate(row):
if col == "l":
new_platform = Platform(ix*110, iy*60, 130,30,(255, 255, 255))
platforms.append(new_platform)
pause = False
def quitgame():
pygame.quit()
def unpause():
global pause
pause = False
def paused():
greenbutton10 = button111((0,255,0),190,262,120,43, '')
greenbutton11 = button111((0,255,0),200,340,100,35, '')
greenbutton12 = button111((0,255,0),177,437,157,55, '')
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("", largeText)
TextRect.center = ((700/2),(500/2))
window.blit(TextSurf, TextRect)
pos = pygame.mouse.get_pos()
button("",190,262,120,43,green,darkgreen,unpause)
button("",200,340,100,35,red,darkred,quitgame)
button("",177,437,157,55,blue,darkblue,game_intro)
window.blit(GP,(0,0))
if greenbutton10.isOver(pos):
window.blit(GP2,(0,0))
if greenbutton11.isOver(pos):
window.blit(GP3,(0,0))
if greenbutton12.isOver(pos):
window.blit(GP4,(0,0))
pygame.display.update()
clock.tick(15)
# This is what happens when you toch the water
lose = False
def quitgame():
pygame.quit()
def unlose():
global lose
lose = False
def lost():
greenbutton12 = button111((0,255,0),168,285,175,65, '')
while lose:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("", largeText)
TextRect.center = ((700/2),(500/2))
window.blit(TextSurf, TextRect)
pos = pygame.mouse.get_pos()
button("",168,285,175,65,blue,darkblue,game_intro)
window.blit(DD,(0,0))
if greenbutton12.isOver(pos):
window.blit(DD2,(0,0))
pygame.display.update()
clock.tick(15)
# redrawing window
def redrawwindow():
window.fill((0,0,0))
window.blit(BG,(0,0))
# bliting a counter the game
window.blit(text,textRect)
# showing player on the screen
playerman.draw()
# Drawing Platform
for Platform in platforms:
Platform.draw()
# Drawing Platform2
for Platform2 in Platforms:
Platform2.draw()
# Drawing Dirt
for Dirt in dirts:
Dirt.draw()
# Drawing Water
water1.draw()
window.blit(M,(0,0))
window.blit(P,(0,0))
window.blit(UM,(0,0))
pos = pygame.mouse.get_pos()
greenbutton7 = button111((0,255,0),400,146,100,35, '')
greenbutton8 = button111((0,255,0),400,211,100,35, '')
greenbutton9 = button111((0,255,0),400,285,100,35, '')
#greenbutton9.draw(window)
if greenbutton7.isOver(pos):
window.blit(P2,(0,0))
if greenbutton8.isOver(pos):
window.blit(M2,(0,0))
if greenbutton9.isOver(pos):
window.blit(UM2,(0,0))
# The conter and how its going look like
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render(" = "+str(score),True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((150,40))
fps = 30
clock = pygame.time.Clock()
x = 10
y = 10
x_change = 0
y_change = 0
old_x = x
old_y = y
timer = 0
Stimer = 0
# Space down = False
spcdown = False
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if playerman.rect.colliderect(water1.rect):
lose = True
lost()
greenbutton7 = button111((0,255,0),400,146,100,35, '')
pos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN:
if greenbutton7.isOver(pos):
pause = True
paused()
greenbutton8 = button111((0,255,0),400,211,100,35, '')
if event.type == pygame.MOUSEBUTTONDOWN:
if greenbutton8.isOver(pos):
pygame.mixer.music.fadeout(1000)
greenbutton9 = button111((0,255,0),400,285,100,35, '')
if event.type == pygame.MOUSEBUTTONDOWN:
if greenbutton9.isOver(pos):
pygame.mixer.music.play(-1)
if Stimer > 0:
Stimer += 1
if Stimer > 900:
Stimer = 0
water1.y -= water1.speed
if Stimer == 0:
water1.speed += 1
Stimer += 1
for Platform in platforms:
if Platform.y > 510:
Platform.x = random.randint(0, 470)
Platform.y = random.randint(-150, -100)
# Mkaing screen go up
if playerman.y < 250:
delta = 10
playerman.y += delta
for Platform in platforms:
Platform.y += delta
for Platform2 in Platforms:
Platform2.y += delta
for Dirt in dirts:
Dirt.y += delta
water1.y += 9
# Marking screen go down
if playerman.y > 410:
playerman.y -= playerman.fall
for Platform in platforms:
Platform.y -= playerman.fall
for Platform2 in Platforms:
Platform2.y -= playerman.fall
for Dirt in dirts:
Dirt.y -= playerman.fall
water1.y -= playerman.fall
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
x_change = -7
if event.key == pygame.K_a:
x_change = 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
x_change = 0
x += x_change
if x > 500 - playerman.width or x < 0:
x = old_x
# If keys get pressed
keys = pygame.key.get_pressed()
px,py = playerman.x,playerman.y
# Adding one to score every time player jumps
if not keys[pygame.K_SPACE]:
spcdown = False # space released
if keys[pygame.K_SPACE]:
if not spcdown:
score += 1 # if space pressed first time
spcdown = True # space key is pressed
text = font.render(" = "+str(score),True,(255,255,255))
textRect.center = ((150,40))
# Player movment
if keys[pygame.K_a] and px > playerman.speed:
px -= playerman.speed
playerman.direction = "lrun"
elif keys[pygame.K_d] and px < 500 - playerman.width - playerman.speed:
px += playerman.speed
playerman.direction = "run"
else:
if playerman.direction == "run":
playerman.direction = "idle"
else:
if playerman.direction == "lrun":
playerman.direction = "idlel"
if keys[pygame.K_w] and py > playerman.speed:
py -= playerman.speed
if keys[pygame.K_s] and py < 500 - playerman.height - playerman.speed:
py += playerman.speed
# animation for player jump
if playerman.fall > 0 and keys[pygame.K_SPACE]:
if keys[pygame.K_d]:
playerman.direction = "jump"
else:
if playerman.direction == "lrun":
if keys[pygame.K_SPACE]:
playerman.direction = "ljump"
platform_rect_list =[p.rect for p in platforms]
player_rect = playerman.get_rect()
playerman.rect.topleft = (px,py)
playerman.y = py
if player_rect.collidelist(platform_rect_list) < 0:
playerman.x = px
# About isJump
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
# this part lets you jump on platform only the top
collide = False
for Platform in platforms:
if playerman.get_rect().colliderect(Platform.rect):
collide = True
playerman.isJump = False
playerman.y = Platform.rect.top - playerman.height
if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
playerman.x = Platform.rect.left - playerman.width
if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
playerman.x = Platform.rect.right
# Lets Player jump on top of second Platform
for Platform2 in Platforms:
if playerman.get_rect().colliderect(Platform2.rect):
collide = True
playerman.isJump = False
playerman.y = Platform2.rect.top - playerman.height
if playerman.rect.right > Platform2.rect.left and playerman.rect.left < Platform2.rect.left - playerman.width:
playerman.x = Platform2.rect.left - playerman.width
if playerman.rect.left < Platform2.rect.right and playerman.rect.right > Platform2.rect.right + playerman.width:
playerman.x = Platform2.rect.right
# colliding with floor
if playerman.rect.bottom >= 500:
collide = True
playerman.isJump = False
playerman.Jumpcount = 10
playerman.y = 500 - playerman.height
# Jumping
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 0
# Jump Count
else:
if playerman.JumpCount >= 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.5
playerman.JumpCount -= 1
else:
playerman.isJump = False
playerman.JumpCount = 10
redrawwindow()
pygame.display.update()
pygame.quit()
unpause()
unlose()
game_intro()
main_loop()
解决方案
推荐阅读
- javascript - How to randomise increment order JS
- algorithm - 埃德蒙匹配算法。如何从空匹配集开始?
- ios - 使用 flutter_ble_lib 扫描 BLE 设备时出现问题。流超时过早
- python - 将数字添加到字符串 (Python)
- scala - 当同时使用默认参数和多态时,scala 会选择哪种方法?
- excel - VBA数据输入表单的上一个和下一个按钮功能不起作用
- python - 如何在 Python 谷歌云存储库方法 list_blobs 中获取下一个 blob
- django - Django - 如何显示投票应用程序的多个选项
- php - 如何解决在 PHP 和 Mysql 中选择多个列?
- r - 如何使用 R 在布局中绘制 geom_ribbon 以绘制 3 y 轴