Pygame游戲開發(fā)實(shí)例講解之圖形繪制與鍵鼠事件
圖形繪制
格式:
pygame.draw.circle(surface, color, center, radius, width=0, draw_top_right=None, draw_top_left=None, draw_bottom_left=None, draw_bottom_right=None)
參數(shù):
- surface: 需要繪制的表面
- color: RGB 格式, 圓形的顏色
- center: 元組格式 (x, y), 圓心坐標(biāo)
- radius: 圓形半徑
- width: 圓形厚度
- draw_top_right: 布爾類型, 只畫右上角 1/4, 默認(rèn)為 None
- draw_top_left: 布爾類型, 只畫左上角 1/4, 默認(rèn)為 None
- draw_bottom_right: 布爾類型, 只畫右下角 1/4, 默認(rèn)為 None
- draw_bottom_left: 布爾類型, 只畫左下角 1/4, 默認(rèn)為 None
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設(shè)置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設(shè)置背景顏色 screen.fill((255, 205, 232)) # 設(shè)置標(biāo)題 pygame.display.set_caption("繪制圓") # 繪制圓 pygame.draw.circle(screen, (184, 241, 237), (200, 200), 100, 12, draw_top_left=True) pygame.draw.circle(screen, (217, 184, 241), (400, 200), 100, 12) # 更新顯示 pygame.display.update() # 捕獲游戲事件 typelist = [QUIT] while True: # 獲取事件 for event in pygame.event.get(): # 接收到退出事件, 退出程序 if event.type in typelist: sys.exit() # 退出
輸出結(jié)果:
繪制矩形
格式:
pygame.draw.rect(surface, color, rect, width=0, border_radius=0, border_top_left_radius=-1, border_top_right_radius=-1, border_bottom_left_radius=-1, border_bottom_right_radius=-1)
參數(shù):
- surface: 需要繪制的表面
- color: RGB 格式, 顏色
- rect: 元組格式 (x, y, w, h), 矩形的坐標(biāo)和尺寸
- width: 矩形粗細(xì)
- border_radius: 使矩形有圓角, 圓角的半徑
- border_top_left_radius: 矩形左上角的圓角半徑
- border_top_right_radius: 矩形右上角的圓角半徑
- border_bottom_left_radius: 矩形左下角的圓角半徑
- border_bottom_right_radius: 矩形右下角的圓角半徑
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設(shè)置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設(shè)置背景顏色 screen.fill((255, 205, 232)) # 設(shè)置標(biāo)題 pygame.display.set_caption("繪制矩形") # 繪制矩形 pygame.draw.rect(screen, (184, 241, 204), (50, 200, 150, 150), 12, border_radius=25) # 圓角矩形 pygame.draw.rect(screen, (184, 241, 237), (250, 200, 150, 150), 12, border_top_right_radius=25) # 右上角圓角 pygame.draw.rect(screen, (217, 184, 241), (450, 200, 120, 120), 12) # 更新顯示 pygame.display.update() # 捕獲游戲事件 typelist = [QUIT] while True: # 獲取事件 for event in pygame.event.get(): # 接收到退出事件, 退出程序 if event.type in typelist: sys.exit() # 退出
輸出結(jié)果:
繪制直線
格式:
pygame.draw.line(surface, color, start_pos, end_pos, width=1)
參數(shù):
- surface: 需要繪制的表面
- color: RGB 格式, 顏色
- start_pos: 元組格式 (x, y), 起始坐標(biāo)
- end_pos: 元組格式 (x, y), 結(jié)束坐標(biāo)
- width: 線粗細(xì)
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設(shè)置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設(shè)置背景顏色 screen.fill((255, 205, 232)) # 設(shè)置標(biāo)題 pygame.display.set_caption("繪制線") # 繪制線 pygame.draw.line(screen, (184, 241, 237), (200, 200), (400, 200), 10) # 直線 pygame.draw.line(screen, (217, 184, 241), (200, 200), (400, 300), 10) # 斜線 # 更新顯示 pygame.display.update() # 捕獲游戲事件 typelist = [QUIT] while True: # 獲取事件 for event in pygame.event.get(): # 接收到退出事件, 退出程序 if event.type in typelist: sys.exit() # 退出
輸出結(jié)果:
繪制圓弧
格式:
pygame.draw.arc(surface, color, rect, start_angle, stop_angle, width=1)
參數(shù):
- surface: 需要繪制的表面
- color: RGB 格式, 顏色
- rect: 元組格式 (x, y, w, h), 矩形的坐標(biāo)和尺寸
- start_angle: 起始弧度
- stop_angle: 結(jié)束弧度
- width: 圓弧粗細(xì)
例子:
import pygame from pygame.locals import * import sys import math # 初始化 pygame pygame.init() # 設(shè)置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設(shè)置背景顏色 screen.fill((255, 205, 232)) # 設(shè)置標(biāo)題 pygame.display.set_caption("繪制圓弧") # 繪制圓弧 pygame.draw.arc(screen, (184, 241, 237), (100, 100, 200, 200), math.radians(0), math.radians(180), 10) pygame.draw.arc(screen, (217, 184, 241), (400, 100, 200, 250), math.radians(90), math.radians(270), 10) # 更新顯示 pygame.display.update() # 捕獲游戲事件 typelist = [QUIT] while True: # 獲取事件 for event in pygame.event.get(): # 接收到退出事件, 退出程序 if event.type in typelist: sys.exit() # 退出
輸出結(jié)果:
通過math.radians()
方法, 將角度值轉(zhuǎn)換為弧度.
案例
pygame 實(shí)現(xiàn)矩形移動:
import pygame from pygame.locals import * import sys import time # 初始化 pygame pygame.init() # 設(shè)置窗口大小 screen = pygame.display.set_mode((500, 500)) # 設(shè)置背景顏色 screen.fill((255, 205, 232)) # 設(shè)置標(biāo)題 pygame.display.set_caption("移動的矩形") # 捕獲游戲事件 typelist = [QUIT] # 矩形的初始橫坐標(biāo) pos_x = 300 pos_y = 250 # 矩形移動距離 vel_x = 2 vel_y = 1 while True: # 獲取事件 for event in pygame.event.get(): # 接收到退出事件, 退出程序 if event.type in typelist: sys.exit() # 退出 # 刷新背景 screen.fill((255, 205, 232)) # 刷新矩形 pos_x += vel_x pos_y += vel_y # 邊緣反彈 if pos_x >= 400 or pos_x <= 0: vel_x = -vel_x if pos_y >= 400 or pos_y <= 0: vel_y = -vel_y # 繪制矩形 pygame.draw.rect(screen, (217, 184, 241), (pos_x, pos_y, 100, 100), 0) time.sleep(0.01) # 更新顯示 pygame.display.update()
輸出結(jié)果:
鍵鼠事件
鍵盤事件
pygame.key.get_pressed()
會返回一個按鍵列表, 用 True / False 表示鍵盤各個鍵是否按下.
格式:
keys = pygame.key.get_pressed()
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設(shè)置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設(shè)置字體和字號 (仿宋) myFont = pygame.font.Font("C:\Windows\Fonts\simfang.ttf", 50) # 設(shè)置背景顏色 screen.fill((255, 205, 232)) # 設(shè)置標(biāo)題 pygame.display.set_caption("鍵盤事件") # 捕獲游戲事件 typelist = [QUIT] while True: # 獲取事件 for event in pygame.event.get(): # 接收到退出事件, 退出程序 if event.type == QUIT: sys.exit() # 退出 # 獲取按鍵 keys = pygame.key.get_pressed() # 刷新背景 screen.fill((255, 205, 232)) # 鍵盤事件 if keys[K_LEFT]: # 顯示文字 text_img1 = myFont.render("按下左箭頭", True, (184, 241, 237)) screen.blit(text_img1, (10, 50)) if keys[K_RIGHT]: text_img2 = myFont.render("按下右箭頭", True, (184, 241, 237)) screen.blit(text_img2, (10, 120)) # 更新顯示 pygame.display.update()
輸出結(jié)果:
鼠標(biāo)事件
event.pos
獲取事件坐標(biāo), event.rel
獲取事件相對位置.
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設(shè)置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設(shè)置字體和字號 (仿宋) myFont = pygame.font.Font("C:\Windows\Fonts\simfang.ttf", 50) # 設(shè)置背景顏色 screen.fill((255, 205, 232)) # 設(shè)置標(biāo)題 pygame.display.set_caption("鼠標(biāo)事件") # 捕獲游戲事件 typelist = [QUIT] # 初始化變量 mouse_x = mouse_y = 0 # 鼠標(biāo)位置 move_x = move_y = 0 mouse_down = 0 # 按鍵按下 mouse_down_x = mouse_down_y = 0 # 按鍵按下位置 mouse_up = 0 # 按鍵抬起 mouse_up_x = mouse_up_y = 0 # 按鍵按下位置 while True: # 獲取事件 for event in pygame.event.get(): # 接收到退出事件, 退出程序 if event.type == QUIT: sys.exit() # 退出 # 獲取鼠標(biāo)位置 elif event.type == MOUSEMOTION: mouse_x, mouse_y = event.pos move_x, move_y = event.rel # 獲取鼠標(biāo)按鍵 elif event.type == MOUSEBUTTONDOWN: mouse_down = event.button mouse_down_x, mouse_down_y = event.pos elif event.type == MOUSEBUTTONUP: mouse_up = event.button mouse_up_x, mouse_up_y = event.pos # 獲取按鍵 keys = pygame.key.get_pressed() # 刷新背景 screen.fill((255, 205, 232)) # 鼠標(biāo)事件 text_img1 = myFont.render("鼠標(biāo)位置: " + str(mouse_x) + ", " + str(mouse_y), True, (184, 241, 237)) text_img2 = myFont.render("鼠標(biāo)相對位置: " + str(move_x) + ", " + str(move_y), True, (184, 241, 237)) text_img3 = myFont.render("鼠標(biāo)按鈕按下: " + str(mouse_down) + ", " + str(mouse_down_x) + ", " + str(mouse_down_y), True, (184, 241, 237)) text_img4 = myFont.render("鼠標(biāo)按鈕抬起: " + str(mouse_up) + ", " + str(mouse_up_x) + ", " + str(mouse_up_y), True, (184, 241, 237)) screen.blits([(text_img1, (10, 50)), (text_img2, (10, 100)), (text_img3, (10, 150)), (text_img4, (10, 200))]) # 更新顯示 pygame.display.update()
輸出結(jié)果:
到此這篇關(guān)于Pygame游戲開發(fā)實(shí)例講解之圖形繪制與鍵鼠事件的文章就介紹到這了,更多相關(guān)Pygame圖形繪制與鍵鼠事件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中pycharm編輯器界面風(fēng)格修改方法
這篇文章主要介紹了Python中pycharm編輯器界面風(fēng)格修改方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Python對象與json數(shù)據(jù)的轉(zhuǎn)換問題實(shí)例詳解
JSON(JavaScript?Object?Notation)?是一種輕量級的數(shù)據(jù)交換格式,很受廣大用戶喜愛,今天通過本文給大家介紹Python對象與json數(shù)據(jù)的轉(zhuǎn)換問題,需要的朋友可以參考下2022-07-07Python應(yīng)用開發(fā)頻繁假死的問題分析及解決
最近在開發(fā)一款自動化的應(yīng)用,但是,在測試時,卻發(fā)現(xiàn)了問題,當(dāng)我點(diǎn)擊暫停任務(wù)后,此時子線程被阻塞,如果我這個時候點(diǎn)擊停止,那么就會任務(wù)結(jié)束,之后,如果我再點(diǎn)擊開始運(yùn)行,整個應(yīng)用就會卡死,所以本文介紹了Python應(yīng)用開發(fā)頻繁假死的問題分析及解決,需要的朋友可以參考下2024-08-08pytest-sugar?執(zhí)行過程中顯示進(jìn)度條的腳本分享
Pytest-sugar是一款用來改善控制臺顯示的插件,增加了進(jìn)度條顯示,使得在用例執(zhí)行過程中可以看到進(jìn)度條,而且進(jìn)度條是根據(jù)用例是否通過標(biāo)注不同顏色,非常醒目,接下來通過本文給大家分享下pytest?sugar?顯示進(jìn)度條的腳本,感興趣的朋友一起看看吧2022-12-12Python Web框架Django的模型和數(shù)據(jù)庫遷移詳解
Django 是一個極其強(qiáng)大的 Python Web 框架,它提供了許多工具和特性,能夠幫助我們更快速、更便捷地構(gòu)建 Web 應(yīng)用,在本文中,我們將會關(guān)注 Django 中的模型(Models)和數(shù)據(jù)庫遷移(Database Migrations)這兩個核心概念,需要的朋友可以參考下2023-08-08