簡體   English   中英

我剛開始 python,我正在嘗試制作剪刀石頭布游戲,但我不知道如何更改字體及其大小

[英]I've just started python and I'm trying to make a Rock ,Paper ,Scissors game and I can't work out how to change the font and its size

這是我的代碼,如果有人可以提供幫助,那將是非常重要的。

import random

a = ["rock", "paper", "scissors"]

def game():
    while True:
        try:
            word = input('Enter rock, paper, or scissors: ')
            if word not in a:
                raise ValueError # this will send it to the print message and back to the input option
            break 
          
        except ValueError:
            print(" You must enter rock, paper, or scissors.")


    comp_draw = random.choice(a)
    print('The computer drew ' + comp_draw)

    if comp_draw == 'rock' and word =='rock':
        print('It was a tie')
    elif comp_draw == 'paper' and word =='paper':
        print('It was a tie')
    elif comp_draw == 'scissors' and word =='scissors':
        print('It was a tie')
    elif comp_draw == 'paper' and word =='rock':
        print('GAME OVER')
    elif comp_draw == 'rock' and word =='paper':
        print('you won!')
    elif comp_draw == 'rock' and word =='scissors':
        print('GAME OVER')
    elif comp_draw == 'scissors' and word =='rock':
        print('you won!')
    elif comp_draw == 'scissors' and word =='paper':
        print('GAME OVER')
    elif comp_draw == 'scissors' and word =='rock':
        print('you won!')


if __name__ == "__main__":
    game()
    while True:
        if input('Would you like to play_again? ') == 'yes':
            game()
        else:
          break
        
      font = 'Arial (sans-serif)' : 'normal' 
        'weight' : 'bold',
        'size'   : 99} 

這就是我嘗試更改字體的方式

font = 'Arial (sans-serif)' : 'normal' 
        'weight' : 'bold',
        'size'   : 99} 

這是我收到的錯誤信息

IndentationError: unindent does not match any outer indentation level on line 48 font = 'Arial (sans-serif)': 'normal' ^ 在 main.py

為了使代碼正常工作,您需要取消縮進字體,在“normal”之后添加一個逗號並添加一個左大括號:

if __name__ == "__main__":
    game()
    while True:
        if input('Would you like to play_again? ') == 'yes':
            game()
        else:
          break
        
font = {'Arial (sans-serif)' : 'normal' ,
       'weight' : 'bold',
       'size'   : 99} 

但是,您沒有使用字體變量,因此它不會改變任何內容。 如前所述,如果您要打印到終端,則無法更改 python 中的字體。

不幸的是,沒有一種簡單的方法來更改打印的字體,它的大小一直到 Python。您可以嘗試使用 shell 的 api 更改控制台字體和大小,但這是非常特定於平台的,並且不容易設置。

您可以做的是使用像pygame這樣的簡單圖形庫,您可以在其中從 python 應用程序中獲取 output 圖形。 您可以查看文檔以了解如何在 pygame 中使用 fonts。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM