Python+Pygame繪制小球的實例詳解
1.準備工作
1.在文件里找到設(shè)置
2.在項目里找到python解釋器,點擊右邊的加號
3.搜素pygame并安裝
同理下載pgzero安裝包
2.開始制作
1.創(chuàng)建一個小球
代碼
import pgzrun def draw(): screen.fill('green') screen.draw.filled_circle((400,300),30,'red') pgzrun.go()
fill后面的green表示設(shè)置背景的填充顏色為綠色
filled_circle后面的(400,300)表示圓中心位置坐標,30表示圓的半徑,red表示圓的顏色
執(zhí)行結(jié)果
2.創(chuàng)建逐漸變大的小球
代碼
import pgzrun r=1 def draw(): screen.fill('black') screen.draw.filled_circle((400,300),r,'red') def update(): global r r=r+1 pgzrun.go()
執(zhí)行結(jié)果
3.創(chuàng)建一個自由下落的小球
代碼
import pgzrun y=100 def draw(): screen.fill('black') screen.draw.filled_circle((400,y),30,'red') def update(): global y y=y+1 pgzrun.go()
小球的半徑從1開始,每次增加1
執(zhí)行結(jié)果
4.用if語句實現(xiàn)循環(huán)下落
代碼
import pgzrun y=100 def draw(): screen.fill('black') screen.draw.filled_circle((400,y),30,'red') def update(): global y y=y+1 if y>600: y=0 pgzrun.go()
5.做一個循環(huán)上下反彈的小球
代碼:
import pgzrun y=100 t=3 def draw(): screen.fill('black') screen.draw.filled_circle((400,y),30,'red') def update(): global y,t y=y+t if y>=570: t=-t if y<=30: t=-t pgzrun.go()
執(zhí)行結(jié)果
到此這篇關(guān)于Python+Pygame繪制小球的實例詳解的文章就介紹到這了,更多相關(guān)Python Pygame繪制小球內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python生成requirements.txt文件的兩種方法
requirements.txt 文件是項目的依賴包及其對應(yīng)版本號的信息列表,本文主要介紹了python生成requirements.txt文件的兩種方法,具有一定的參考價值,感興趣的可以了解一下2023-12-12python socket發(fā)送TCP數(shù)據(jù)方式
這篇文章主要介紹了python socket發(fā)送TCP數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09python畫圖——實現(xiàn)在圖上標注上具體數(shù)值的方法
今天小編就為大家分享一篇python畫圖——實現(xiàn)在圖上標注上具體數(shù)值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07TensorFlow實現(xiàn)從txt文件讀取數(shù)據(jù)
今天小編就為大家分享一篇TensorFlow實現(xiàn)從txt文件讀取數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python3刪除排序數(shù)組中重復(fù)項的方法分析
這篇文章主要介紹了Python3刪除排序數(shù)組中重復(fù)項的方法,結(jié)合實例形式分析了Python3刪除排序數(shù)組重復(fù)項的原理、相關(guān)遍歷及刪除操作技巧,需要的朋友可以參考下2019-01-01