What can Python do?
It can do daily tasks, such as automatically backing up your MP3, and can make websites. Many famous websites like Zhihu and YouTube are written in Python. It can be the background of online games, and the background of many online games is developed in Python.
None of the above has ever happened, hahaha. But I do know that Python can do some interesting things, such as copying Douyin with Pycham, which is also widely used for Python development. Turtle is a Python library for drawing simple minions, roses, love trees, and so on. This library can also be said to be a magic pen of Ma Liang.
1. Confessions
1, create a game screen 2, load title 3, load button, 4, when the mouse over ‘forget it’ reload desktop and randomly generate a ‘forget it’ coordinates; 5. Display different titles when the mouse cursor moves over ‘ok’. Here is the Python script:
WIDTH, HEIGHT = 640, 480 screen = pygame.display.set_mode((WIDTH, If (0, 0, 0) = 0, 0, 0, 0, 0) if (0, 0, 0) = 0, 0, 0) if (0, 0, 0) = 0, 0, 0) if (0, 0, 0) = 0, 0 font = pygame.font.SysFont('SimHei', WIDTH//(len(text)*2)) textRender = font.render(text, True, # textRect = textrender.get_rect () # textRect.midTop = (WIDTH/scale[0], Screen. Blit (textRender, (WIDTH/scale[0], WIDTH/scale[0], Def button(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, 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: # fill window screen.fill((255, 255, ImgRect = img.get_rect() imgRect.midTOP = int(WIDTH / 1.3), img = pygame.image.load('d:/love2.png') imgRect = img.get_rect() imgRect.midtop = int(WIDTH / 1.3), HEIGHT // 7 screen.blit(img, ImgRect = 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(' Little sister, ', screen, scale=[5, 8]) button(' ok ', like_pos_x, like_pos_y, like_pos_y, Like_pos_x, unlike_pos_y, unlike_pos_width, like_color, screen) button(' forget ', 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 (' I know you like me ', screen, color=(255, 0, 0)) pygame.display.flip() pygame.display.update() clock.tick(60) main()``Copy the code
The tree of love
Import Turtle import random def love(x,y):# In (x,y) lalala lv= turtle.turtle () lv.hideturtle() lv.up() Def curvemove(): Lv. Right (10) lv. Forward (2) LV. Color ('red','pink') LV. Speed (100) LV. lv.left(140) lv.forward(22) curvemove() lv.left(120) curvemove() lv.forward(22) Lv. write(" Arial", font=("Arial",12,"normal"),align="center")# write lv.left(140)# write lv.end_fill() def Tree (branchLen,t): if branchLen > 5: t.color("green") t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5)) t.command () t.fowler (branchLen) love(t.xcor(),t.ycor()) t.backward(branchLen) t.color("brown") return t.pensize(random.uniform((branchLen+5)/4-2,(branchLen+6)/4+5)) t.down() Uniform (15,45). Light (ang) tree(branchlen-random. Uniform (12,16),t)# reduce the length randomly T (2*ang) tree(branchlen-random. uniform(12,16),t)# 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()Copy the code
3. A fireworks show
Over 100 lines of Python code and the Tkinter library, we end up with the following effect:
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 are generated randomly in the air, -id: particle ID -x, Y: particle coordinates -vx, vy: speed of change in coordinates -total: total: age: particle duration -color: color -cv: Canvas - Lifespan 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) # 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 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) # Create a two-dimensional list of all the numb_explode particles in range: Objects = [] x_cordi = randINT (50, 550) y_cordi = randint(50, 150) speed = uniform(0.5, 1.5) size = uniform(0.5, 500) 3) Color = choice(colors) explosion_speed = uniform(0.2, 1) Total_particles = randint(10, 50) for I in range(1, 2) color = choice(colors) explosion_speed = uniform(0.2, 1) Total_Particles = randint(10, 50) for I in range(1, 2) 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, Append (objects) total_time =.0 # 1.8s while total_time < 1.8s: Sleep (0.01) tnew = time() t, dt = tnew, tnew-t for point in explode_points: for item in point: Root. after(wait_time, simulate, CV) def close(*ignore): "" "exit, close the window, "" global" root root. The 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()Copy the code
This is just a simple version, but as you get more familiar with Tkinter, you can add more beautiful background photos and make your code explode even more beautifully for you!
More source code, game source point conveyor belt
Will, will, will, and that’s it. Now you’re ready to surprise your girlfriend with your Python boyfriend charm. I’ve always thought that having a Boyfriend who knows Python is particularly handsome and attractive when typing code.