发现有一个会Python的男友魅力值杠杠的!!!

Python能做什么?

可以做日常任务,比如自动备份你的MP3,可以做网站,很多著名的网站像知乎、YouTube就是Python写的, 可以做网络游戏的后台,很多在线游戏的后台都是Python开发的。

上面说的这些本人并没有实现过,哈哈哈哈。 但是我知道Python可以做一些有趣的东西,比如仿制抖音表白小软件,用的的开发工具为pycham,pycham也是广泛用于做Python开发的工具。运用的turtle库,当然了如果是安装了anaconda3这个库更好,这里面会有我们做Python程序设计时用到的大部分的库,turtle它是python中一个绘制图像的函数库,可以用它来绘制很多的东西,比如简单的小黄人、玫瑰花、爱心树等,这个库也可以说是一只马良的神笔的吧。

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后,还可以添加更多颜色更漂亮的背景照片,让代码为你绽放更美的烟花!

将将将,到这里结束了,现在就可以动手准备给你们女朋友一个惊喜,展现你的Python男友魅力,我一直就觉得有个会Python的男朋友特别帅气,敲代码时特别有魅力。

如果你对Python感兴趣,想要学习python,这里给大家分享一份Python全套学习资料,都是我自己学习时整理的,希望可以帮到你,一起加油!

😝有需要的小伙伴,可以点击下方链接免费领取或者V扫描下方二维码免费领取🆓
Python全套学习资料

1️⃣零基础入门

① 学习路线

对于从来没有接触过Python的同学,我们帮你准备了详细的学习成长路线图 。可以说是最科学最系统的学习路线 ,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

② 路线对应学习视频

还有很多适合0基础入门的学习视频,有了这些视频,轻轻松松上手Python~

③练习题

每节视频课后,都有对应的练习题哦,可以检验学习成果哈哈!

2️⃣国内外Python书籍、文档

① 文档和书籍资料

3️⃣Python工具包+项目源码合集

①Python工具包

学习Python常用的开发软件都在这里了!每个都有详细的安装教程,保证你可以安装成功哦!

②Python实战案例

光学理论是没用的,要学会跟着一起敲代码,动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。100+实战案例源码等你来拿!

③Python小游戏源码

如果觉得上面的实战案例有点枯燥,可以试试自己用Python编写小游戏,让你的学习过程中增添一点趣味!

4️⃣Python面试题

我们学会了Python之后,有了技能就可以出去找工作啦!下面这些面试题是都来自阿里、腾讯、字节等一线互联网大厂,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

5️⃣Python兼职渠道

而且学会Python以后,还可以在各大兼职平台接单赚钱,各种兼职渠道+兼职注意事项+如何和客户沟通,我都整理成文档了。

上述所有资料 ⚡️ ,朋友们如果有需要的,可以扫描下方👇👇👇二维码免费领取🆓

相关推荐
数据智能老司机2 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机4 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机4 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机4 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i4 小时前
drf初步梳理
python·django
每日AI新事件4 小时前
python的异步函数
python
Raymond运维4 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉4 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
这里有鱼汤5 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook14 小时前
Manim实现脉冲闪烁特效
后端·python·动效