<br />
Python版本:3.6.5 相关模块: pygame模块; 以及一些Python自带的模块。
鼠标无法点击“滚”按钮,只能点击“好呀”按钮。抖音上的程序据说是用C#写的,具体的源码自己没有去找,用上次文章提到的方法将txt改成vbs应该也能实现,但是今天我们利用python实现这一有趣好玩的功能。
按钮实现
# 按钮
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,255)):
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 show_like_interface(text, screen, color=( 255,0,255)):
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()
设置跳转后页面的相关属性,按钮,标题和跳转后的页面的代码非常类似,大家可以对比着来看。
主函数
# 主函数
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption('FROM全网最帅Ahab')
clock = pygame.time.Clock()
pygame.mixer.music.load('./bg_music/1.mp3')
pygame.mixer.music.play(-1, 30.0)
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,192,203)
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
因为代码比较多这里我只上传了一部分代码,这里将一些素材进行引入,设置主要逻辑,原理就是不断捕获鼠标位置,当检测到鼠标出现在我们不希望被点击的按钮上时,就改变该按钮的位置,使得对方无法点击到该按钮。
1
2
3
4
··· END ···