0
点赞
收藏
分享

微信扫一扫

Python小项目



Python小项目学习整理


  • ​​一、Python猜数字小游戏​​

  • ​​1.1 知识储备​​
  • ​​1.2 代码实现​​

  • ​​二、turtle 库​​

  • ​​2.1 画一个六边形​​
  • ​​2.2 画一个 奥运五环​​
  • ​​2.3 新年快乐​​
  • ​​2.4 来片雪花​​
  • ​​2.5 来一个浪漫的表白程序​​
  • ​​2.6 小猪佩奇​​



一、Python猜数字小游戏

很久没有更新过python的内容,现在我就来写一点大家比较感兴趣的东西,我们来完成一个小游戏,我们就从最基础的开始,最简单的猜数字的小游戏,进阶的玩法就是做一个GUI界面,给这个游戏加一个交互式的界面,使这个游戏变得更加人性化

1.1 知识储备


1、输入、格式化输出(强制类型转换函数)
2、判断结构的使用
3、循环的使用(while,for循环的使用)
4、随机数函数的使用 random 中 randint (你需要导入random这个包)


1.2 代码实现

Python3实现

import random
n = random.randint(0,100)#产生0~100的一个随机数
print("请输入0~100之间的任意一个整数:")#给用户一个良好的用户的界面
count = 0#记录我们猜的次数
while True:#猜多少次都可以
a = int(input()) # 输入默认的是字符串,所以需要强制转换
count+=1
if a>n:
print("你猜的数字大了")
elif a==n:
print("bingo,猜对了,你真棒")
break#跳出循环,不然会一直让你不停的输入
elif a<n:
print("你猜的数字小了")

#格式化输出,说明你猜游戏结束了
print("产生的随机数是{},你总共猜了{}次".format(n,count))

Python小项目_小游戏

其实你还可以指定猜游戏的次数,增加游戏难度

二、turtle 库

2.1 画一个六边形

import turtle # 导入 turtle 库
t=turtle.Pen() # 实例化 turtle 对象,这样做的目的是我们以后画图都可以通过 t 来操作
for x in range(180): # 循环 180次,这样小海龟每次走的路就会一次比一次长
t.forward(x) # 前进 x 步
t.left(59) # 像左转动 59°

Python小项目_ide_02

2.2 画一个 奥运五环

import turtle
t = turtle.Pen()
t.width(10) # 设置 turtle 笔的宽度
t.color('blue') # 设置 画笔的颜色

t.circle(50) # 换一个半径为 50 像素的圈圈
t.penup() # 提笔操作
t.goto(120,0) # 将画笔的坐标 移动至 (120,0)
t.pendown() # 落笔操作

t.color('black')
t.circle(50)
t.penup()
t.goto(240,0)
t.pendown()

t.color('red')
t.circle(50)
t.penup()
t.goto(60,-50)
t.pendown()

t.color('yellow')
t.circle(50)
t.penup()
t.goto(180,-50)
t.pendown()

t.color('green')
t.circle(50)

2.3 新年快乐

import turtle
turtle.screensize(600,800)
turtle.pensize(10)
turtle.pencolor("red")
turtle.seth(-45)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(92)
turtle.circle(-6,270)
turtle.fd(92)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(88)
turtle.fd(20)
turtle.seth(135)
turtle.fd(20)
turtle.seth(225)
turtle.fd(20)
turtle.seth(315)
turtle.fd(20)
turtle.seth(45)
turtle.fd(20)
turtle.seth(135)
turtle.begin_fill()
turtle.fillcolor("red")
turtle.fd(50)
turtle.seth(45)
turtle.fd(30)
turtle.seth(-45)
turtle.fd(30)
turtle.seth(225)
turtle.fd(30)
turtle.end_fill()
turtle.seth(90)
turtle.fd(40)
turtle.pensize(20)
turtle.fd(10)
turtle.pensize(5)
turtle.seth(105)
turtle.fd(30)
turtle.circle(-8,240)
turtle.circle(20,20)
turtle.fd(5)
turtle.circle(20,60)
turtle.fd(25)
turtle.penup()
turtle.setx(0)
turtle.sety(0)
turtle.goto(2,-127)
turtle.pendown()
turtle.pensize(5)
turtle.begin_fill()
turtle.fillcolor("red")
turtle.seth(0)
turtle.fd(15)
turtle.seth(-90)
turtle.fd(10)
turtle.seth(180)
turtle.fd(15)
turtle.seth(90)
turtle.fd(10)
turtle.seth(0)
turtle.fd(15)
turtle.end_fill()
turtle.pensize(2)
for x in range(6):
turtle.seth(-90)
turtle.fd(50)
turtle.penup()
turtle.seth(90)
turtle.fd(50)
turtle.seth(180)
turtle.fd(3)
turtle.pendown()

Python小项目_项目实践_03

2.4 来片雪花

import turtle
from turtle import *
def koch(size, n):#n 代表雪花的级数,n越大,就越像雪花
if n == 0:
turtle.fd(size)
else:
for angle in [0, 60, -120, 60]:
turtle.left(angle)
koch(size / 3, n - 1)
def main():
turtle.setup(600,600)
turtle.pen(speed = 7, pencolor = 'blue')
turtle.penup()
turtle.goto(-200,100)
turtle.pendown()
turtle.pensize(1)
level = 3 #这里修改雪花的级数,级数越大,雪花越像
koch(400,level)
turtle.right(120)
koch(400, level)
turtle.right(120)
koch(400, level)
turtle.hideturtle()
done()

if __name__ == '__main__':
main()

Python小项目_小游戏_04

2.5 来一个浪漫的表白程序

import turtle
import time # 导入时间库

# 画爱心的顶部
def LittleHeart():
for i in range(200):
turtle.right(1)
turtle.forward(2)


# 输入表白的语句,默认I Love you
love = input('输入表白的语句,默认I Love you: ')
# 输入署名或者赠谁,没有不执行
me = input('输入署名或者赠谁,没有不执行: ')
if love == '':
love = 'I Love you'
# 窗口大小
turtle.setup(width=900, height=500)
# 颜色
turtle.color('red', 'pink')
# 笔粗细
turtle.pensize(3)
# 速度
turtle.speed(1)
# 提笔
turtle.up()
# 隐藏笔
turtle.hideturtle()
# 去到的坐标,窗口中心为0,0
turtle.goto(0, -180)
turtle.showturtle()
# 画上线
turtle.down()
turtle.speed(1)
turtle.begin_fill()
turtle.left(140)
turtle.forward(224)
# 调用画爱心左边的顶部
LittleHeart()
# 调用画爱右边的顶部
turtle.left(120)
LittleHeart()
# 画下线
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()
# 在心中写字 一次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('#CD5C5C', 'pink')
# 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
time.sleep(2)
# 在心中写字 二次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('red', 'pink')
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
# 写署名
if me != '':
turtle.color('black', 'pink')
time.sleep(2)
turtle.goto(180, -180)
turtle.showturtle()
turtle.write(me, font=(20,), align="center", move=True)

# 点击窗口关闭
window = turtle.Screen()
window.exitonclick()

Python小项目_ide_05

2.6 小猪佩奇

import turtle as t


def init_pen():
'''
初始化画笔的一些属性
'''
t.pensize(4) # 设置画笔的大小
t.colormode(255) # 设置GBK颜色范围为0-255
t.color((255, 155, 192), "pink") # 设置画笔颜色和填充颜色(pink)
t.setup(900, 500) # 设置主窗口的大小为900*500
t.speed(10) # 设置画笔速度为10


def norse():
'''
鼻子
'''
t.pu() # 提笔
t.goto(-100, 100) # 画笔前往坐标(-100,100)
t.pd() # 下笔
t.seth(-30) # 笔的角度为-30°
t.begin_fill() # 外形填充的开始标志
a = 0.4
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill() # 依据轮廓填充
t.pu() # 提笔
t.seth(90) # 笔的角度为90度
t.fd(25) # 向前移动25
t.seth(0) # 转换画笔的角度为0
t.fd(10)
t.pd()
t.pencolor(255, 155, 192) # 设置画笔颜色
t.seth(10)
t.begin_fill()
t.circle(5) # 画一个半径为5的圆
t.color(160, 82, 45) # 设置画笔和填充颜色
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()


def head():
'''

'''
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30) # 顺时针画一个半径为300,圆心角为30°的园
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill()


def ear():
'''
耳朵
'''
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()


def eye():
'''
眼睛
'''
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()


def blusher():
'''

'''
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()


def mouth():
'''

'''
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)


def body():
'''
身体
'''
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (255, 100, 100))
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill()


def hand():
'''

'''
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90)


def foot():
'''

'''
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)


def tail():
'''
尾巴
'''
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)


def main():
init_pen()
norse()
head()
ear()
eye()
blusher()
mouth()
body()
hand()
foot()
tail()

if __name__ == '__main__':
main()
# 能够停住画布,不至于迅速结束
t.mainloop()

Python小项目_小游戏_06



举报

相关推荐

0 条评论