Pygame游戲開發(fā)實例講解之圖形繪制與鍵鼠事件
圖形繪制
格式:
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), 圓心坐標
- radius: 圓形半徑
- width: 圓形厚度
- draw_top_right: 布爾類型, 只畫右上角 1/4, 默認為 None
- draw_top_left: 布爾類型, 只畫左上角 1/4, 默認為 None
- draw_bottom_right: 布爾類型, 只畫右下角 1/4, 默認為 None
- draw_bottom_left: 布爾類型, 只畫左下角 1/4, 默認為 None
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設置背景顏色 screen.fill((255, 205, 232)) # 設置標題 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() # 退出
輸出結果:
繪制矩形
格式:
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), 矩形的坐標和尺寸
- width: 矩形粗細
- 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() # 設置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設置背景顏色 screen.fill((255, 205, 232)) # 設置標題 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() # 退出
輸出結果:
繪制直線
格式:
pygame.draw.line(surface, color, start_pos, end_pos, width=1)
參數(shù):
- surface: 需要繪制的表面
- color: RGB 格式, 顏色
- start_pos: 元組格式 (x, y), 起始坐標
- end_pos: 元組格式 (x, y), 結束坐標
- width: 線粗細
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設置背景顏色 screen.fill((255, 205, 232)) # 設置標題 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() # 退出
輸出結果:
繪制圓弧
格式:
pygame.draw.arc(surface, color, rect, start_angle, stop_angle, width=1)
參數(shù):
- surface: 需要繪制的表面
- color: RGB 格式, 顏色
- rect: 元組格式 (x, y, w, h), 矩形的坐標和尺寸
- start_angle: 起始弧度
- stop_angle: 結束弧度
- width: 圓弧粗細
例子:
import pygame from pygame.locals import * import sys import math # 初始化 pygame pygame.init() # 設置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設置背景顏色 screen.fill((255, 205, 232)) # 設置標題 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() # 退出
輸出結果:
通過math.radians()
方法, 將角度值轉換為弧度.
案例
pygame 實現(xiàn)矩形移動:
import pygame from pygame.locals import * import sys import time # 初始化 pygame pygame.init() # 設置窗口大小 screen = pygame.display.set_mode((500, 500)) # 設置背景顏色 screen.fill((255, 205, 232)) # 設置標題 pygame.display.set_caption("移動的矩形") # 捕獲游戲事件 typelist = [QUIT] # 矩形的初始橫坐標 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()
輸出結果:
鍵鼠事件
鍵盤事件
pygame.key.get_pressed()
會返回一個按鍵列表, 用 True / False 表示鍵盤各個鍵是否按下.
格式:
keys = pygame.key.get_pressed()
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設置字體和字號 (仿宋) myFont = pygame.font.Font("C:\Windows\Fonts\simfang.ttf", 50) # 設置背景顏色 screen.fill((255, 205, 232)) # 設置標題 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()
輸出結果:
鼠標事件
event.pos
獲取事件坐標, event.rel
獲取事件相對位置.
例子:
import pygame from pygame.locals import * import sys # 初始化 pygame pygame.init() # 設置窗口大小 screen = pygame.display.set_mode((600, 400)) # 設置字體和字號 (仿宋) myFont = pygame.font.Font("C:\Windows\Fonts\simfang.ttf", 50) # 設置背景顏色 screen.fill((255, 205, 232)) # 設置標題 pygame.display.set_caption("鼠標事件") # 捕獲游戲事件 typelist = [QUIT] # 初始化變量 mouse_x = mouse_y = 0 # 鼠標位置 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() # 退出 # 獲取鼠標位置 elif event.type == MOUSEMOTION: mouse_x, mouse_y = event.pos move_x, move_y = event.rel # 獲取鼠標按鍵 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)) # 鼠標事件 text_img1 = myFont.render("鼠標位置: " + str(mouse_x) + ", " + str(mouse_y), True, (184, 241, 237)) text_img2 = myFont.render("鼠標相對位置: " + str(move_x) + ", " + str(move_y), True, (184, 241, 237)) text_img3 = myFont.render("鼠標按鈕按下: " + str(mouse_down) + ", " + str(mouse_down_x) + ", " + str(mouse_down_y), True, (184, 241, 237)) text_img4 = myFont.render("鼠標按鈕抬起: " + 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()
輸出結果:
到此這篇關于Pygame游戲開發(fā)實例講解之圖形繪制與鍵鼠事件的文章就介紹到這了,更多相關Pygame圖形繪制與鍵鼠事件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python對象與json數(shù)據(jù)的轉換問題實例詳解
JSON(JavaScript?Object?Notation)?是一種輕量級的數(shù)據(jù)交換格式,很受廣大用戶喜愛,今天通過本文給大家介紹Python對象與json數(shù)據(jù)的轉換問題,需要的朋友可以參考下2022-07-07pytest-sugar?執(zhí)行過程中顯示進度條的腳本分享
Pytest-sugar是一款用來改善控制臺顯示的插件,增加了進度條顯示,使得在用例執(zhí)行過程中可以看到進度條,而且進度條是根據(jù)用例是否通過標注不同顏色,非常醒目,接下來通過本文給大家分享下pytest?sugar?顯示進度條的腳本,感興趣的朋友一起看看吧2022-12-12Python Web框架Django的模型和數(shù)據(jù)庫遷移詳解
Django 是一個極其強大的 Python Web 框架,它提供了許多工具和特性,能夠幫助我們更快速、更便捷地構建 Web 應用,在本文中,我們將會關注 Django 中的模型(Models)和數(shù)據(jù)庫遷移(Database Migrations)這兩個核心概念,需要的朋友可以參考下2023-08-08