Python能做什么?
可以做日常任務,比如自動備份你的MP3;
可以做網站,很多著名的網站像知乎、YouTube就是Python寫的;
可以做網路游戲的后臺,很多在線游戲的后臺都是Python開發的,
上面說的這些本人并沒有實作過,哈哈哈哈,
但是我知道Python可以做一些有趣的東西,比如仿制抖音表白小軟體,用的的開發工具為pycham,pycham也是廣泛用于做Python開發的工具,運用的turtle庫,當然了如果是安裝了anaconda3這個庫更好,這里面會有我們做Python程式設計時用到的大部分的庫,turtle它是python中一個繪制影像的函式庫,可以用它來繪制很多的東西,比如簡單的小黃人、玫瑰花、愛心樹等,這個庫也可以說是一只馬良的神筆的吧,520就要來了,小編提前分享出來,以下就是能為女朋友準備的小驚喜,

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后,還可以添加更多顏色更漂亮的背景照片,讓代碼為你綻放更美的煙花!
將將將,到這里結束了,不是快520了嗎?現在就可以動手準備給你們女朋友一個驚喜,展現你的Python男友魅力,小編自己一直就覺得有個會Python的男朋友特別帥氣,嘻嘻嘻,想學習更多Python有關的內容,可以看我主頁加群,或關注上面公眾號,免費送你學習資料,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/281649.html
標籤:python
