将pygame脚本编译为exe PYTHON时出现的问题
在我学习python的过程中,我为石头、布、剪刀创建了一个pygame。我昨晚很晚才完成,今天早上我正试图把它编译成一个可执行文件。当我通过.py文件运行游戏时,它运行得非常好,但是我希望能够与没有安装python的朋友共享它。我试着用Pyinstaller来做这件事。我使用“pip install pyinstaller”来安装它,经过一点麻烦之后,它被正确安装了(必须向PATH变量添加一个文件地址)。我使用命令“pyinstaller filename.py--onefile”来创建可执行文件。然而,在创建之后,出现了两个问题将pygame脚本编译为exe PYTHON时出现的问题,python,compilation,pygame,exe,Python,Compilation,Pygame,Exe,在我学习python的过程中,我为石头、布、剪刀创建了一个pygame。我昨晚很晚才完成,今天早上我正试图把它编译成一个可执行文件。当我通过.py文件运行游戏时,它运行得非常好,但是我希望能够与没有安装python的朋友共享它。我试着用Pyinstaller来做这件事。我使用“pip install pyinstaller”来安装它,经过一点麻烦之后,它被正确安装了(必须向PATH变量添加一个文件地址)。我使用命令“pyinstaller filename.py--onefile”来创建可执行文
from random import randint
import pygame as pg
pg.init()
# Creating the Game Window
screen = pg.display.set_mode((800,600))
bg = pg.image.load('Data/background_blue.jpg')
pg.display.set_caption('Rock, Paper, Scissors')
icon = pg.image.load('Data/console.png')
pg.display.set_icon(icon)
comicsans50 = pg.font.SysFont('Comic Sans', 50)
comicsans25 = pg.font.SysFont('Comic Sans', 25)
score_text = comicsans50.render('Score: ', 1, (255,0,255))
initialScore = '0 - 0'
initialScore = comicsans50.render(initialScore, 1, (255,0,255))
fps = 60
CLOCK = pg.time.Clock()
# Variables
version = 'V1.0'
color_black = (0,0,0)
playerMove = ''
computer_move = ''
game_outcome = 0
first_run = True
# Game Images
rockIMG = pg.image.load('Data/rock.png')
rockIMG = pg.transform.scale(rockIMG, (200,200))
paperIMG = pg.image.load('Data/paper.png')
paperIMG = pg.transform.scale(paperIMG, (200,200))
scissorsIMG = pg.image.load('Data/scissors.png')
scissorsIMG = pg.transform.scale(scissorsIMG, (200,200))
screenIMG = pg.image.load('Data/screen.jpg')
screenIMG = pg.transform.scale(screenIMG, (600, 300))
initialScreen = pg.transform.scale(screenIMG, (200,200))
computerRockIMG = pg.image.load('Data/rock_flipped.png')
computerRockIMG = pg.transform.scale(computerRockIMG, (200, 200))
computerPaperIMG = pg.image.load('Data/paper_flipped.png')
computerPaperIMG = pg.transform.scale(computerPaperIMG, (200,200))
computerScissorsIMG = pg.image.load('Data/scissors_flipped.png')
computerScissorsIMG = pg.transform.scale(computerScissorsIMG, (200,200))
def playerMove_function(move):
"""Defines the playerMove variable with the player's choice"""
global player_move_IMG, playerMove
if move == 'Rock':
playerMove = 'Rock'
player_move_IMG = rockIMG
elif move == 'Paper':
playerMove = 'Paper'
player_move_IMG = paperIMG
elif move == 'Scissors':
playerMove = 'Scissors'
player_move_IMG = scissorsIMG
else:
playerMove = 'Initiate'
player_move_IMG = initialScreen
return player_move_IMG
def computerMove_function(computer_move_num):
global computer_move_IMG, computer_move, ihatemylife
ihatemylife = computer_move_num
if computer_move_num == 1:
computer_move == 'Rock'
computer_move_IMG = computerRockIMG
elif computer_move_num == 2:
computer_move == 'Paper'
computer_move_IMG = computerPaperIMG
elif computer_move_num == 3:
computer_move == 'Scissors'
computer_move_IMG = computerScissorsIMG
elif computer_move_num == 4:
computer_move_IMG = initialScreen
return computer_move_IMG
def updateScore(winner_state):
"""Updates the score with the parameter of the winner"""
global playerScore, computerScore
if winner_state == 'Player':
playerScore += 1
computerScore += 0
elif winner_state == 'Computer':
computerScore += 1
playerScore += 0
elif winner_state == 'Tie':
playerScore += 0
computerScore += 0
if first_run is not True:
if winner_state != 'Tie':
winner = winner_state + ' wins! '
else:
winner = winner_state
winner = comicsans25.render(winner, 1, (255,0,255))
screen.blit(winner,(5,30))
combinedScore = str(playerScore) + ' - ' + str(computerScore)
combinedScore = comicsans50.render(combinedScore, 1, (255,0,255))
draw_score(combinedScore)
def draw_score(state):
if first_run is True:
screen.blit(initialScore, (400,25))
global playerScore, computerScore
playerScore = 0
computerScore = 0
else:
screen.blit(state, (400,25))
screen.blit(score_text, (250,25))
class button():
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,win,outline=None):
# Call this method to draw the button on the screen
if outline:
pg.draw.rect(win, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
pg.draw.rect(win, self.color, (self.x,self.y,self.width,self.height),0)
if self.text != '':
if self.text == 'Restart' or self.text == 'Exit':
font = pg.font.SysFont('Comic Sans', 25)
else:
font = pg.font.SysFont('Comic Sans', 60)
text = font.render(self.text, 1, (0,0,0))
win.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 check_winner():
# Game Outcomes
# 1 means game tie
# 2 means computer win
# 3 means player win
global game_outcome
# If the player's move is Rock:
if playerMove == 'Rock' and ihatemylife == 1:
game_outcome = 1
elif playerMove == 'Rock' and ihatemylife == 2:
game_outcome = 2
elif playerMove == 'Rock' and ihatemylife == 3:
game_outcome = 3
# If the player's move is paper:
if playerMove == 'Paper' and ihatemylife == 1:
game_outcome = 3
elif playerMove == 'Paper' and ihatemylife == 2:
game_outcome = 1
elif playerMove == 'Paper' and ihatemylife == 3:
game_outcome = 2
# If the player's move is scissors:
if playerMove == 'Scissors' and ihatemylife == 1:
game_outcome = 2
elif playerMove == 'Scissors' and ihatemylife == 2:
game_outcome = 3
elif playerMove == 'Scissors' and ihatemylife == 3:
game_outcome = 1
rockButton = button((255,0,255), 50, 475, 200, 100, 'Rock')
paperButton = button((255,0,255), 300, 475, 200, 100, 'Paper')
scissorsButton = button((255,0,255), 550, 475, 200, 100, 'Scissors')
exitButton = button((255,0,255), 700, 0, 100, 25, 'Exit')
restartButton = button((255,0,255), 0, 0, 100, 25, 'Restart')
def game(move, thecomputermove):
# Draws the screen
screen.fill((0, 0, 0))
screen.blit(bg, (0, 0))
rockButton.draw(screen)
paperButton.draw(screen)
scissorsButton.draw(screen)
screen.blit(screenIMG, (100,75))
exitButton.draw(screen)
restartButton.draw(screen)
global first_run
if first_run is True:
draw_score(initialScore)
# Game code
global turn_player, current_move, computer_move
turn_player = False
turn_computer = False
current_move = 2
playerMove = ''
for i in range(2):
# Switches game move
if current_move % 2 == 0:
turn_player = True
elif current_move % 2 != 0:
turn_computer = True
if turn_player is True:
screen.blit(playerMove_function(move), (105,150))
current_move +=1
# Makes the computer's move
if turn_computer is True:
screen.blit(computerMove_function(thecomputermove), (495,150))
current_move +=1
check_winner()
# Updates the score
if game_outcome == 1:
updateScore('Tie')
elif game_outcome == 2:
updateScore('Computer')
elif game_outcome == 3:
updateScore('Player')
first_run = False
def reset_game_scores():
global playerScore, computerScore, first_run
first_run = True
draw_score(initialScore)
game('Screen', 4)
reset_game_scores()
while True:
for event in pg.event.get():
mouse_pos = pg.mouse.get_pos()
if event.type == pg.MOUSEBUTTONDOWN:
if rockButton.isOver(mouse_pos) and turn_player is True:
current_move += 1
game('Rock', randint(1,3))
elif scissorsButton.isOver(mouse_pos) and turn_player is True:
current_move += 1
game('Scissors', randint(1,3))
elif paperButton.isOver(mouse_pos) and turn_player is True:
current_move += 1
game('Paper', randint(1,3))
if exitButton.isOver(mouse_pos):
pg.quit()
if restartButton.isOver(mouse_pos):
reset_game_scores()
if event.type == pg.QUIT:
pg.quit()
CLOCK.tick(fps)
pg.display.update()
pg.quit()
来自随机导入randint
导入pygame作为pg
第init页()
#创建游戏窗口
屏幕=pg.display.set_模式((800600))
bg=pg.image.load('Data/background_blue.jpg'))
pg.display.set_标题(“石头、布、剪刀”)
icon=pg.image.load('Data/console.png'))
pg.display.set_图标(图标)
comicsans50=pg.font.SysFont('ComicSans',50)
comicsans25=pg.font.SysFont('ComicSans',25)
score_text=comicsans50.render('score:',1,(255,0255))
初始分数='0-0'
initialScore=comicsans50.render(initialScore,1,(255,0255))
fps=60
时钟=pg.time.CLOCK()
#变数
版本='V1.0'
颜色_黑色=(0,0,0)
playerMove=“”
计算机移动=“”
游戏结果=0
首次运行=真
#游戏图像
rockIMG=pg.image.load('Data/rock.png')
rockIMG=pg.transform.scale(rockIMG,(200200))
paperIMG=pg.image.load('Data/paper.png')
paperIMG=pg.transform.scale(paperIMG,(200200))
scissorsIMG=pg.image.load('Data/scissors.png')
scissorsIMG=pg.transform.scale(scissorsIMG,(200200))
screenIMG=pg.image.load('Data/screen.jpg'))
screenIMG=pg.transform.scale(screenIMG,(600300))
初始屏幕=pg.transform.scale(屏幕img,(200200))
computerRockIMG=pg.image.load('Data/rock\u fliped.png'))
computerRockIMG=pg.transform.scale(computerRockIMG,(200200))
computerPaperIMG=pg.image.load('Data/paper\u fliped.png'))
computerPaperIMG=pg.transform.scale(computerPaperIMG,(200200))
computerScissorsIMG=pg.image.load('Data/scissors\u fliped.png'))
computerScissorsIMG=pg.transform.scale(computerScissorsIMG,(200200))
def playerMove_功能(移动):
“”“根据玩家的选择定义playerMove变量”“”
全球玩家移动,玩家移动
如果move==“Rock”:
playerMove=‘摇滚乐’
玩家\u移动\u IMG=rockIMG
elif move==“纸张”:
playerMove=‘纸张’
玩家移动图片=纸张图片
elif move==“剪刀”:
playerMove=‘剪刀’
玩家移动=剪刀
其他:
playerMove='Initiate'
玩家移动=初始屏幕
返回玩家\u移动\u IMG
def计算机移动功能(计算机移动数量):
全球计算机移动,计算机移动,ihatemylife
ihatemylife=计算机移动数量
如果计算机\u move\u num==1:
电脑移动==‘摇滚乐’
计算机移动
elif computer_move_num==2:
计算机移动==“纸张”
计算机\u移动\u IMG=计算机纸张IMG
elif computer_move_num==3:
计算机移动==“剪刀”
计算机移动=计算机剪刀
elif computer_move_num==4:
计算机\u移动\u图像=初始屏幕
返回计算机\u移动\u图像
def updateScore(赢家州):
“”“使用获胜者的参数更新分数”“”
全球球员核心,计算机评分
如果获胜者的状态==‘玩家’:
playerScore+=1
计算机分数+=0
elif winner_state==“计算机”:
计算机分数+=1
playerScore+=0
elif winner_state==‘平局’:
playerScore+=0
计算机分数+=0
如果第一次运行不正确:
如果你的州是赢家结':
winner=winner\u state+“wins!”
其他:
胜利者=胜利者\州
winner=comicsans25.render(winner,1,(255,0255))
银幕布利特(优胜者,(5,30))
组合分数=str(玩家核心)+'-'+str(计算机分数)
combinedScore=comicsans50.render(combinedScore,1,(255,0255))
抽签得分(组合得分)
def draw_分数(状态):
如果第一次运行为真:
屏幕blit(初始分数,(400,25))
全球球员核心,计算机评分
playerScore=0
计算机分数=0
其他:
屏幕blit(状态,(400,25))
屏幕。blit(分数文本,(250,25))
类按钮():
定义初始值(自身、颜色、x、y、宽度、高度、文本=“”):
self.color=颜色
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.text=文本
def draw(自我、胜利、轮廓=无):
#调用此方法在屏幕上绘制按钮
如果概述:
pg.draw.rect(win,outline,(self.x-2,self.y-2,self.width+4,self.height+4),0)
pg.draw.rect(win,self.color,(self.x,self.y,self.width,self.height),0)
如果self.text!='':
如果self.text==“重新启动”或self.text==“退出”:
字体=