尝试使用pyinstaller将.py
脚本转换为.exe
文件时出现错误:TypeError
类似字节的对象不需要' str'
我使用pygame制作了一个简单的蛇游戏,我希望将其转换为.exe
文件。
以下是代码:
import pygame
import random
pygame.init()
black = (0, 0, 0)
red = (255, 0, 0)
green = (164, 199, 141)
blue = (0, 0, 255)
smallfont = pygame.font.SysFont(None, 25)
medfont = pygame.font.SysFont(None, 50)
largefont = pygame.font.SysFont(None, 80)
scorefont = pygame.font.SysFont(None, 30)
pygame.display.set_caption("Wąż v1.0")
clock = pygame.time.Clock()
snake_size = 10
food_size = 10
class window:
display_width = 800
display_height = 600
FPS = 15
gameDisplay = pygame.display.set_mode((window.display_width, window.display_height))
def scoreMessage(score):
text = scorefont.render("Wynik: " + str(score), True, black)
gameDisplay.blit(text, [16, 6])
def levelMessage(level):
text = scorefont.render("Poziom: " + str(level), True, black)
gameDisplay.blit(text, [116, 6])
def intro():
intro = True
cursor = 2
with open('wynik', 'r') as f:
wynik = sorted(map(int, f.readline().split(',')))
print(wynik)
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
cursor = 0
if event.key == pygame.K_DOWN:
cursor = 1
if event.key == pygame.K_RETURN and cursor == 0:
gameLoop()
if event.key == pygame.K_RETURN and cursor == 1:
pygame.quit()
quit()
if cursor == 0:
message_to_screen("Zakończ", green, 80)
message_to_screen("Zakończ", black, 80)
message_to_screen("Graj", green, 40)
message_to_screen("Graj", blue, 40)
pygame.display.update()
if cursor == 1:
message_to_screen("Graj", green, 40)
message_to_screen("Graj", black, 40)
message_to_screen("Zakończ", green, 80)
message_to_screen("Zakończ", blue, 80)
pygame.display.update()
if cursor == 2:
gameDisplay.fill(green)
message_to_screen("Wąż", red, -200, "large")
message_to_screen("Graj", black, 40)
message_to_screen("Zakończ", black, 80)
message_to_screen("Twoim celem jest zjedzenie jak największej ilości jabłek które losowo pojawiają sią na ekranie.", black, -120)
message_to_screen("Uważaj na ściany oraz swój ogon któy wydłuża się wraz ze zjedzeniem jabłka.", black, -90)
message_to_screen("Pamiętaj, że wraz z Twoim wynikiem zwiększa się prędkość węża.", black, -60)
message_to_screen("Do sterowania używaj strzałek.", black, -30)
message_to_screen("Najlepszy wynik: " + str(wynik[-1]), red, 0)
message_to_screen("_______________________________________________________________________", black, 10)
pygame.display.update()
clock.tick(15)
def outro():
game_over = True
cursor2 = 2
with open('wynik', 'r') as f:
wynik = sorted(map(int, f.readline().split(',')))
print(wynik)
while game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(green)
if str(score) > str(wynik[-1]):
message_to_screen("Gratulacje, uzyskałeś najlepszy wynik!", red, -50, "medium")
message_to_screen("Koniec gry!", red, -100, "large")
message_to_screen("Zagraj ponownie", black, 10)
message_to_screen("Zakończ", black, 50)
pygame.display.update()
clock.tick(100)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
cursor2 = 0
if event.key == pygame.K_DOWN:
cursor2 = 1
if event.key == pygame.K_RETURN and cursor2 == 0:
gameLoop()
if event.key == pygame.K_RETURN and cursor2 == 1:
pygame.quit()
quit()
if cursor2 == 0:
message_to_screen("Zakończ", green, 50)
message_to_screen("Zakończ", black, 50)
message_to_screen("Zagraj ponownie", green, 10)
message_to_screen("Zagraj ponownie", blue, 10)
pygame.display.update()
if cursor2 == 1:
message_to_screen("Zagraj ponownie", green, 10)
message_to_screen("Zagraj ponownie", black, 10)
message_to_screen("Zakończ", green, 50)
message_to_screen("Zakończ", blue, 50)
pygame.display.update()
def snake(snake_size, snakeList):
for XY in snakeList:
pygame.draw.rect(gameDisplay, black, [XY[0], XY[1], snake_size, snake_size])
def text_objects(text, color, size):
if size == "small":
textSurface = smallfont.render(text, True, color)
elif size == "medium":
textSurface = medfont.render(text, True, color)
elif size == "large":
textSurface = largefont.render(text, True, color)
return textSurface, textSurface.get_rect()
def message_to_screen(msg, color, y_displace=0, size="small"):
textSurf, textRect = text_objects(msg, color, size)
textRect.center = (window.display_width / 2), (window.display_height / 2) + y_displace
gameDisplay.blit(textSurf, textRect)
def gameLoop():
gameExit = False
gameOver = False
x = window.display_width / 2
y = window.display_height / 2
x_change = 0
y_change = 0
global score
score = 0
level = 0
snakeList = []
snakeLength = 1
randAppleX = round(random.randrange(15, window.display_width - food_size - 10) / 10.0) * 10.0
randAppleY = round(random.randrange(30, window.display_height - food_size - 10) / 10.0) * 10.0
while not gameExit:
if gameOver == True:
outro()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -snake_size
y_change = 0
elif event.key == pygame.K_RIGHT:
x_change = snake_size
y_change = 0
elif event.key == pygame.K_UP:
y_change = -snake_size
x_change = 0
elif event.key == pygame.K_DOWN:
y_change = snake_size
x_change = 0
x += x_change
y += y_change
gameDisplay.fill(green)
pygame.draw.rect(gameDisplay, black, [0, 0, 10, 600])
pygame.draw.rect(gameDisplay, black, [10, 0, 780, 30])
pygame.draw.rect(gameDisplay, green, [10, 5, 780, 20])
pygame.draw.rect(gameDisplay, black, [790, 0, 10, 600])
pygame.draw.rect(gameDisplay, black, [10, 590, 780, 10])
scoreMessage(score)
levelMessage(level)
if x > window.display_width - 10:
gameOver = True
wynik = open('wynik', 'a')
wynik.write(',' + str(score))
elif x < 10:
gameOver = True
wynik = open('wynik', 'a')
wynik.write(',' + str(score))
elif y > window.display_height - 10:
gameOver = True
wynik = open('wynik', 'a')
wynik.write(',' + str(score))
elif y < 30:
gameOver = True
wynik = open('wynik', 'a')
wynik.write(',' + str(score))
pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, food_size, food_size])
snakeHead = []
snakeHead.append(x)
snakeHead.append(y)
snakeList.append(snakeHead)
if len(snakeList) > snakeLength:
del snakeList[0]
for eachSegment in snakeList[:-1]:
if eachSegment == snakeHead:
gameOver = True
snake(snake_size, snakeList)
pygame.display.update()
if x == randAppleX and y == randAppleY:
randAppleX = round(random.randrange(15, window.display_width - food_size - 10) / 10.0) * 10.0
randAppleY = round(random.randrange(30, window.display_height - food_size - 10) / 10.0) * 10.0
snakeLength += 1
score += 1
if score <= 4:
clock.tick(window.FPS)
level = 1
if score > 4 and score <= 9:
clock.tick(20)
level = 2
if score > 9:
clock.tick(25)
level = 3
pygame.quit()
quit()
intro()
gameLoop()
outro()
提前致谢