一、实践课程目的
1、通过综合实践练习,能够掌握Python基础语法,能够应用Python语言编程解决某一具体领域的应用问题。
2、通过综合实践练习,能够理论联系实际,提升分析问题与解决问题的能力。
二、设备与环境
PC机、Windows操作系统、Python 3.7软件等。
三、项目设计内容
1、课题名称
《强制表白神器》
2、程序功能
通过Python语句实现简单表白;
通过设置按钮实现一个按钮躲避鼠标至随机点,一个按钮可以正常使用,不选择好呀无法关闭程序以达到强制表白的目的;
通过turtle等导包辅助画出完整的爱心树加强表白时的浪漫气氛;
音乐的添加使得被表白者情绪高涨,心防减弱,更容易接受表白。
3、源程序代码
import pygame 
# pygame是一个设计用来开发游戏的Python模块,专为电子游戏设计。包含图像、声音,在SDL库的基础上提供了各种接口,从而使用用户能够使用python语言创建各种各样的游戏或多媒体程序
import random 
import time 
import turtle 
#Turtle库是Python语言中一个很流行的绘制图像的函数库,根据一组函数指令的控制,在这个平面坐标
系中移动,从而在爬行的路径上绘制图形。
import multiprocessing 
import sys 
#该模块提供对解释器使用或维护的一些变量的访问,以及与解释器强烈交互的函数
from pygame.locals import * 
import matplotlib.pyplot as plt#坐标绘图
WIDTH, HEIGHT = 640, 480 
BACKGROUND = (255,201, 174) #背景颜
# 按钮
def button(text, x, y, w, h, color, screen): 
(screen, color, (x, y, w, h)) 
font = pygame.font.Font('./f', 20) #字体样式和大小
textRender = der(text, True, (0, 0, 0)) 
textRect = _rect() 
< = ((x+w/2), (y+h/2)) #按钮位置
screen.blit(textRender, textRect) 
# 标题
def title(text, screen, scale, color=(255, 0, 0)): 
font = pygame.font.Font('./f', WIDTH//(len(text)*2)) 
textRender = der(text, True, color) 
textRect = _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 
# 点击喜欢按钮后显示的页面
1 / 6
def show_like_interface(text, screen, color=(255, 0, 0)): 
screen. ill(BACKGROUND) 
字体属性
font = pygame.font.Font('./f', WIDTH//(len(text))) #字体属性
textRender = der(text, True, color) 
textRect = _rect() 
textRect.midtop = (WIDTH/2, HEIGHT/2-50) 
screen.blit(textRender, textRect) 
pygame.display.update() 
while True: 
for event in (): 
if pe == QUIT: 
pygame.quit() 
def draw_arc(lv): 
for i in range(20): 
lv.right(10) 
lv.forward(2) 
画爱心树上的爱心
#画爱心树上的爱心
def draw_love(x, y): 
love = turtle.Turtle() 
love.hideturtle() 
love.up() 
<(x, y) #爱心的坐标
爱心的坐标
画笔的速度
love.speed(100000000000) #画笔的速度
画笔粗细
love.pensize(2) #画笔粗细
love.down() 
love.begin_ ill() 
画笔自右向左移动
love.left(140) #画笔自右向左移动
love.forward(22) #画笔向上方移动画爱心
画笔向上方移动画爱心
draw_arc(love) 
love.left(120) 
draw_arc(love) 
love.forward(22) 
love.left(140) 
终止
爱心树干
#爱心树干
def draw_tree(branchLen, tur): 
if branchLen > 5: 
if branchLen < 20: 
短树杈判断,如果是为绿
tur.pensize(random.uniform((branchLen+5)/4-2, (branchLen+6)/4+5)) 
tur.down() 
tur.forward(branchLen) 
draw_(), ()) 
tur.up() 
树干为棕
tur.backward(branchLen) #树干为棕
return 
tur.pensize(random.uniform((branchLen+5)/4-2, (branchLen+6)/4+5)) 
tur.down() 
tur.forward(branchLen) 
angle = random.uniform(15, 45) 
tur.right(angle) 
draw_tree(branchLen-random.uniform(12, 16), tur) 
tur.left(2*angle) 
draw_tree(branchLen-random.uniform(12, 16), tur) 
tur.right(angle) 
tur.up() 
tur.backward(branchLen) 
爱心树实现函数
#爱心树实现函数
def draw_main(): 
Win = turtle 表白代码编程可复制
tur = turtle.Turtle() 
tur.hideturtle() 
2 / 6
tur.speed(10000000) 
tur.left(90) 
tur.up() 
tur.backward(200) 
tur.down() 
树主干颜
tur.pensize(32) 
tur.forward(60) 
draw_tree(100, tur) 
主函数
# 主函数
def main(): 
pygame.init() 
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) 
窗口显示
pygame.display.set_caption('FROM一个喜欢你很久的小') #窗口显示
clock = pygame.time.Clock() 
背景音乐存放地址加载背景音乐
pygame.mixer.music.load('./bg_music/1.mp3') #背景音乐存放地址加载背景音乐
秒播放
pygame.mixer.music.play(-1, 30.0) #背景音乐循环播放,从音乐第30秒播放
背景音乐音量
pygame.mixer.music.set_volume(0.25) #背景音乐音量
unlike_pos_x = 330 
unlike_pos_y = 300 
unlike_pos_width = 100 
unlike_pos_height = 50 
like_pos_x = 180 
like_pos_y = 300 
like_pos_width = 100 
like_pos_height = 50 
running = True 
like_color = (255,128, 255) 
运行程序
while running: #运行程序
screen. ill(BACKGROUND) 
img = pygame.image.load("./imgs/1.png") #背景中间的图片
背景中间的图片
imgRect = _rect() 
imgRect.midtop = WIDTH//2, HEIGHT//4 
screen.blit(img, imgRect) 
按钮随机躲避鼠标设置
for event in (): #按钮随机躲避鼠标设置
if pe == pygame.MOUSEBUTTONDOWN: 
mouse_pos = _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 = _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, 128, 255), screen) 
更新整个待显示的 Surface 对象到屏幕上
对象到屏幕上
pygame.display. lip() #更新整个待显示的
更新部分界面显示
pygame.display.update() #更新部分界面显示
clock.tick(60) 
process1 = multiprocessing.Process(target=draw_main) 
process1.start() 
show_like_interface('我就知道小哥哥你也喜欢我~', screen, color=(255, 0,0)) 
if __name__ == '__main__':
main() 
3 / 6
4、程序运行结果
、程序运行结果
运行首界面,图4-1:
图4-1 首界面
点击“算了吧”,“算了吧”随机移动,无法点击,并且无法关闭窗口,如图4-2:
图4-2 
页面:
只能点击“好吧”,出现如下图4-3页面:
图4-3 
紧接着,出现画出“表白树”的过程,如图4-4:
4 / 6
图4-4 
最后形成如图4-5 “表白树”:
“表白树”
图4-5 “表白树”
5 / 6