日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python之機器人卡牌

系統 1767 0

介紹

這個例子主要利用turtle庫實現根據輸入動態展示不同機器人的圖像和屬性信息。

代碼部分非原創只是做了些許修改和整理使得更易閱讀。

圖片和文件資源請訪問git倉庫獲取:

https://gitee.com/indexman/python-core

涉及以下知識點:

  1. 文件讀取
  2. 字典
  3. turtle庫的使用
  4. 控制語句

?

實現的效果

Python之機器人卡牌_第1張圖片

?

代碼

            
              #!/bin/python3

from turtle import *
from random import choice

screen = Screen()
screen.setup(400, 400)
screen.bgcolor('white')
penup()
hideturtle()
robots = {}

file = open('resource/cards.txt', 'r')

# 將文件中機器人信息裝載到字典中
for line in file.read().splitlines():
    name, battery, intelligence, usefulness, speed, image, colour = line.split(', ')
    robots[name] = [battery, intelligence, usefulness, speed, image, colour]
    screen.register_shape('img/' + image)
file.close()

print('Robots: ', ', '.join(robots.keys()), ' (or random)')

while True:
    robot = input("Choose a robot: ")
    if robot == "random":
        robot = choice(list(robots.keys()))
        print(robot)

    if robot in robots:
        stats = robots[robot]
        style = ('Courier', 14, 'bold')
        clear()
        color(stats[5])
        goto(0, 100)
        shape('img/' + stats[4])
        setheading(90)
        # 將當前位置上的形狀復制到畫布上
        stamp()
        setheading(-90)
        forward(70)
        write('Name: ' + robot, font=style, align='center')
        forward(25)
        write('Battery: ' + stats[0], font=style, align='center')
        forward(25)
        write('Intelligence: ' + stats[1], font=style, align='center')
        forward(25)
        write('Usefulness: ' + stats[2], font=style, align='center')
        forward(25)
        write('Speed: ' + stats[3], font=style, align='center')
    else:
        print("Robot doesn't exist!")

            
          

?

?

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 浦江县| 仁化县| 隆昌县| 奇台县| 义乌市| 闸北区| 丰城市| 咸丰县| 色达县| 育儿| 女性| 宜春市| 盐山县| 铜梁县| 侯马市| 治多县| 徐汇区| 宜宾市| 体育| 荔波县| 山西省| 五指山市| 通州区| 郧西县| 海原县| 莒南县| 日喀则市| 观塘区| 和龙市| 衡南县| 巧家县| 曲靖市| 阳江市| 邵阳市| 新沂市| 静海县| 景泰县| 遂宁市| 福安市| 皮山县| 衡阳县|