harm ! Now it is 10 Month. , After the national day, the temperature has also cooled down —— The Chinese New Year is just a few months away , Oh roar ~
This year is almost over , I don't know if Xiaobian has used so many confession codes for everyone ? Did you use it ? If it doesn't work, Xiaobian will Continue to assist !
I don't know, singles , Is there anyone from single dog Evolve into Show of affection dog scatter ~
When I was a senior , A computer friend touched a girl in the Department of mathematics with his own code .
It's like this .( This love is made of CI yuntu !) I don't know why my sister gave up the whole class 40 Many handsome guys ( you 're right , She is the only one in their class
A girl ), And chose the increasingly sparse hair “ June pregnancy ” Of me —— friend .
today , I'll teach you , How to use Python Make two special gifts for your lover .
Yes, of course , If you're still single , You can also use this as Express the artifact , With the person you love Confession .
For those who know programming, see the following ! After giving this gift , I promise you tomorrow night ..... After Wushan cloud and rain , The weather comes late in autumn ! Hehe, hehe ~
Do you like me ? Good coincidence , Me too ~
This paper is based on Tkinter Interface applet , Prepare the corresponding installation environment materials as follows :
Python3、Pycharm、Pygame Not to mention modules and some of their own !
Unified installation module command :pip install -i https://pypi.douban.com/simple/ + Module name
Some pictures prepared by Xiaobian here are as follows :
Some background music 、 Of course, the font should also be prepared :
3.1 First introduce the function of some words in the code
Function:
Button class
Initial Args:
--x, y: Coordinates of the upper left corner of the button
--width, height: Button width and height
--text: The text displayed by the button
--fontpath: The font path
--fontsize: font size
--fontcolor: The font color
--bgcolors: Button background color
--is_want_to_be_selected: Does the button want to be selected by the player
--screensize: Software screen size
'''
3.2 Configure some small settings of the interface window
# Window size (width, height)
SCREENSIZE = (500, 260)
# Define some colors
RED = (255, 0, 0)
BLACK = (0, 0, 0)
AZURE = (240, 255, 255)
WHITE = (255, 255, 255)
MISTYROSE = (255, 228, 225)
PALETURQUOISE = (175, 238, 238)
PAPAYAWHIP = (255, 239, 213)
LIGHTGRAY = (211, 211, 211)
GAINSBORO = (230, 230, 230)
WHITESMOKE = (245, 245, 245)
DARKGRAY = (169, 169, 169)
BLUE = (0, 0, 255)
DEEPSKYBLUE = (0, 191, 255)
SKYBLUE = (135, 206, 235)
LIGHTSKYBLUE = (135, 206, 250)
# Background music path
BGM_PATH = os.path.join(os.getcwd(), 'resources/music/bgm.mp3')
# The font path
FONT_PATH = os.path.join(os.getcwd(), 'resources/font/STXINGKA.TTF')
# Background image path
BG_IMAGE_PATH = os.path.join(os.getcwd(), 'resources/images/01.jpg')
# ICON route
ICON_IMAGE_PATH = os.path.join(os.getcwd(), 'resources/images/icon.png')
3.3 The main function
def main():
# initialization
pygame.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE, 0, 32)
pygame.display.set_icon(pygame.image.load(cfg.ICON_IMAGE_PATH))
pygame.display.set_caption(' You know ?')
# The background music
pygame.mixer.music.load(cfg.BGM_PATH)
pygame.mixer.music.play(-1, 30.0)
# biu The background picture of love
bg_image = pygame.image.load(cfg.BG_IMAGE_PATH)
bg_image = pygame.transform.smoothscale(bg_image, (150, 150))
# Instantiate two buttons
button_yes = Button(x=20, y=cfg.SCREENSIZE[1]-70, width=120, height=35,
text=' willing ', fontpath=cfg.FONT_PATH, fontsize=15, fontcolor=cfg.BLACK, edgecolor=cfg.SKYBLUE,
edgesize=2, bgcolors=[cfg.DARKGRAY, cfg.GAINSBORO], is_want_to_be_selected=True, screensize=cfg.SCREENSIZE)
button_no = Button(x=cfg.SCREENSIZE[0]-140, y=cfg.SCREENSIZE[1]-70, width=120, height=35,
text=' Unwilling ', fontpath=cfg.FONT_PATH, fontsize=15, fontcolor=cfg.BLACK, edgecolor=cfg.DARKGRAY,
edgesize=1, bgcolors=[cfg.DARKGRAY, cfg.GAINSBORO], is_want_to_be_selected=False, screensize=cfg.SCREENSIZE)
# Did you click the OK button
is_agree = False
# Main circulation
clock = pygame.time.Clock()
while True:
# -- Background image
screen.fill(cfg.WHITE)
screen.blit(bg_image, (cfg.SCREENSIZE[0]-bg_image.get_height(), 0))
# -- Mouse event capture
for event in pygame.event.get():
if event.type == pygame.QUIT:
# ---- Do not exit the program until you click the OK button
if is_agree:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button:
if button_yes.rect.collidepoint(pygame.mouse.get_pos()):
button_yes.is_selected = True
root = Tk()
root.withdraw()
messagebox.showinfo('', ' love you forever ')
root.destroy()
is_agree = True
# -- According to the text
showText(screen=screen, text=' Since the moment we met ,', position=(40, 50),
fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=False)
showText(screen=screen, text=' You are my star that never sets day and night !', position=(40, 100),
fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=True)
# -- Display button
button_yes.draw(screen, pygame.mouse.get_pos())
button_no.draw(screen, pygame.mouse.get_pos())
# -- Refresh
pygame.display.update()
clock.tick(60)
After running the code —— The background music has that smell as soon as it comes up ~ Hey Super romantic , The interface is also very nice —— What font do you want 、 The interface can be set, modified and optimized by itself !
4.1 Since the moment we met , You are my star that never sets day and night . —— Lemontov 《 Dark eyes 》
4.2 Will I not love you ? Don't love you ? Can't , Love you as love life .—— Wang Xiaobo
4.3 Lovers are super sweet —— The sweetness burst ! Ha ha ha ha
The corresponding installation environment materials are as follows :
Python3、Pycharm、Pillow Not to mention modules and some of their own !
Unified installation module command :pip install -i https://pypi.douban.com/simple/ + Module name
Some pictures prepared by Xiaobian here are as follows :
3.1 Main code
#-*- coding=utf-8 -*-
from PIL import Image
IMG = '02.jpg' # Set picture file
WIDTH = 150 # Set the width of the character drawing
HEIGHT = 80 # Set the height of the character drawing
OUTPUT = 'ym.txt' # Set the text file for storing character drawings
ascii_char = list(" I like you ") # Set the character set to be displayed
# take 256 Grayscale maps to 70 On characters
def get_char(r,g,b,alpha = 256):
#alpha For transparency
# Judge alpha value , by 0 It means full transparency
if alpha == 0:
return ' '
# Gets the length of the character set , Here for 70
length = len(ascii_char)
# take RGB Value to grayscale value gray, The gray value range is 0-255
gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
# The gray value range is 0-255, The character set has only 70
# The following processing is required to map the gray value to the specified character
# Prevent when the gray value is 255 when , The output of the 70 Characters out of list index , So we need to (255+1)
unit = (255.0 + 1)/length
# Returns the character corresponding to the gray value
return ascii_char[int(gray/unit)]
if __name__ == '__main__':
# Open and adjust the width and height of the picture
im = Image.open(IMG)
im = im.resize((WIDTH,HEIGHT), Image.NEAREST)
# Initialize the output string
txt = ""
# Traverse every line in the picture
for i in range(HEIGHT):
# Traverse each column in the row
for j in range(WIDTH):
# take (j,i) Coordinate RGB Pixels are converted to characters and added to txt character string
txt += get_char(*im.getpixel((j,i)))
# After traversing a line, you need to add a newline character
txt += '\n'
# Output to the screen
print(txt)
with open(OUTPUT,'w') as f:
f.write(txt)
4.1 The picture and text are not well chosen , So the effect is a little social death ! Ha ha ha ~ An awkward smile to ease , Don't break up as a result of confession !
4.2 I don't think I can see that it's a person ! So —— Choosing pictures and words is tricky , I'm just lazy !
“ The rest of my life is very long , I want to be with you forever ”—— So when you see the guys here, remember to pay attention ! Ha ha ha
All right. ! These two confession codes are written here , Is it right? Today, I learned another trick that can flirt with girls and men . Try it yourself ~
Your support is my biggest motivation !! Remember Sanlian ~mua Welcome to read previous articles ~