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


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

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

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

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

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

#@小五義 http://www.cnblogs.com/xiaowuyi
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中引用圖像最簡單的以夷伐夷是image函數。下面在馬路的實例中,加入一輛汽車。首先pygame.image.load()函數從硬盤加載一個圖像,並創建一個名為my_car的對象。這里,my_car是一個surface,不過是存在內存中,並未顯示出來,然后用blit(塊移)方法將my_car復制到screen表面上,從而顯示出來。具體代碼如下:

#@小五義 http://www.cnblogs.com/xiaowuyi
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') #當前文件夾下的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/xiaowuyi
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)
            
                
    
                
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM