马上中秋了!给老板写了个猜灯谜小脚本!猜到的越多奖金就越高?赚翻了!

987 阅读3分钟

导语

​​

​****中秋节图片

(中国四大传统节日之一)中秋节自古便有祭月、赏月、吃月饼、玩花灯、赏桂花、饮桂花酒猜灯谜等民俗,流传至今,经久不息。

欢迎阅读往期中秋相关文章:

1.【中秋来袭】卧槽!没想到,用Python竟能做巧克力月饼![附源码]

2.万万没想到 一盒月饼火了!这款秘制Python月饼,拿走不谢!

3.女神约我去赏月!我一看中秋节可能会下雨,立马用Python写了款赏月工具!

图片

哈哈哈哈哈!好啦介绍完我们的中秋佳节啦,下面开始正题。不知道大家今年都会回家吧?记得都吃月饼咯~

图片

中秋除了月饼?我们还能聊些什么?除了正常的团员饭、月饼,当然可以有饭后赏月游玩时间了,

当中秋遇上猜灯猜猜会发生什么么?

图片

图片

​正文

​中秋即将到来,以此一款猜灯谜小游戏送给大家!提前祝大家中秋快乐撒~ps:好像有点儿太早了 hhhhhh

(1)设置游戏开始界面参数等。

class startInterface():
	def __init__(self, screen, **kwargs):
		self.text1 = '猜 灯 谜'
		self.text2 = '开始游戏'
		self.text3 = '退出游戏'
		self.screen = screen
	def start(self):
		button_groups = pygame.sprite.Group()
		button_start = Button(font_path=font_path, font_size=24, text=self.text2, position=(236, 300))
		button_quit = Button(font_path=font_path, font_size=24, text=self.text3, position=(464, 300))
		button_groups.add(button_start)
		button_groups.add(button_quit)
		font = pygame.font.Font(font_path, 48)
		clock = pygame.time.Clock()
		while True:
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					pygame.quit()
					sys.exit()
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if event.button == 1:
						mouse_pos = pygame.mouse.get_pos()
						if button_start.rect.collidepoint(mouse_pos):
							return
						elif button_quit.rect.collidepoint(mouse_pos):
							pygame.quit()
							sys.exit()
			self.screen.fill((0, 0, 0))
			title = font.render(self.text1, True, DeepSkyBlue)
			self.screen.blit(title, (302, 150))
			button_groups.update()
			button_groups.draw(self.screen)
			pygame.display.update()
			clock.tick(60)

如下:

​​​​

(2)设置游戏结束界面。

class endInterface():
	def __init__(self, screen, num_correct, num_total, **kwargs):
		self.text1 = '恭喜你猜完了所有灯谜'
		self.text2 = '答对的灯谜数量: %s' % num_correct
		self.text3 = '灯谜总数量: %s' % num_total
		self.text4 = '正确率: %.2f%%' % (num_correct*100/num_total)
		self.screen = screen
	def start(self):
		button_groups = pygame.sprite.Group()
		button_restart = Button(font_path=font_path, font_size=24, text='重新开始', position=(236, 400))
		button_quit = Button(font_path=font_path, font_size=24, text='退出游戏', position=(464, 400))
		button_groups.add(button_restart)
		button_groups.add(button_quit)
		font_big = pygame.font.Font(font_path, 32)
		font = pygame.font.Font(font_path, 24)
		clock = pygame.time.Clock()
		while True:
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					pygame.quit()
					sys.exit()
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if event.button == 1:
						mouse_pos = pygame.mouse.get_pos()
						if button_restart.rect.collidepoint(mouse_pos):
							startGuess()
						elif button_quit.rect.collidepoint(mouse_pos):
							pygame.quit()
							sys.exit()
			self.screen.fill((0, 0, 0))
			text1_render = font_big.render(self.text1, True, DeepSkyBlue)
			text2_render = font.render(self.text2, True, DeepSkyBlue)
			text3_render = font.render(self.text3, True, DeepSkyBlue)
			text4_render = font.render(self.text4, True, DeepSkyBlue)
			self.screen.blit(text1_render, (250, 100))
			self.screen.blit(text2_render, (250, 200))
			self.screen.blit(text3_render, (250, 240))
			self.screen.blit(text4_render, (250, 280))
			button_groups.update()
			button_groups.draw(self.screen)
			pygame.display.update()
			clock.tick(60)

如下:

​​

​(3)每答对一题用if else循环判断答对就增加。

if button_choice1.rect.collidepoint(mouse_pos):   flag = True   if answer == 0:      text = '恭喜你, 答对了本题'      num_correct += 1   else:      text = '很遗憾, 答错了本题'elif button_choice2.rect.collidepoint(mouse_pos):   flag = True   if answer == 1:      text = '恭喜你, 答对了本题'      num_correct += 1   else:      text = '很遗憾, 答错了本题'elif button_choice3.rect.collidepoint(mouse_pos):   flag = True   if answer == 2:      text = '恭喜你, 答对了本题'      num_correct += 1   else:      text = '很遗憾, 答错了本题'elif button_choice4.rect.collidepoint(mouse_pos):   flag = True   if answer == 3:      text = '恭喜你, 答对了本题'      num_correct += 1   else:      text = '很遗憾, 答错了本题'

(4)设置灯谜数量为25这里随机展示2组。

data = [        {            'quesion': '黑嘴雀,落田渦,食水少,講話多(打一文具)',            'options': ['鋼筆', '毛筆','鉛筆', '墨水'],
            'answer': 1
        },
        {
            'quesion': '發言音太輕,聽眾有意見(打一成語)',
            'options': ['人微言輕', '眾口難調','不盡人意', '低聲下氣'],
            'answer': 3
        },
......省略......

效果图:

​​

灯谜猜猜看活动:

  • 1.十五的月亮(打一成语)

  • 2.月到中秋(数字)

  • 3.中秋之夜开香槟(三字民俗)

  • 4.华夏共赏中秋月(打一旅游用语)

  • 5.海上升明月,天涯共此时(打一山西地名) 

图片

​怎么样,是不是略有难度?动动脑筋猜一猜吧,答案请在评论区留言(记得写明谜题顺序)。图片

总结

最后,木木子恭祝各位朋友,节日快乐!阖家团圆幸福!记得三连点赞的哦~

如果需要猜灯谜完整的代码、 Python新手安装包、免费激活码、等等更多Python资料

源码基地:#私信小编06#即可免费领取源码!

图片