国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

pygame學習筆記(2):畫點的三種方法和動畫實例

2019-11-25 17:44:12
字體:
來源:轉載
供稿:網友

1、單個像素(畫點)

利用pygame畫點主要有三種方法:
方法一:畫長寬為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.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的圓

復制代碼 代碼如下:

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()。
復制代碼 代碼如下:

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)。下面的例子畫出了一條馬路,具體如下:

復制代碼 代碼如下:

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表面上,從而顯示出來。具體代碼如下:

復制代碼 代碼如下:

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()在新位置引入圖像。下面的例子實現了汽車從駛入到駛出的過程。

復制代碼 代碼如下:

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)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大丰市| 观塘区| 安国市| 沂水县| 额尔古纳市| 万安县| 青神县| 烟台市| 西平县| 庆阳市| 阜宁县| 望谟县| 大竹县| 中牟县| 额尔古纳市| 奉化市| 泾阳县| 淮北市| 两当县| 遂川县| 奉新县| 阳谷县| 佛山市| 本溪市| 惠安县| 南漳县| 鹤壁市| 竹溪县| 湖北省| 保定市| 弥勒县| 监利县| 浑源县| 阿尔山市| 克什克腾旗| 平安县| 敦化市| 郯城县| 丹江口市| 岑溪市| 卢氏县|