python画一个点_pygame学习笔记(2):画点的三种方法和动画实例
1、單個(gè)像素(畫(huà)點(diǎn))
利用pygame畫(huà)點(diǎn)主要有三種方法:
方法一:畫(huà)長(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) #畫(huà)1*1的矩形,線寬為1,這里不能是0,因?yàn)?*1無(wú)空白區(qū)域。
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
方法二:畫(huà)個(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()
方法三:這種方法并不是畫(huà)上去的,而是改變了surface上某個(gè)點(diǎn)的顏色,這樣看上去像是畫(huà)了一個(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表面、顏色、閉合線或者非閉合線(如果閉合為T(mén)rue,否則為False),點(diǎn)的列表,線寬。pygame.draw.lines(surface,[color],False/True,plotpoints,1)。下面的例子畫(huà)出了一條馬路,具體如下:
import pygame,sys
def lineleft(): #畫(huà)馬路左邊界
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():#畫(huà)馬路右邊界
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():#畫(huà)馬路中間虛線
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í)例中,加入一輛汽車(chē)。首先pygame.image.load()函數(shù)從硬盤(pán)加載一個(gè)圖像,并創(chuàng)建一個(gè)名為my_car的對(duì)象。這里,my_car是一個(gè)surface,不過(guò)是存在內(nèi)存中,并未顯示出來(lái),然后用blit(塊移)方法將my_car復(fù)制到screen表面上,從而顯示出來(lái)。具體代碼如下:
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)畫(huà)
計(jì)算機(jī)動(dòng)畫(huà)實(shí)際上就是把圖像從一個(gè)地方移動(dòng)到另一個(gè)地方,同時(shí)幾個(gè)連接動(dòng)作交待顯示就會(huì)產(chǎn)生逼真的效果。因此,在做動(dòng)畫(huà)中,最基本要考慮的因素主要是三個(gè),一是時(shí)間,什么時(shí)間移動(dòng),多長(zhǎng)時(shí)間變下一個(gè)動(dòng)作,二是位置,從什么位置到什么位置,三是動(dòng)作,前后兩個(gè)動(dòng)作的連續(xù)性。在這個(gè)例子中,因?yàn)檐?chē)是俯視的,所以車(chē)輪轉(zhuǎn)動(dòng)實(shí)際是看不到的,所以不用考慮連續(xù)動(dòng)作的變化,而是只考慮車(chē)的位置和多長(zhǎng)時(shí)間移動(dòng)即可。第一步pygame.time.delay()來(lái)實(shí)現(xiàn)時(shí)間延遲;第二步利用pygame.draw.rect()把原來(lái)位置的圖像覆蓋掉;第三步screen.blit()在新位置引入圖像。下面的例子實(shí)現(xiàn)了汽車(chē)從駛?cè)氲今偝龅倪^(guò)程。
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)
總結(jié)
以上是生活随笔為你收集整理的python画一个点_pygame学习笔记(2):画点的三种方法和动画实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: retrofit content-len
- 下一篇: 江苏python工资一般多少_会计行业一