python - TypeError:不支持解码str,我如何在Pygame中解码字符串
问题描述
我正在开发井字游戏程序,但是当我尝试解码分配给我的东西时,您能帮我解决它吗?如果您无法修复它,还有另一种方法可以做到这一点,如果您想要我自己的代码,您也可以使用此代码:)
这是代码:
# Initalize pygame
pygame.init()
# Creating the Window
win = pygame.display.set_mode((500 , 500))
# Font
font = pygame.font.Font('freesansbold.ttf', 32)
# Variables
Blackk = 0 , 0 , 0
Redd = 255 , 0 , 0
Bluee = 0 , 0 , 255
Character = ""
# X or O
x_o = random.randint( 0 , 1 )
if x_o == 1:
Character == "X"
elif x_o == 0:
Character == "O"
STR_xo = str("You ARE \n" , Character)
# Text
You_R = font.render((STR_xo) , True , Blackk , (240 , 240 , 240))
# Make it Rect
textRect = You_R.get_rect()
# Title And Icon
pygame.display.set_caption("Tic Tac Toe")
#TitIco = pygame.image.load()
#pygame.display.set_icon()
# Main Game Loop
RunNow = True
while RunNow:
for event in pygame.event.get():
win.fill((240 , 240 , 240 ))
pygame.draw.rect(win, Blackk, pygame.Rect(150, 0, 20, 500))
pygame.draw.rect(win, Blackk, pygame.Rect(335, 0, 20, 500))
pygame.draw.rect(win, Blackk, pygame.Rect(0, 150, 500, 20))
pygame.draw.rect(win, Blackk, pygame.Rect(0, 335, 500, 20))
win.blit(You_R, textRect)
if event.type == pygame.QUIT:
RunNow = False
pygame.display.update()
解决方案
只需将字符串与+
:
STR_xo = str("You ARE \n" , Character)
STR_xo = "You ARE" + Character
或使用格式化的字符串文字:
STR_xo = f"You ARE {Character}"
但是,如果你想在 Pygame 中渲染一个包含多行的字符串,则需要单独渲染每一行。请参阅在 pygame 中使用多行渲染文本:
You_R_surf = font.render("You ARE" , True , Blackk , (240 , 240 , 240))
Character_surf = font.render( Character, True , Blackk , (240 , 240 , 240))
# [...]
RunNow = True
while RunNow:
for event in pygame.event.get():
if event.type == pygame.QUIT:
RunNow = False
win.fill((240 , 240 , 240 ))
pygame.draw.rect(win, Blackk, pygame.Rect(150, 0, 20, 500))
pygame.draw.rect(win, Blackk, pygame.Rect(335, 0, 20, 500))
pygame.draw.rect(win, Blackk, pygame.Rect(0, 150, 500, 20))
pygame.draw.rect(win, Blackk, pygame.Rect(0, 335, 500, 20))
win.blit(You_R_surf, (0, 0))
win.blit(Character_surf, (0, 32))
pygame.display.update()
另外还有错别字。赋值运算符=
代替==
:
x_o = random.randint( 0 , 1 )
if x_o == 1:
Character = "X"
elif x_o == 0:
Character = "O"
推荐阅读
- angular - 如何在数据表中按日期排序,Angular Material
- matlab - 从 MATLAB 网站获取 MATLAB 链路预算分析文件
- java - 如何在 Java 中读取 JSON 文件?使用 JSON.simple 库
- django - Web 应用程序用户界面开发
- google-chrome-extension - 新的 Microsoft Edge (Chromium) 在我的 Windows 计算机上安装扩展的位置是什么?
- oracle - 使用选择的游标plsql
- python - Keras InceptionV3 TypeError: unhashable type: 'Dimension'
- laravel - Laravel 系统的一对多关系
- javascript - 第二次点击后如何销毁cookie
- javascript - Javascript 自动将 s 添加到我的表格元素中