網(wǎng)站開發(fā)與維護(hù)課程設(shè)計(jì)seo關(guān)鍵詞優(yōu)化價(jià)格
導(dǎo)語
pygame是一個(gè)跨平臺(tái)Python庫(pygame news),專門用來開發(fā)游戲。pygame主要為開發(fā)、設(shè)計(jì)2D電子游戲而生,提供圖像模塊(image)、聲音模塊(mixer)、輸入/輸出(鼠標(biāo)、鍵盤、顯示屏)模塊等。使用pygame,理論上可以開發(fā)設(shè)計(jì)市面上所有的2D類型游戲。
?正文
1、繪制線條?
我們可以使用 pygame.draw.line()函數(shù)來繪制直線。
pygame.draw.line(screen,線段的顏色,起點(diǎn)坐標(biāo),終點(diǎn)坐標(biāo),線寬)
pygame.draw.line(screen ,lightgreen, (300,0), (300,600), linewidth)
import pygame #導(dǎo)包
from pygame.locals import*
import sysblack=0,0,0
lightgreen=144,238,144pygame.init() #初始化
screen = pygame.display.set_mode(size=(600,600),flags=0)
#繪制一個(gè)600*600的框框while True:for event in pygame.event.get():if event.type in (QUIT,KEYDOWN):sys.exit()#python的退出程序linewidth=4 #“線”的粗細(xì)pygame.draw.line(screen,lightgreen,(300,0),(300,600),linewidth)pygame.display.update() # 刷新展示
2、繪制網(wǎng)格?
通過循環(huán)繪制一個(gè)網(wǎng)格大小為三十像素的網(wǎng)絡(luò)?
import pygame #導(dǎo)包
from pygame.locals import*
import sysblack=0,0,0
lightgreen=144,238,144screen_width=600
screen_height=600pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height),flags=0)
#繪制一個(gè)600*600的框框# 設(shè)置網(wǎng)格大小
grid_size = 30 # 每個(gè)網(wǎng)格的大小為30個(gè)像素# 繪制網(wǎng)格
for i in range(0, screen_width, grid_size):pygame.draw.line(screen, lightgreen, (i, 0), (i, screen_height))
for i in range(0, screen_height, grid_size):pygame.draw.line(screen, lightgreen, (0, i), (screen_width, i))while True:for event in pygame.event.get():if event.type in (QUIT,KEYDOWN):sys.exit()#python的退出程序linewidth=4pygame.display.update() # 刷新展示
3、繪制圓
pygame.draw.circle(screen,lightgreen,position,radius,linewidth)
通過pygame.draw.circle()可以畫圓
- screen代表屏幕
- lightgreen代表著圓的顏色
- position代表著圓心所在的位置
- radius代表著圓的半徑
- linewidth代表著線的粗細(xì)?
import pygame #導(dǎo)包
from pygame.locals import*
import sysblack=0,0,0
lightgreen=144,238,144screen_width=600
screen_height=600pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height),flags=0)
#繪制一個(gè)600*600的框框# 主循環(huán)
while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()color = 255,255,255position = 300,250radius = 100width = 10linewidth=4screen.fill(color)pygame.draw.circle(screen,lightgreen,position,radius,linewidth)pygame.display.update() # 刷新展示
?
4、繪制矩形
pygame.draw.rect()
?是pygame庫中的一個(gè)函數(shù),用于在給定的屏幕上繪制一個(gè)矩形。
函數(shù)的參數(shù)解釋如下:
- screen: 這是你要在其上繪制矩形的pygame Surface 對(duì)象。
- lightgreen: 這是矩形的顏色。它是一個(gè)包含RGB值的元組,例如 (144, 238, 144)。
- pos: 這是矩形左上角的坐標(biāo) + 以左上標(biāo)點(diǎn)為原點(diǎn)的右下角坐標(biāo),下面給出了具體例子。
- width: 這是矩形的寬度。這是一個(gè)整數(shù)。
import pygame #導(dǎo)包
from pygame.locals import*
import sysscreen_width=600
screen_height=600
pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height))
pygame.display.set_caption("這是標(biāo)題")
pos_x = 300
pos_y =300while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()lightgreen=144,238,144width = 0pos = pos_x, pos_y, 300, 300pygame.draw.rect(screen, lightgreen, pos, width)#300*300的矩形pygame.display.update()
?繪制右下角的矩形區(qū)域:
?5、繪制一個(gè)會(huì)動(dòng)的矩形
設(shè)置屏幕的寬度和高度為600x600像素。 定義矩形的橫向和縱向移動(dòng)速度為0.14像素/幀。? 每次循環(huán)將背景填充為黑色。 根據(jù)速度更新矩形的位置。 當(dāng)矩形超出屏幕邊界時(shí),改變其速度的方向,使其反向移動(dòng)。 使用pygame.draw.rect()方法繪制矩形,設(shè)置顏色為黃色,寬度為0(實(shí)心矩形)。
import pygame #導(dǎo)包
from pygame.locals import*
import sysscreen_width=600
screen_height=600
pygame.init() #初始化
screen = pygame.display.set_mode(size=(screen_width,screen_height))
pygame.display.set_caption("這是標(biāo)題")
pos_x = 300
pos_y =300
vel_x = 0.14
vel_y = 0.1#粗略滴可以看作矩形的移動(dòng)速度
while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()screen.fill((0,0,0))#每次循環(huán)都要將背景置為黑色pos_x += vel_xpos_y += vel_y#當(dāng)矩形超過屏幕范圍后返回if pos_x > 500 or pos_x < 0:vel_x = -vel_xif pos_y > 500 or pos_y < 0:vel_y = -vel_y
#繪制矩形color = 255, 255, 1width = 0pos = pos_x,pos_y, 100, 100pygame.draw.rect(screen,color,pos,width)pygame.display.update()
?視頻沒發(fā)上來,有興趣的自己運(yùn)行一下吧。
?6、還有哪些常用的繪制圖形方法?
Pygame 提供了多種繪制圖形的方法,這些方法主要用于在 Surface 對(duì)象上繪制基本的幾何形狀。以下是一些常用的 Pygame 繪制圖形的方法:
-
pygame.draw.polygon()
: 用于繪制多邊形。你需要提供一個(gè)點(diǎn)的列表來定義多邊形的頂點(diǎn),以及多邊形的顏色。 -
pygame.draw.ellipse()
: 用于繪制橢圓。你需要指定一個(gè)矩形區(qū)域,橢圓將在這個(gè)區(qū)域內(nèi)繪制,同時(shí)還需要指定橢圓的顏色。 -
pygame.draw.arc()
: 用于繪制圓弧。你需要指定一個(gè)矩形區(qū)域,圓弧將在這個(gè)區(qū)域內(nèi)繪制,同時(shí)還需要指定圓弧的起始和結(jié)束角度、顏色。 -
pygame.draw.lines()
: 用于繪制一系列相連的線段,可以創(chuàng)建開放或閉合的多邊形。你需要提供一個(gè)點(diǎn)的列表來定義線段的頂點(diǎn),以及線段的顏色和寬度。 -
pygame.draw.aaline()
?和?pygame.draw.aalines()
: 用于繪制抗鋸齒線段,可以平滑線段的邊緣。這兩個(gè)方法只能繪制一個(gè)像素寬的線段,不支持設(shè)置線寬。
小結(jié)
在本篇文章中,主要介紹了如何繪制一條線、圓形、矩形、以及簡(jiǎn)單地實(shí)現(xiàn)了將矩形“動(dòng)起來”,每個(gè)例子都有其對(duì)應(yīng)的完整代碼,可以直接復(fù)制使用。