欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pygame學(xué)習(xí)筆記(2):畫點(diǎn)的三種方法和動(dòng)畫實(shí)例

 更新時(shí)間:2015年04月15日 09:15:09   投稿:junjie  
這篇文章主要介紹了pygame學(xué)習(xí)筆記(2):畫點(diǎn)的三種方法和動(dòng)畫實(shí)例,本文講解了單個(gè)像素(畫點(diǎn))、連接多個(gè)點(diǎn)形成線、引用圖像、動(dòng)畫完整實(shí)例,需要的朋友可以參考下

1、單個(gè)像素(畫點(diǎn))

利用pygame畫點(diǎn)主要有三種方法:
方法一:畫長(zhǎng)寬為1個(gè)像素的正方形

復(fù)制代碼 代碼如下:

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的圓

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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è)氲今偝龅倪^程。

復(fù)制代碼 代碼如下:

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編寫android截屏腳本雙擊運(yùn)行即可

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

    使用python編寫一個(gè)截屏的腳本,雙擊運(yùn)行腳本就OK,截屏成功后會(huì)將截屏文件已當(dāng)前時(shí)間命名,并保存在存放腳本的當(dāng)前路徑的screenshot文件夾下
    2014-07-07
  • Python VTK計(jì)算曲面的高斯曲率和平均曲率

    Python VTK計(jì)算曲面的高斯曲率和平均曲率

    這篇文章主要介紹了Python VTK計(jì)算曲面的高斯曲率和平均曲率,如何使用戶Python版本的VTK計(jì)算曲面的高斯曲率并映射在曲面上。本例中使用了兩個(gè)不同的表面,每個(gè)表面根據(jù)其高斯曲率和平均曲率著色,需要的朋友可以參考一下
    2022-04-04
  • Python變量的定義和運(yùn)算符的使用

    Python變量的定義和運(yùn)算符的使用

    這篇文章主要介紹了Python變量的定義和運(yùn)算符的使用,Python和C/Java不同,在定義變量的時(shí)候不需要顯示的指定變量的類型,在賦值的時(shí)候自動(dòng)就會(huì)確定類型,需要的朋友可以參考下
    2023-05-05
  • 最新評(píng)論