python实现机器人卡牌

yipeiwu_com5年前Python基础

介绍

这个例子主要利用turtle库实现根据输入动态展示不同机器人的图像和属性信息。

代码部分非原创只是做了些许修改和整理使得更易阅读。

图片和文件资源请访问git仓库获取:链接地址

涉及以下知识点:

1.文件读取
2.字典
3.turtle库的使用
4.控制语句 

实现的效果

代码

#!/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!")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python通过nmap扫描在线设备并尝试AAA登录(实例代码)

如果管理网络设备很多,不可能靠人力每天去登录设备去查看是否在线。所以,可以利用python脚本通过每天扫描网络中的在线设备。可以部署在服务器上做成定时任务,每天发送AAA巡检报告。 下面...

Python使用 Beanstalkd 做异步任务处理的方法

Python使用 Beanstalkd 做异步任务处理的方法

使用 Beanstalkd 作为消息队列服务,然后结合 Python 的装饰器语法实现一个简单的异步任务处理工具. 最终效果 定义任务: from xxxxx.job_queue i...

使用pandas模块读取csv文件和excel表格,并用matplotlib画图的方法

使用pandas模块读取csv文件和excel表格,并用matplotlib画图的方法

如下所示: # coding=utf-8 import pandas as pd # 读取csv文件 3列取名为 name,sex,births,后面参数格式为names= name...

python的dataframe转换为多维矩阵的方法

python的dataframe转换为多维矩阵的方法

最近有一个需求要把dataframe转换为多维矩阵,然后可以使用values来实现,下面记录一下代码,方便以后使用。 import pandas as pd import numpy...

python网络编程学习笔记(六):Web客户端访问

6.1 最简单的爬虫 网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成。python的urllib\urllib2等模块很容易实现这一功能,下面的例...