Python能做什么?
可以做日常任务,比如自动备份你的MP3;
可以做网站,很多著名的网站像知乎、YouTube就是Python写的;
可以做网络游戏的后台,很多在线游戏的后台都是Python开发的。
上面说的这些本人并没有实现过,哈哈哈哈。
但是我知道Python可以做一些有趣的东西,比如仿制抖音表白小软件,用的的开发工具为pycham,pycham也是广泛用于做Python开发的工具。运用的turtle库,当然了如果是安装了anaconda3这个库更好,这里面会有我们做Python程序设计时用到的大部分的库,turtle它是python中一个绘制图像的函数库,可以用它来绘制很多的东西,比如简单的小黄人、玫瑰花、爱心树等,这个库也可以说是一只马良的神笔的吧。520就要来了,小编提前分享出来,以下就是能为女朋友准备的小惊喜。
1.告白神器
1、创建一个游戏屏幕
2、加载title
3、加载button,
4、当鼠标移动到 ‘算了吧’ 上面的时候 重加加载桌面并随机生成一个 ‘算了吧’ 坐标;
5、当鼠标移动到 ‘好呀’上面时 显示不同的title
以下就是Python脚本:
import pygame
import random
# 设置游戏屏幕大小 这是一个常量
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption('FROM一个喜欢你很久的小哥哥')
# 标题
def title(text, screen, scale, color=(255, 0, 0)):
font = pygame.font.SysFont('SimHei', WIDTH//(len(text)*2))
textRender = font.render(text, True, color)
# 获取此图片的矩形框
# textRect = textRender.get_rect()
# textRect.midtop = (WIDTH/scale[0], HEIGHT/scale[1])
# screen.blit(textRender, textRect)
# 初始化文字的坐标
screen.blit(textRender, (WIDTH/scale[0], HEIGHT/scale[1]))
# 按钮
def button(text, x, y, w, h, color, screen):
pygame.draw.rect(screen, color, (x, y, w, h))
font = pygame.font.SysFont('SimHei', 20)
textRender = font.render(text, True, (0, 0, 0))
textRect = textRender.get_rect()
textRect.center = ((x+w/2), (y+h/2))
screen.blit(textRender, textRect)
# 生成随机的位置坐标
def get_random_pos():
x, y = random.randint(20, 620), random.randint(20, 460)
return x, y
# 点击喜欢按钮后显示的页面
def show_like_interface(text, screen, color=(255, 0, 0)):
screen.fill((255, 255, 255))
font = pygame.font.SysFont('SimHei', WIDTH//(len(text)))
textRender = font.render(text, True, color)
textRect = textRender.get_rect()
textRect.midtop = (WIDTH/2, HEIGHT/2)
screen.blit(textRender, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
def main():
pygame.init()
clock = pygame.time.Clock()
unlike_pos_x = 330
unlike_pos_y = 250
unlike_pos_width = 80
unlike_pos_height = 40
unlike_color = (0, 191, 255)
like_pos_x = 180
like_pos_y = 250
like_pos_width = 80
like_pos_height = 40
like_color = (0, 191, 255)
running = True
while running:
# 填充窗口
screen.fill((255, 255, 255))
img = pygame.image.load('d:/love2.png')
imgRect = img.get_rect()
imgRect.midtop = int(WIDTH / 1.3), HEIGHT // 7
screen.blit(img, imgRect)
# 获取坐标
pos = pygame.mouse.get_pos()
if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
while True:
unlike_pos_x, unlike_pos_y = get_random_pos()
if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[
0] > unlike_pos_x - 5 and \
pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[
1] > unlike_pos_y - 5:
continue
break
title('小姐姐,我观察你很久了', screen, scale=[5, 8])
title('做我女朋友好不好呀', screen, scale=[5, 4])
button('好呀', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen)
button('算了吧', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if pos[0] < like_pos_x + like_pos_width + 5 and pos[0] > like_pos_x - 5 and pos[1] < like_pos_y + like_pos_height + 5 and pos[1] > like_pos_y - 5:
show_like_interface('我就知道小姐姐你也喜欢我~', screen, color=(255, 0, 0))
pygame.display.flip()
pygame.display.update()
clock.tick(60)
main()``
2、爱情之树
import turtle
import random
def love(x,y):#在(x,y)处画爱心lalala
lv=turtle.Turtle()
lv.hideturtle()
lv.up()
lv.goto(x,y)#定位到(x,y)
def curvemove():#画圆弧
for i in range(20):
lv.right(10)
lv.forward(2)
lv.color('red','pink')
lv.speed(100)
lv.pensize(1)
#开始画爱心lalala
lv.down()
lv.begin_fill()
lv.left(140)
lv.forward(22)
curvemove()
lv.left(120)
curvemove()
lv.forward(22)
lv.write("杨幂",font=("Arial",12,"normal"),align="center")#写上表白的人的名字
lv.left(140)#画完复位
lv.end_fill()
def tree(branchLen,t):
if branchLen > 5:#剩余树枝太少要结束递归
if branchLen<20:
t.color("green")
t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
t.down()
t.forward(branchLen)
love(t.xcor(),t.ycor())#传输现在turtle的坐标
t.up()
t.backward(branchLen)
t.color("brown")
return
t.pensize(random.uniform((branchLen+5)/4-2,(branchLen+6)/4+5))
t.down()
t.forward(branchLen)
# 以下递归
ang=random.uniform(15,45)
t.right(ang)
tree(branchLen-random.uniform(12,16),t)#随机决定减小长度
t.left(2*ang)
tree(branchLen-random.uniform(12,16),t)#随机决定减小长度
t.right(ang)
t.up()
t.backward(branchLen)
myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(1000)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100,t)
myWin.exitonclick()
3.一场烟花表演
100余行Python代码和程序库Tkinter,最后我们就能达到下面这个效果:
import tkinter as tk
from PIL import Image, ImageTk
from time import time, sleep
from random import choice, uniform, randint
from math import sin, cos, radians
# 模拟重力
GRAVITY = 0.05
# 颜色选项(随机或者按顺序)
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']
'''
particles 类
粒子在空中随机生成随机,变成一个圈、下坠、消失
属性:
- id: 粒子的id
- x, y: 粒子的坐标
- vx, vy: 在坐标的变化速度
- total: 总数
- age: 粒子存在的时长
- color: 颜色
- cv: 画布
- lifespan: 最高存在时长
'''
class Particle:
def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx=0., vy=0., size=2., color='red', lifespan=2,
**kwargs):
self.id = idx
self.x = x
self.y = y
self.initial_speed = explosion_speed
self.vx = vx
self.vy = vy
self.total = total
self.age = 0self.color = color
self.cv = cv
self.cid = self.cv.create_oval(
x - size, y - size, x + size,
y + size, fill=self.color)
self.lifespan = lifespan
def update(self, dt):
self.age += dt
# 粒子范围扩大
if self.alive() and self.expand():
move_x = cos(radians(self.id * 360 / self.total)) * self.initial_speed
move_y = sin(radians(self.id * 360 / self.total)) * self.initial_speed
self.cv.move(self.cid, move_x, move_y)
self.vx = move_x / (float(dt) * 1000)
# 以自由落体坠落
elif self.alive():
move_x = cos(radians(self.id * 360 / self.total))
# we technically don't need to update x, y because move will do the job
self.cv.move(self.cid, self.vx + move_x, self.vy + GRAVITY * dt)
self.vy += GRAVITY * dt
# 移除超过最高时长的粒子
elif self.cid is not None:
cv.delete(self.cid)
self.cid = None
# 扩大的时间
def expand (self):
return self.age <= 1.2
# 粒子是否在最高存在时长内
def alive(self):
return self.age <= self.lifespan
'''
循环调用保持不停
'''
def simulate(cv):
t = time()
explode_points = []
wait_time = randint(10, 100)
numb_explode = randint(6, 10)
# 创建一个所有粒子同时扩大的二维列表
for point in range(numb_explode):
objects = []
x_cordi = randint(50, 550)
y_cordi = randint(50, 150)
speed = uniform(0.5, 1.5)
size = uniform(0.5, 3)
color = choice(colors)
explosion_speed = uniform(0.2, 1)
total_particles = randint(10, 50)
for i in range(1, total_particles):
r = Particle(cv, idx=i, total=total_particles, explosion_speed=explosion_speed, x=x_cordi, y=y_cordi,
vx=speed, vy=speed, color=color, size=size, lifespan=uniform(0.6, 1.75))
objects.append(r)
explode_points.append(objects)
total_time = .0
# 1.8s内一直扩大
while total_time < 1.8:
sleep(0.01)
tnew = time()
t, dt = tnew, tnew - t
for point in explode_points:
for item in point:
item.update(dt)
cv.update()
total_time += dt
# 循环调用
root.after(wait_time, simulate, cv)
def close(*ignore):
"""退出程序、关闭窗口"""
global root
root.quit()
if __name__ == '__main__':
root = tk.Tk()
cv = tk.Canvas(root, height=400, width=600)
# 选一个好看的背景会让效果更惊艳!
image = Image.open("./image.jpg")
photo = ImageTk.PhotoImage(image)
cv.create_image(0, 0, image=photo, anchor='nw')
cv.pack()
root.protocol("WM_DELETE_WINDOW", close)
root.after(100, simulate, cv)
root.mainloop()
这只是一个简单版本,等进一步熟悉Tkinter后,还可以添加更多颜色更漂亮的背景照片,让代码为你绽放更美的烟花!
将将将,到这里结束了,不是快520了吗?现在就可以动手准备给你们女朋友一个惊喜,展现你的Python男友魅力,小编自己一直就觉得有个会Python的男朋友特别帅气,嘻嘻嘻,想学习更多Python有关的内容,可以看我主页加群,或关注上面公众号,免费送你学习资料。