日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python画一个点_pygame学习笔记(2):画点的三种方法和动画实例

發布時間:2024/10/6 python 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python画一个点_pygame学习笔记(2):画点的三种方法和动画实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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)

總結

以上是生活随笔為你收集整理的python画一个点_pygame学习笔记(2):画点的三种方法和动画实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。