pygame學(xué)習(xí)筆記(2):畫點(diǎn)的三種方法和動(dòng)畫實(shí)例
1、單個(gè)像素(畫點(diǎn))
利用pygame畫點(diǎn)主要有三種方法:
方法一:畫長(zhǎng)寬為1個(gè)像素的正方形
import pygame,sys
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
pygame.draw.rect(screen,[0,0,0],[150,50,1,1],1) #畫1*1的矩形,線寬為1,這里不能是0,因?yàn)?*1無空白區(qū)域。
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
方法二:畫個(gè)直徑為1的圓
import pygame,sys
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
pygame.draw.circle(screen,[0,0,0],[150,200],1,1)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
方法三:這種方法并不是畫上去的,而是改變了surface上某個(gè)點(diǎn)的顏色,這樣看上去像是畫了一個(gè)點(diǎn)screen.set_at()。另外,如果要得到某個(gè)像素的顏色,可以使用screen.get_at()。
import pygame,sys
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
screen.set_at([150,150],[255,0,0])#將150,150改為紅色。
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
2、連接多個(gè)點(diǎn)形成線
pygame.draw.lines()方法可以將多個(gè)點(diǎn)連接成為線。該方法有5個(gè)參數(shù):surface表面、顏色、閉合線或者非閉合線(如果閉合為True,否則為False),點(diǎn)的列表,線寬。pygame.draw.lines(surface,[color],False/True,plotpoints,1)。下面的例子畫出了一條馬路,具體如下:
import pygame,sys
def lineleft(): #畫馬路左邊界
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():#畫馬路右邊界
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():#畫馬路中間虛線
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
lineleft()
lineright()
linemiddle()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
3、引用圖像
在pygame中引用圖像最簡(jiǎn)單的以夷伐夷是image函數(shù)。下面在馬路的實(shí)例中,加入一輛汽車。首先pygame.image.load()函數(shù)從硬盤加載一個(gè)圖像,并創(chuàng)建一個(gè)名為my_car的對(duì)象。這里,my_car是一個(gè)surface,不過是存在內(nèi)存中,并未顯示出來,然后用blit(塊移)方法將my_car復(fù)制到screen表面上,從而顯示出來。具體代碼如下:
import pygame,sys
def lineleft():
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
def loadcar(): #載入car圖像
my_car=pygame.image.load('ok1.jpg') #當(dāng)前文件夾下的ok1.jpg文件
screen.blit(my_car,[320,320])
pygame.display.flip()
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
lineleft()
lineright()
linemiddle()
loadcar()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
素材:ok1.jpg
4、動(dòng)畫
計(jì)算機(jī)動(dòng)畫實(shí)際上就是把圖像從一個(gè)地方移動(dòng)到另一個(gè)地方,同時(shí)幾個(gè)連接動(dòng)作交待顯示就會(huì)產(chǎn)生逼真的效果。因此,在做動(dòng)畫中,最基本要考慮的因素主要是三個(gè),一是時(shí)間,什么時(shí)間移動(dòng),多長(zhǎng)時(shí)間變下一個(gè)動(dòng)作,二是位置,從什么位置到什么位置,三是動(dòng)作,前后兩個(gè)動(dòng)作的連續(xù)性。在這個(gè)例子中,因?yàn)檐囀歉┮暤?,所以車輪轉(zhuǎn)動(dòng)實(shí)際是看不到的,所以不用考慮連續(xù)動(dòng)作的變化,而是只考慮車的位置和多長(zhǎng)時(shí)間移動(dòng)即可。第一步pygame.time.delay()來實(shí)現(xiàn)時(shí)間延遲;第二步利用pygame.draw.rect()把原來位置的圖像覆蓋掉;第三步screen.blit()在新位置引入圖像。下面的例子實(shí)現(xiàn)了汽車從駛?cè)氲今偝龅倪^程。
import pygame,sys
def lineleft():
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
def loadcar(yloc):
my_car=pygame.image.load('ok1.jpg')
locationxy=[310,yloc]
screen.blit(my_car,locationxy)
pygame.display.flip()
if __name__=='__main__':
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
lineleft()
lineright()
linemiddle()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
for looper in range(480,-140,-50):
pygame.time.delay(200)
pygame.draw.rect(screen,[255,255,255],[310,(looper+132),83,132],0)
loadcar(looper)
相關(guān)文章
python?gravis庫實(shí)現(xiàn)圖形數(shù)據(jù)可視化實(shí)例探索
這篇文章主要為大家介紹了python?gravis庫實(shí)現(xiàn)圖形數(shù)據(jù)可視化實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02OpenCV搞定騰訊滑塊驗(yàn)證碼的實(shí)現(xiàn)代碼
這篇文章主要介紹了OpenCV搞定騰訊滑塊驗(yàn)證碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Python?Pandas中l(wèi)oc和iloc函數(shù)的基本用法示例
無論是loc還是iloc都是pandas中數(shù)據(jù)篩選的函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python?Pandas中l(wèi)oc和iloc函數(shù)的基本用法示例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07快速解釋如何使用pandas的inplace參數(shù)的使用
這篇文章主要介紹了快速解釋如何使用pandas的inplace參數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

使用python編寫android截屏腳本雙擊運(yùn)行即可