使用python代码表白 |
您所在的位置:网站首页 › python一箭穿心 › 使用python代码表白 |
对于程序员来说,情人节表白当然少不了代码打辅助。小笨聪这次给大家带来两份不同的表白代码(Python版),原理都很基础,第一份主要用到pygame库,第二份主要用到turtle库[附:小笨聪的运行环境是Pycharm配合Anaconda]。 第一份代码 (1)分别设计按钮、标题和点击后的随机坐标 # 按钮 def button(text, x, y, w, h, color, screen): pygame.draw.rect(screen, color, (x, y, w, h)) font = pygame.font.Font('./font/simkai.ttf', 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 title(text, screen, scale, color=(255, 0, 0)): font = pygame.font.Font('./font/simkai.ttf', 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) # 生成随机的位置坐标 def get_random_pos(): x, y = random.randint(20, 620), random.randint(20, 460) return x, y (2)点击“喜欢”按钮后显示的页面 def show_like_interface(text, screen, color=(255, 0, 0)): screen.fill(BACKGROUND) font = pygame.font.Font('./font/simkai.ttf', 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 == QUIT: pygame.quit() sys.exit() (3)循环获取鼠标位置 通过不断捕获鼠标位置,当检测到鼠标出现在我们不希望被点击的按钮上时,就改变该按钮的位置,使得对方无法点击到该按钮。 while running: screen.fill(BACKGROUND) img = pygame.image.load("./imgs/1.png") imgRect = img.get_rect() imgRect.midtop = WIDTH//2, HEIGHT//4 screen.blit(img, imgRect) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if mouse_pos[0] < like_pos_x+like_pos_width+5 and mouse_pos[0] > like_pos_x-5 and\ mouse_pos[1] < like_pos_y+like_pos_height+5 and mouse_pos[1] > like_pos_y-5: like_color = BACKGROUND running = False mouse_pos = pygame.mouse.get_pos() if mouse_pos[0] < unlike_pos_x+unlike_pos_width+5 and mouse_pos[0] > unlike_pos_x-5 and\ mouse_pos[1] < unlike_pos_y+unlike_pos_height+5 and mouse_pos[1] > unlike_pos_y-5: while True: unlike_pos_x, unlike_pos_y = get_random_pos() if mouse_pos[0] < unlike_pos_x+unlike_pos_width+5 and mouse_pos[0] > unlike_pos_x-5 and\ mouse_pos[1] < unlike_pos_y+unlike_pos_height+5 and mouse_pos[1] > unlike_pos_y-5: continue break title('婲丫头,我观察你很久了', screen, scale=[2, 10]) title('做我女朋友好不好呀', screen, scale=[2, 6]) 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, (255, 0, 255), screen) pygame.display.flip() pygame.display.update() clock.tick(60) show_like_interface 运行效果python代码表白小姐姐(1)https://www.zhihu.com/video/1079804725163233280 大家在运行这两份代码时,只需将你想表白的对象名字换一下即可;另外,将代码打包成exe文件运行更方便,打包的方法我已经写在源码里了。 第二份代码 (1)定义“喜欢”按钮的提示 def Love(): love = Toplevel(window) love.geometry("300x100+250+260") love.title("我也喜欢你") label = Label(love, text = "嘻嘻,傻丫头,我也喜欢你!",font = ("微软雅黑",15)) label.pack() btn = Button(love,text = "得了吧",width = 10,height = 2,command=closeallwindow) btn.pack() love.protocol("WM_DELETE_WINDOW", closelove) (2)定义“不喜欢”按钮的提示 def noLove(): no_love = Toplevel(window) no_love.geometry("300x100+520+260") no_love.title("重新选") label = Label(no_love,text="乖乖回去重新选!",font = ("微软雅黑",15)) label.pack() btn = Button(no_love,text="烦你", width=10, height=2,command=no_love.destroy) btn.pack() no_love.protocol("WM_DELETE_WINDOW",closenolove) (3)利用turtle设计窗口并添加插件 window = Tk() # 创建窗口 window.title("喜欢我吗?") # 窗口标题 window.geometry("380x420+500+240") # 窗口大小 btn=Button(text="确定") window.protocol("WM_DELETE_WINDOW",closeWindow ) label = Label(window,text = "hey,婲丫头",font = ("微软雅黑",15),fg = "red")#标签控件 label.grid(row = 0, column = 0 ) label = Label(window,text = "你喜欢我吗?",font = ("微软雅黑",20)) label.grid(row = 1, column = 1,sticky = E) photo = PhotoImage(file = "./123.png") # 插入图片 imageLable = Label(window,image = photo) imageLable.grid(row = 2,columnspan = 2) # 喜欢按钮插件 btn = Button(window,text="喜欢",width=15,height=2,command=Love) btn.grid(row=3,column=0,sticky= W) # 不喜欢按钮插件 btn = Button(window,text="不喜欢" ,command=noLove) btn.grid(row=3,column=1,sticky= E) # 显示窗口 消息循环 window.mainloop() 运行效果python代码表白小姐姐(2)https://www.zhihu.com/video/1079805712426782720 以上就是情人节表白代码的分析与运行效果。 微信公众号“学编程的金融客”后台回复“214表白代码”获取源码用Python代码花式表白小姐姐,她会不会生气啊mp.weixin.qq.com你的点赞就是对我最大的支持!微信公众号二维码 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |