如何使用Pygame建立文字輸入框?


Pygame 是一個免費且開源的庫,用於使用Python開發多媒體應用程式(如影片遊戲)。它包含圖形和聲音庫,在設計影片遊戲中非常有用。Pygame 建立在簡單直接媒體層 (SDL) 庫之上,該庫提供對硬體和輸入裝置的低階訪問。由於 Pygame 建立在SDL之上,因此它為圖形、聲音和輸入處理提供了一個平臺無關的介面。這意味著您可以編寫一次遊戲或多媒體應用程式,並在多個平臺上執行它,包括 Windows、Mac OS 和 Linux。

要使用pygame,應該對Python語言有基本的瞭解。在本教程結束時,我們將能夠理解 Pygame 的工作原理。它包含多個用於製作影片遊戲和圖形的函式。在安裝 Pygame 之前,系統中應該已經安裝了 Python。

示例 1

在這個示例中,我們首先匯入了 Pygame 和 sys,然後使用“pygame.init()”初始化所有匯入的模組,並定義“clock”以在給定的秒數內重新整理幀。之後,我們設定螢幕顯示模式和標題,然後是字型和文字,然後我們建立了一個矩形並設定了顏色引數。接下來,我們使用多個函式設定輸入框的工作流程。最後,我們使用“pygame.display.flip()”函式顯示它。

import pygame,sys
pygame.init()
clock = pygame.time.Clock()
screen= pygame.display.set_mode((500,500)) 
pygame.display.set_caption('text input')
user_text= ' '
font = pygame.font.SysFont('frenchscript',32) 
input_rect = pygame.Rect(200,200,140,40)
active=False
color_ac=pygame.Color('green')
color_pc=pygame.Color('red')
color=color_pc
active=False
while True:    
   for events in pygame.event.get():
      if events.type==pygame.QUIT:
         pygame.quit()
         sys.exit()
              
      if events.type==pygame.MOUSEBUTTONDOWN:
         if input_rect.collidepoint(events.pos):
            active = True

      if events.type == pygame.KEYDOWN:
         if active == True:
         
            if events.key == pygame.K_BACKSPACE:
               user_text = user_text[:-1]
            else:
               user_text+=events.unicode
          
   screen.fill('blue')
   if active:
      color=color_ac
   else:
      color=color_pc
      
   pygame.draw.rect(screen,color,input_rect,2)
     
      
   text_surface = font.render(user_text,True,(255,255,255))
   screen.blit(text_surface,(input_rect.x + 5, input_rect.y +5))
   input_rect.w=max(100,text_surface.get_width() + 10)
   pygame.display.flip()
   clock.tick(60)

輸出

示例 2

這是另一個類似的示例,我們僅使用 pygame 模組建立了一個文字輸入框,然後定義了其他方法來建立文字框。最後,我們使用“pygame.display()”函式顯示它。

import pygame
pygame.init()
screen = pygame.display.set_mode((700, 200))
clock = pygame.time.Clock()

font = pygame.font.SysFont(None, 50)
text = ""
input_active = True

run = True
while run:
   clock.tick(60)
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         run = False
      elif event.type == pygame.MOUSEBUTTONDOWN:
         input_active = True
         text = ""
      elif event.type == pygame.KEYDOWN and input_active:
         if event.key == pygame.K_RETURN:
            input_active = False
         elif event.key == pygame.K_BACKSPACE:
            text =  text[:-1]
         else:
            text += event.unicode

      screen.fill('purple')
      text_surf = font.render(text, True, (255, 0, 0))
      screen.blit(text_surf, text_surf.get_rect(center = screen.get_rect().center))
      pygame.display.flip()

pygame.quit()
exit()

輸出

結論

我們瞭解到 Pygame 是一個流行的庫,用於建立影片遊戲和多媒體應用程式。開發人員可以使用這個著名的庫建立多個遊戲。它提供了一個易於使用的介面來建立和操作圖形。任何人都可以使用它在螢幕上繪製形狀、影像和動畫。您還可以使用它來建立視覺效果,例如粒子系統和滾動背景。總的來說,Pygame 是一個功能強大且用途廣泛的庫,可用於各種應用程式。

透過學習 Pygame,每個人都可以培養圖形程式設計、聲音程式設計、輸入處理、遊戲開發和跨平臺開發方面的技能。憑藉其易於使用的介面和廣泛的功能,Pygame 是任何對使用 Python 建立遊戲或多媒體應用程式感興趣的人的絕佳選擇。

更新於: 2023年5月11日

1K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告