pygame學習筆記(2)——從畫點到動畫

來源:互聯網
上載者:User

轉載請註明:@小五義 http://www.cnblogs.com/xiaowuyi

1、單個像素(畫點)
利用pygame畫點主要有三種方法:
方法一:畫長寬為1個像素的正方形

#@小五義 http://www.cnblogs.com/xiaowuyiimport pygame,syspygame.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,因為1*1無空白地區。pygame.display.flip()while True:for event in pygame.event.get():if event.type==pygame.QUIT:sys.exit()

 

方法二:畫個直徑為1的圓

#@小五義 http://www.cnblogs.com/xiaowuyiimport pygame,syspygame.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上某個點的顏色,這樣看上去像是畫了一個點screen.set_at()。另外,如果要得到某個像素的顏色,可以使用screen.get_at()。

#@小五義 http://www.cnblogs.com/xiaowuyiimport pygame,syspygame.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、串連多個點形成線

pygame.draw.lines()方法可以將多個點串連成為線。該方法有5個參數:surface表面、顏色、閉合線或者非閉合線(如果閉合為True,否則為False),點的列表,線寬。pygame.draw.lines(surface,[color],False/True,plotpoints,1)。下面的例子畫出了一條馬路,具體如下:

#@小五義 http://www.cnblogs.com/xiaowuyiimport pygame,sysdef lineleft(): #畫馬路左邊界plotpoints=[]for x in range(0,640):y=-5*x+1000plotpoints.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-2000plotpoints.append([x,y])pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)pygame.display.flip() def linemiddle():#畫馬路中間虛線plotpoints=[]x=300for 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中引用映像最簡單的以夷伐夷是image函數。下面在馬路的執行個體中,加入一輛汽車。首先pygame.image.load()函數從硬碟載入一個映像,並建立一個名為my_car的對象。這裡,my_car是一個surface,不過是存在記憶體中,並未顯示出來,然後用blit(塊移)方法將my_car複製到screen表面上,從而顯示出來。具體代碼如下:

#@小五義 http://www.cnblogs.com/xiaowuyiimport pygame,sysdef lineleft():plotpoints=[]for x in range(0,640):y=-5*x+1000plotpoints.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-2000plotpoints.append([x,y])pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)pygame.display.flip() def linemiddle():plotpoints=[]x=300for 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') #當前檔案夾下的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、動畫
     電腦動畫實際上就是把映像從一個地方移動到另一個地方,同時幾個串連動作交待顯示就會產生逼真的效果。因此,在做動畫中,最基本要考慮的因素主要是三個,一是時間,什麼時間移動,多長時間變下一個動作,二是位置,從什麼位置到什麼位置,三是動作,前後兩個動作的連續性。在這個例子中,因為車是俯視的,所以車輪轉動實際是看不到的,所以不用考慮連續動作的變化,而是只考慮車的位置和多長時間移動即可。第一步pygame.time.delay()來實現時間延遲;第二步利用pygame.draw.rect()把原來位置的映像覆蓋掉;第三步screen.blit()在新位置引入映像。下面的例子實現了汽車從駛入到駛出的過程。

#@小五義 http://www.cnblogs.com/xiaowuyiimport pygame,sysdef 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)                                                    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.