什么不是字节?

发布于 2025-01-23 11:57:58 字数 5258 浏览 0 评论 0原文

我是这个网站和Pygame的新手,我希望有所帮助。因此,我制作了这款大约180行的太空入侵者克隆,当我在Pycharm上运行它时,它可以正常工作。因此,我决定尝试进一步测试它,并尝试将其转换为.exe文件,这是我的麻烦开始的地方。每当我尝试运行.exe时,此消息出现时,

Traceback (most recent call last):
  File "main.py", line 62, in <module>
    font = pygame.font.Font('freesansbold.ttf',32)
TypeError: expected str, bytes or os.PathLike object, not BytesIO 

我都会使用freecodecamp.org的模板并应用了自己的海关,这是您感兴趣的视频:

时间

import math
import random

import pygame
from pygame import mixer

# Initialize the pygame
pygame.init()

# Create the screen
screen = pygame.display.set_mode((600, 400))



# Background Sound

mixer.music.load('Here he comes.mp3')
mixer.music.play(-1)



# Title and icon
pygame.display.set_caption("Defender of The Blue")
icon = pygame.image.load("Submarine of Vengeance.png")
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load('Protagonist.png')
playerX = 250
playerY = 300
playerX_change = 0

# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load('Antagonist.png'))
    enemyX.append(random.randint(0, 500))
    enemyY.append(random.randint(10, 100))
    enemyX_change.append(0.1)
    enemyY_change.append(20)

# Energy Blast

# Ready - You can't see the bullet state on the screen
# Fire - The blast is currently moving
energyblastImg = pygame.image.load('energy-blasts.png')
energyblastX = 0
energyblastY = 300
energyblastX_change = 0
energyblastY_change = 1
energyblast_state = "ready"

# score

score_value = 0
font = pygame.font.Font('freesansbold.ttf',32)
textX = 10
textY = 10
game_over_font = pygame.font.Font('freesansbold.ttf',50)
# Game Over Text

def show_score(x,y):
    score = font.render("Score :" + str(score_value),True, (250,100,0))
    screen.blit(score, (x, y))

def game_over_text():
    game_over_text = game_over_font.render("GAME OVER",True, (250,0,0))
    screen.blit(game_over_text, (150, 100))


def player(x, y):
    screen.blit(playerImg, (x, y))


def enemy(x, y, i):
    screen.blit(enemyImg[i], (x, y))


def fire_energyblast(x, y):
    global energyblast_state
    energyblast_state = "fire"
    screen.blit(energyblastImg, (x + -1, y + 10))


def isCollison(enemyX, enemyY, energyblastX, energyblastY):
    distance = math.sqrt((math.pow(enemyX - energyblastX, 2)) + (math.pow(enemyY - energyblastY, 2)))
    if distance < 27:
        return True
    else:
        return False


# Game Loop
running = True
while running:

    # RGB = Red, Green, Blue
    screen.fill((0, 0, 128))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # if keystroke is pressed check whether its right or left
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -1
            if event.key == pygame.K_RIGHT:
                playerX_change = 1
            if event.key == pygame.K_SPACE:
                if energyblast_state is "ready":
                    energyblast_sound = mixer.Sound('Energy Blast.mp3')
                    energyblast_sound.play()
                    # Get the current x coordinate of the submarine
                    energyblastX = playerX
                    fire_energyblast(energyblastX, energyblastY)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0

    # 5 = 5 + -0.1 -> 5 = 5 - 0.1
    # 5 = 5 + 0.1

    # Checking for boundaries of submarine so it doesn't go out of bounds
    playerX += playerX_change

    if playerX <= 0:
        playerX = 0
    elif playerX >= 500:
        playerX = 500

    # enemy movement
    for i in range(num_of_enemies):
        # Game Over
        if enemyY[i] > 272:
            for j in range(num_of_enemies):
                enemyY[j] = 2000
            game_over_text()
            break
        enemyX[i] += enemyX_change[i]
        if enemyX[i] <= 0:
            enemyX_change[i] = 1
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 500:
            enemyX_change[i] = -1
            enemyY[i] += enemyY_change[i]

        # Collision
        collison = isCollison(enemyX[i], enemyY[i], energyblastX, energyblastY)
        if collison:
            explosion_Sound = mixer.Sound('Explode.mp3')
            explosion_Sound.play()
            energyblastY = 300
            energyblast_state = "ready"
            score_value += 50
            enemyX[i] = random.randint(0, 500)
            enemyY[i] = random.randint(10, 100)

        enemy(enemyX[i],enemyY[i], i)

    # energy blast movement
    if energyblastY <= 0:
        energyblastY = 300
        energyblast_state = "ready"

    if energyblast_state is "fire":
        fire_energyblast(energyblastX, energyblastY)
        energyblastY -= energyblastY_change


    player(playerX, playerY)
    show_score(textX,textY)
    pygame.display.update()

https://www.youtube.com/watch?v=ffwpglfmi7w 字节是什么。这就像用两次用过的东西取消了吗?我该如何更改?

I'm kind of new to this site and pygame and I was hoping for a bit of help. So I made this space invader clone which is about 180 lines right, when I run it on Pycharm, it works fine. So I decided to try testing it out further and tried converting it into an .exe file which is where my troubles began. Every time I try running the .exe, this message appears

Traceback (most recent call last):
  File "main.py", line 62, in <module>
    font = pygame.font.Font('freesansbold.ttf',32)
TypeError: expected str, bytes or os.PathLike object, not BytesIO 

I used freeCodeCamp.org's template and applied my own customs, here's a video incase you're interested: https://www.youtube.com/watch?v=FfWpgLFMI7w&t=7041s

Here's the code as well:

import math
import random

import pygame
from pygame import mixer

# Initialize the pygame
pygame.init()

# Create the screen
screen = pygame.display.set_mode((600, 400))



# Background Sound

mixer.music.load('Here he comes.mp3')
mixer.music.play(-1)



# Title and icon
pygame.display.set_caption("Defender of The Blue")
icon = pygame.image.load("Submarine of Vengeance.png")
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load('Protagonist.png')
playerX = 250
playerY = 300
playerX_change = 0

# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load('Antagonist.png'))
    enemyX.append(random.randint(0, 500))
    enemyY.append(random.randint(10, 100))
    enemyX_change.append(0.1)
    enemyY_change.append(20)

# Energy Blast

# Ready - You can't see the bullet state on the screen
# Fire - The blast is currently moving
energyblastImg = pygame.image.load('energy-blasts.png')
energyblastX = 0
energyblastY = 300
energyblastX_change = 0
energyblastY_change = 1
energyblast_state = "ready"

# score

score_value = 0
font = pygame.font.Font('freesansbold.ttf',32)
textX = 10
textY = 10
game_over_font = pygame.font.Font('freesansbold.ttf',50)
# Game Over Text

def show_score(x,y):
    score = font.render("Score :" + str(score_value),True, (250,100,0))
    screen.blit(score, (x, y))

def game_over_text():
    game_over_text = game_over_font.render("GAME OVER",True, (250,0,0))
    screen.blit(game_over_text, (150, 100))


def player(x, y):
    screen.blit(playerImg, (x, y))


def enemy(x, y, i):
    screen.blit(enemyImg[i], (x, y))


def fire_energyblast(x, y):
    global energyblast_state
    energyblast_state = "fire"
    screen.blit(energyblastImg, (x + -1, y + 10))


def isCollison(enemyX, enemyY, energyblastX, energyblastY):
    distance = math.sqrt((math.pow(enemyX - energyblastX, 2)) + (math.pow(enemyY - energyblastY, 2)))
    if distance < 27:
        return True
    else:
        return False


# Game Loop
running = True
while running:

    # RGB = Red, Green, Blue
    screen.fill((0, 0, 128))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # if keystroke is pressed check whether its right or left
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -1
            if event.key == pygame.K_RIGHT:
                playerX_change = 1
            if event.key == pygame.K_SPACE:
                if energyblast_state is "ready":
                    energyblast_sound = mixer.Sound('Energy Blast.mp3')
                    energyblast_sound.play()
                    # Get the current x coordinate of the submarine
                    energyblastX = playerX
                    fire_energyblast(energyblastX, energyblastY)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0

    # 5 = 5 + -0.1 -> 5 = 5 - 0.1
    # 5 = 5 + 0.1

    # Checking for boundaries of submarine so it doesn't go out of bounds
    playerX += playerX_change

    if playerX <= 0:
        playerX = 0
    elif playerX >= 500:
        playerX = 500

    # enemy movement
    for i in range(num_of_enemies):
        # Game Over
        if enemyY[i] > 272:
            for j in range(num_of_enemies):
                enemyY[j] = 2000
            game_over_text()
            break
        enemyX[i] += enemyX_change[i]
        if enemyX[i] <= 0:
            enemyX_change[i] = 1
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 500:
            enemyX_change[i] = -1
            enemyY[i] += enemyY_change[i]

        # Collision
        collison = isCollison(enemyX[i], enemyY[i], energyblastX, energyblastY)
        if collison:
            explosion_Sound = mixer.Sound('Explode.mp3')
            explosion_Sound.play()
            energyblastY = 300
            energyblast_state = "ready"
            score_value += 50
            enemyX[i] = random.randint(0, 500)
            enemyY[i] = random.randint(10, 100)

        enemy(enemyX[i],enemyY[i], i)

    # energy blast movement
    if energyblastY <= 0:
        energyblastY = 300
        energyblast_state = "ready"

    if energyblast_state is "fire":
        fire_energyblast(energyblastX, energyblastY)
        energyblastY -= energyblastY_change


    player(playerX, playerY)
    show_score(textX,textY)
    pygame.display.update()

I'm not quite sure what BytesIO is. Is it like having something used twice and cancels out? How can I change this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如荒岛 2025-01-30 11:57:58

您是否确定“ FreeSansbold.ttf”已下载并且在与您试图运行的代码相同的目录中下载,如果由于某种原因无法使用,您应该能够

font = pygame.font.Font('freesansbold.ttf',32)

font = pygame.font.Font('(file path)freesansbold.ttf',32)

Are you certain that "freesansbold.ttf" is downloaded and in the same directory as the code youre trying to run, if this isnt possible for whatever reason you should be able to replace the

font = pygame.font.Font('freesansbold.ttf',32)

with

font = pygame.font.Font('(file path)freesansbold.ttf',32)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文