Pygame游戲開(kāi)發(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: 布爾類(lèi)型, 只畫(huà)右上角 1/4, 默認(rèn)為 None
- draw_top_left: 布爾類(lèi)型, 只畫(huà)左上角 1/4, 默認(rèn)為 None
- draw_bottom_right: 布爾類(lèi)型, 只畫(huà)右下角 1/4, 默認(rèn)為 None
- draw_bottom_left: 布爾類(lèi)型, 只畫(huà)左下角 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é)果:

通過(guò)math.radians()方法, 將角度值轉(zhuǎn)換為弧度.
案例
pygame 實(shí)現(xiàn)矩形移動(dòng):
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("移動(dòng)的矩形")
# 捕獲游戲事件
typelist = [QUIT]
# 矩形的初始橫坐標(biāo)
pos_x = 300
pos_y = 250
# 矩形移動(dòng)距離
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é)果:

鍵鼠事件
鍵盤(pán)事件
pygame.key.get_pressed()會(huì)返回一個(gè)按鍵列表, 用 True / False 表示鍵盤(pán)各個(gè)鍵是否按下.
格式:
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è)置字體和字號(hào) (仿宋)
myFont = pygame.font.Font("C:\Windows\Fonts\simfang.ttf", 50)
# 設(shè)置背景顏色
screen.fill((255, 205, 232))
# 設(shè)置標(biāo)題
pygame.display.set_caption("鍵盤(pán)事件")
# 捕獲游戲事件
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))
# 鍵盤(pán)事件
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獲取事件相對(duì)位置.
例子:
import pygame
from pygame.locals import *
import sys
# 初始化 pygame
pygame.init()
# 設(shè)置窗口大小
screen = pygame.display.set_mode((600, 400))
# 設(shè)置字體和字號(hào) (仿宋)
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)相對(duì)位置: " + 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游戲開(kāi)發(fā)實(shí)例講解之圖形繪制與鍵鼠事件的文章就介紹到這了,更多相關(guān)Pygame圖形繪制與鍵鼠事件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
YOLOv5車(chē)牌識(shí)別實(shí)戰(zhàn)教程(八)Web應(yīng)用與API開(kāi)發(fā)
這篇文章主要介紹了YOLOv5車(chē)牌識(shí)別實(shí)戰(zhàn)教程(八)Web應(yīng)用與API開(kāi)發(fā),在這個(gè)教程中,我們將一步步教你如何使用YOLOv5進(jìn)行車(chē)牌識(shí)別,幫助你快速掌握YOLOv5車(chē)牌識(shí)別技能,需要的朋友可以參考下2023-04-04
詳解JavaScript編程中的window與window.screen對(duì)象
這篇文章主要介紹了JavaScript編程中的window與window.screen對(duì)象,是JS在瀏覽器中視圖編程的基礎(chǔ),需要的朋友可以參考下2015-10-10
Python之ThreadPoolExecutor線程池問(wèn)題
這篇文章主要介紹了Python之ThreadPoolExecutor線程池問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
pytorch:實(shí)現(xiàn)簡(jiǎn)單的GAN示例(MNIST數(shù)據(jù)集)
今天小編就為大家分享一篇pytorch:實(shí)現(xiàn)簡(jiǎn)單的GAN示例(MNIST數(shù)據(jù)集),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python實(shí)現(xiàn)Kmeans聚類(lèi)算法
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)Kmeans聚類(lèi)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
pyinstaller打包python3.6和PyQt5中各種錯(cuò)誤的解決方案匯總
pyinstaller是打包python很方便的一個(gè)套件,我們可以很輕易地使用他,下面這篇文章主要給大家介紹了關(guān)于pyinstaller打包python3.6和PyQt5中各種錯(cuò)誤解決的相關(guān)資料,需要的朋友可以參考下2022-08-08

