Vante901 2022-03-28 09:11 采纳率: 55.6%
浏览 38
已结题

创建一个名为roll_and_show的函数。 函数使用throw_dice函数来投掷骰子,函数应该用show_roll函数来显示作为图像抛出的数字。

import random
for x in range(1,11):    
    throw_dice = random.randint(1,6) 
    print(throw_dice) 

#Import libraries
from PIL import Image
import requests
from io import BytesIO
import random

#Dice side image URLs
roll1_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011091.png"
roll2_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011093.png"
roll3_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011095.png"
roll4_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011097.png"
roll5_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011099.png"
roll6_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011104.png"

#Get images from URLs
roll1 = requests.get(roll1_url)
roll2 = requests.get(roll2_url)
roll3 = requests.get(roll3_url)
roll4 = requests.get(roll4_url)
roll5 = requests.get(roll5_url)
roll6 = requests.get(roll6_url)

num = random.randint(1,6)
roll_images=[roll1,roll2,roll3,roll4,roll5,roll6]

show_roll = roll_images[num-1] 
img = Image.open(BytesIO(show_roll.content)) 
img.thumbnail([50,50]) 
display(img)
我想要达到的结果

创建另一个名为roll_and_show的函数。 函数应该用throw_dice函数来投掷骰子。 之后,函数应该调用show_roll函数来显示出抛出数字对应的骰子图像。

  • 写回答

3条回答 默认 最新

  • 陈年椰子 2022-03-28 09:22
    关注

    更新下新需求

    from PIL import Image
    import requests
    from io import BytesIO
    import random
    
    # Dice side image URLs
    roll1_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011091.png"
    roll2_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011093.png"
    roll3_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011095.png"
    roll4_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011097.png"
    roll5_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011099.png"
    roll6_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011104.png"
    
    # Get images from URLs
    roll1 = requests.get(roll1_url)
    roll2 = requests.get(roll2_url)
    roll3 = requests.get(roll3_url)
    roll4 = requests.get(roll4_url)
    roll5 = requests.get(roll5_url)
    roll6 = requests.get(roll6_url)
    roll_images = [roll1, roll2, roll3, roll4, roll5, roll6]
    
    def throw_dice():
        num = random.randint(1, 6)
        return num
    
    def show_roll(round_list):
        # 显示数字
        print("dice:", ",".join(str(d) for d in round_list))
        # 准备图像
        die_num = round_list[0]
        show_roll = roll_images[die_num - 1]
    
        img = Image.open(BytesIO(show_roll.content))
        img.thumbnail([50, 50])
        width,height  = img.size
        total_height = height
        max_width = (width+10)*6 - 10
        new_img = Image.new('RGB', (max_width, total_height), (200,100,100))
        x = y = 0
        for i in range(6):
            new_img.paste(img, (x, y))
            x += width+10
        # 合并后的图像
        new_img.show()
    
    def roll_and_show():
        played_rounds = 0
        while True:
            round = []
            played_rounds += 1
            for i in range(6):
                round.append(throw_dice())
            if round[0] == round[1] == round[2] == round[3] == round[4]:
                print('win! ',played_rounds , 'times')
                show_roll(round)
                break
    
    
    # 整合
    roll_and_show()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月6日
  • 已采纳回答 3月29日
  • 创建了问题 3月28日

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器