要在python中畫圓角矩形,我們可以使用pygame庫。
首先,我們需要導入pygame庫:
import pygame
然后,我們需要定義畫布的尺寸和矩形的參數:
width = 640 height = 480 rect_width = 200 rect_height = 100 radius = 20
接著,我們創建一個pygame的窗口并設置它的尺寸:
pygame.init() screen = pygame.display.set_mode((width, height))
現在,我們可以畫出圓角矩形了:
pygame.draw.rect(screen, (255, 0, 0), (width / 2 - rect_width / 2, height / 2 - rect_height / 2, rect_width, rect_height), radius)
最后,我們需要更新屏幕并保持程序運行,直到用戶關閉窗口:
pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()
完整代碼如下:
import pygame import sys width = 640 height = 480 rect_width = 200 rect_height = 100 radius = 20 pygame.init() screen = pygame.display.set_mode((width, height)) pygame.draw.rect(screen, (255, 0, 0), (width / 2 - rect_width / 2, height / 2 - rect_height / 2, rect_width, rect_height), radius) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()