发现有一个会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以后,还可以在各大兼职平台接单赚钱,各种兼职渠道+兼职注意事项+如何和客户沟通,我都整理成文档了。

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

相关推荐
梦想科研社几秒前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
风等雨归期1 分钟前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
yanglamei19623 分钟前
基于GIKT深度知识追踪模型的习题推荐系统源代码+数据库+使用说明,后端采用flask,前端采用vue
前端·数据库·flask
千穹凌帝4 分钟前
SpinalHDL之结构(二)
开发语言·前端·fpga开发
AlexMercer10126 分钟前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
Adolf_19937 分钟前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask
冯宝宝^7 分钟前
基于mongodb+flask(Python)+vue的实验室器材管理系统
vue.js·python·flask
friklogff7 分钟前
【无标题】云端之C#:全面解析6种云服务提供商的SDK
开发语言·flask·c#
dot.Net安全矩阵15 分钟前
.NET内网实战:通过命令行解密Web.config
前端·学习·安全·web安全·矩阵·.net
叫我:松哥19 分钟前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap