利用python如何實(shí)現(xiàn)貓捉老鼠小游戲
python實(shí)現(xiàn)貓捉老鼠小游戲
首界面
開(kāi)始游戲界面
然后鍵盤(pán)操作小老鼠上下左右移動(dòng),貓自己去追,當(dāng)貓追上老鼠則游戲結(jié)束
這里用時(shí)3.2秒,最后將游戲時(shí)長(zhǎng)與貓和老鼠都顯示在主頁(yè)面上
下面我把貓與老鼠的小圖片也放上來(lái)供大家使用
以下是程序源代碼
import turtle import time import random def up(): jerry.setheading(90) jerry.forward(20) def down(): jerry.setheading(270) jerry.forward(20) def left(): jerry.setheading(180) jerry.forward(20) def right(): jerry.setheading(0) jerry.forward(20) playground=turtle.Screen() playground.register_shape('tom.gif') playground.register_shape('jerry.gif') playground.onkey(up,'Up') playground.onkey(down,'Down') playground.onkey(left,'Left') playground.onkey(right,'Right') #監(jiān)聽(tīng) playground.listen() writer=turtle.Turtle() writer.color('brown') writer.hideturtle() writer.penup() writer.home() writer.write("Tom & JERRY",align='center',font=("Comic sans MS",50,"bold")) writer.goto(0,-50) writer.write("READY?3,2,1,GO",align='center',font=("Comic sans MS",20,"bold")) time.sleep(3) writer.clear() tom=turtle.Turtle() tom.shape('tom.gif') tom.penup() tom.goto(random.randint(-200,200),random.randint(-200,200)) tom.pendown() tom.pensize(3) tom.color('blue') jerry=turtle.Turtle() jerry.shape('jerry.gif') jerry.speed(0) jerry.penup() jerry.goto(random.randint(-200,200),random.randint(-200,200)) jerry.color('brown') start=time.time() while True: tom.setheading(tom.towards(jerry)) tom.forward(5) if tom.distance(jerry)<10: end=time.time() playground.clear() jerry.goto(0,0) jerry.write("GAME OVER",align='center',font=("Comic sans MS",50,"bold")) jerry.goto(0,-50) jerry.write("YOU SURVIVED {:.1f} SECONDS".format(end-start),align='center',font=("Comic sans MS",20,"bold")) tom.pu() tom.goto(-50,-70) tom.stamp() jerry.pu() jerry.goto(50,-70) jerry.stamp() break
總結(jié)
到此這篇關(guān)于利用python如何實(shí)現(xiàn)貓捉老鼠小游戲的文章就介紹到這了,更多相關(guān)python貓捉老鼠小游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python實(shí)現(xiàn)滑雪者小游戲
- 教你用Python實(shí)現(xiàn)一個(gè)輪盤(pán)抽獎(jiǎng)小游戲
- python實(shí)現(xiàn)21點(diǎn)小游戲
- 教你用Python寫(xiě)一個(gè)植物大戰(zhàn)僵尸小游戲
- 教你如何用python開(kāi)發(fā)一款數(shù)字推盤(pán)小游戲
- python反編譯教程之2048小游戲?qū)嵗?/a>
- Python實(shí)現(xiàn)我的世界小游戲源代碼
- Python實(shí)現(xiàn)簡(jiǎn)單的2048小游戲
- 基于Python-Pycharm實(shí)現(xiàn)的猴子摘桃小游戲(源代碼)
- Python約瑟夫生者死者小游戲?qū)嵗v解
- 用Python實(shí)現(xiàn)童年貪吃蛇小游戲功能的實(shí)例代碼
- 利用python制作拼圖小游戲的全過(guò)程
- 學(xué)會(huì)用Python實(shí)現(xiàn)滑雪小游戲,再也不用去北海道啦
相關(guān)文章
淺談python已知元素,獲取元素索引(numpy,pandas)
今天小編就為大家分享一篇淺談python已知元素,獲取元素索引(numpy,pandas),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11python?numpy.linalg.norm函數(shù)的使用及說(shuō)明
這篇文章主要介紹了python?numpy.linalg.norm函數(shù)的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Python基于xlutils修改表格內(nèi)容過(guò)程解析
這篇文章主要介紹了Python基于xlutils修改表格內(nèi)容過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07解決Django的request.POST獲取不到內(nèi)容的問(wèn)題
今天小編就為大家分享一篇解決Django的request.POST獲取不到內(nèi)容的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Python實(shí)現(xiàn)列表中非負(fù)數(shù)保留,負(fù)數(shù)轉(zhuǎn)化為指定的數(shù)值方式
這篇文章主要介紹了Python實(shí)現(xiàn)列表中非負(fù)數(shù)保留,負(fù)數(shù)轉(zhuǎn)化為指定的數(shù)值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06