日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

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

作者:五包辣條! ? 更新時間: 2022-05-25 編程語言

直接上效果

游戲素材

1.背景圖

2.籃子

3.蘋果

代碼

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

?1.新建屏幕

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

?2.導入圖片

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

3.屬性設置

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()

總結

原文鏈接:https://blog.csdn.net/AI19970205/article/details/123535875

欄目分類
最近更新