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

Python小游戲?qū)崿F(xiàn)實例之接蘋果

 更新時間:2022年03月23日 10:11:00   作者:五包辣條!  
其實利用Python編寫的小游戲很簡單,下面這篇文章主要給大家介紹了關(guān)于Python小游戲?qū)崿F(xiàn)實例之接蘋果的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

直接上效果

游戲素材

1.背景圖

2.籃子

3.蘋果

代碼

"""
   接蘋果小游戲,本程序?qū)崿F(xiàn)手動控制幀率
   Sprite類是繼承自Turtle的一個類,所以歸于海龜畫圖。
"""

 1.新建屏幕

from sprites import *
 
screen = Screen()                        # 新建屏幕
screen.tracer(0,0)                       # 追蹤命令                  
screen.setup(800,500)

 2.導(dǎo)入圖片

screen.bgpic('greenforest.png')
 
basket = Sprite('basket.png')

3.屬性設(shè)置

counter = 0
fps = 60
start_time = time.perf_counter()

動態(tài)效果

1.產(chǎn)生一個蘋果

while 1:
    if random.randint(1,10)==1:          # 產(chǎn)生一個蘋果
        x = random.randint(-380,380)
        y = 400
        a = Sprite('apple.png',pos=(x,y),tag='apple')        
        a.scale(max(0.5,random.random()))

2.移動邏輯

for apple in screen.turtles():
    if apple.get_tag()!= 'apple':continue      
    apple.move(0,-5)                   # 在水平和垂直方向移動
    if apple.collide(basket):
        apple.remove()                 # 移除蘋果
        counter += 1                   # 接到蘋果了進行統(tǒng)計
        continue
    if apple.ycor() < -250:apple.remove()

3.控制頻率

mx,my = mousepos()                    # 獲取鼠標指針的x,y坐標
basket.goto(mx,-180)    
screen.update()
screen.title('大海老師接蘋果游戲,已接到:' + str(counter) + '個蘋果')
 
# 以下代碼實現(xiàn)手動控制幀率為60
end_time = time.perf_counter()
if end_time - start_time < 1/fps:
    time.sleep(1/fps - (end_time - start_time))
start_time = time.perf_counter()

總結(jié)

到此這篇關(guān)于Python小游戲之接蘋果的文章就介紹到這了,更多相關(guān)Python接蘋果小游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論