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函数来显示出抛出数字对应的骰子图像。
更新下新需求
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()
你代码都写了这么一大堆了
简单封装一下,不要都堆在一起,把不同功能拆开,分别放到对应的函数里,不就得了
from PIL import Image
import requests
from io import BytesIO
import random
def throw_dice():
return random.randint(1, 6)
def roll_and_show():
return throw_dice()
def show_roll():
num = roll_and_show()
show_roll = roll_images[num-1]
img = Image.open(BytesIO(show_roll.content))
img.thumbnail([50,50])
img.show()
#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]
random.shuffle(roll_images)
show_roll()