声明:以下代码大家如果有兴趣的话可以用LDLE代码编辑器运行看看。
NO.1:万能计算器(难度系数:1,算术运算符)
a = input("请输入第一个数字:")
b = input("请输入第二个数字:")
a = float(a)
b = float(b)
print("和:",a + b)
print("差:",a - b)
print("积:",a * b)
print("商:",a / b)
print("整除:",a // b)
print("取余:",a % b)
print("幂:",a ** b)
NO.2:扇子(难度系数:1,turtle模块)
import turtle
turtle.forward(200)
turtle.left(90)
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(100,180)
turtle.end_fill()
turtle.left(90)
turtle.forward(100)
for i in range(17):
turtle.left(10)
turtle.pencolor('yellow')
turtle.forward(100)
turtle.backward(100)
turtle.left(100)
turtle.pensize(10)
turtle.pencolor('red')
turtle.forward(100)
turtle.hideturtle()
NO.3:一朵鲜花(难度系数:1,turtle模块)
import turtle
turtle.speed(9)
turtle.left(90)
turtle.color("lawngreen")
turtle.pensize(10)
turtle.forward(180)
turtle.right(180)
turtle.pencolor("pink")
turtle.fillcolor("red")
turtle.begin_fill()
for i in range(10):
turtle.right(160)
turtle.pensize(3)
turtle.circle(50,360)
turtle.end_fill()
turtle.up()
turtle.right(232)
turtle.forward(350)
turtle.down()
turtle.color("red")
NO.4:飞花令(难度系数:2,文件的操作、成员运算符)
注:该代码需要素材,若需要,请点击下方链接。
https://download.csdn.net/download/kingiscome/87793581
import random
file = open("诗词库多.txt","r",encoding = "utf-8")
file_lst = file.readlines()
file.close()
for i in range(len(file_lst)):
file_lst[i] = file_lst[i].strip()
print("规则:请输一句带月的诗词,举例:'*月***,*****。'")
answered_lst = []
rounds = 1
while True:
print("第" + str(rounds) + "回合")
#玩家开始对诗
user = input("请说一句带月的诗词:")
if user in answered_lst:
computer = "这句之前出现过了"
print(computer)
break
elif user not in file_lst:
computer = "诗词库暂未收录,最终解释权归主办方"
print(computer)
break
else:
answered_lst.append(user)
file_lst.remove(user)
idx = random.randint(0,len(file_lst) - 1)
computer = file_lst.pop(idx)
print(computer)
answered_lst.append(computer)
rounds += 1
NO.5:疯狂弹球(难度系数:3,pygame模块、pygame窗口)
注:①注:该代码需要素材,若需要,请点击下方链接。②在运行代码前,请先下载pygame模块。
https://download.csdn.net/download/kingiscome/87793581
import pygame,time # 导入pygame,time模块
# 初始化(检查pygame的功能)
pygame.init()
# 设置窗口
width = 400 # 窗口的宽
height = 500 # 窗口的高
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("弹球游戏")
screen.fill((0, 125, 125))
# 设置小球
ball_x = 100 # 小球x坐标
ball_y = 100 # 小球y坐标
ball = pygame.image.load("小球.png")
screen.blit(ball, (ball_x, ball_y))
# 设置挡板
flap_x = 150 # 挡板x坐标
flap_y = 485 # 挡板y坐标
flap = pygame.image.load("挡板.png")
ball_speed_x = 5 # 小球x轴方向移动速度
ball_speed_y = 5 # 小球y轴方向移动速度
while True:
ball_x += ball_speed_x # 将速度作用在坐标上
ball_y += ball_speed_y
screen.fill((0, 125, 125)) # 背景颜色
screen.blit(ball, (ball_x, ball_y))
screen.blit(flap, (flap_x, flap_y))
# 边界检测
if ball_x <= 0 or ball_x >= width - 30:
ball_speed_x = - ball_speed_x # 将x方向移动距离设置成负
if ball_y <= 0 or ball_y >= height - 30:
ball_speed_y = -ball_speed_y # 将y方向移动距离设置成负
time.sleep(0.01)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.update() # 刷新
你觉得哪个作品最好呢?快在文章下方参与投票吧!