python--pygame实现各级菜单栏目设置

随着学期的开始,同学们也即将进入计算机相关课程的课程设计了,对于python课程设计的小伙伴,可能有些是需要利用pygame来写应该小游戏的,因为最近很多小伙伴同学也在想我要一些基于python的pygame模块做的游戏项目,此外我的博客里也有一些之前写的pygame游戏项目,有一般难度的python恐龙快跑小游戏(代码量共计300行左右)链接:python--谷歌恐龙快跑小项目_恐龙快跑代码_DY.memory的博客-CSDN博客,python城堡保卫战游戏(代码量共计500行左右)链接:python--城堡保卫战_DY.memory的博客-CSDN博客,也要难度较高一点的如飞机大战(代码里共计800行左右)链接:python--飞机大战(课程设计)_python飞机大战_DY.memory的博客-CSDN博客,python射击闯关游戏(代码量共计1100行左右)链接:python大作业高分项目--射击闯关游戏_python射击游戏_DY.memory的博客-CSDN博客等等项目,这些项目具体使用什么python知识和具体功能什么的我在这就不具体介绍了,需要的小伙伴同学们可以翻看我的具体博客!

今天这期博客主要给大家带来的是利用pygame实现各级菜单栏的功能,因为部分同学在做相关设计的时候可能会用到各级菜单栏的功能来使自己程序更加友好高效,使自己的程序更加灵活,同时也是容易加分的地方,所以这期菜单功能的代码已经写好,同学们只需在对应的部分添加对应要实现的功能即可,好了下面进入代码分享!

代码用的知识自然是pygame和一些python基础,共两个类文件,代码共计120行左右!

代码如下:

button_new.py

import pygame

#按钮类
class Button():
	def __init__(self, x, y, image, scale):
		width = image.get_width()
		height = image.get_height()
		self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
		self.rect = self.image.get_rect()
		self.rect.topleft = (x, y)
		self.clicked = False

	def draw(self, surface):
		action = False
		pygame.draw.rect(surface, (0, 0, 0), (self.rect.x, self.rect.y, self.image.get_width(), self.image.get_height()))
		bk = pygame.draw.rect(surface, (255, 255, 255), (self.rect.x, self.rect.y, self.image.get_width(), self.image.get_height()), 3)
		#得到鼠标的位置
		pos = pygame.mouse.get_pos()

		#判断鼠标划过按钮并点击
		if bk.collidepoint(pos):
			if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
				self.clicked = True
				action = True
				pygame.draw.rect(surface, (0, 0, 255),
								 (self.rect.x, self.rect.y, self.image.get_width(), self.image.get_height()))

		if pygame.mouse.get_pressed()[0] == 0:
			self.clicked = False

		#画按钮到屏幕
		surface.blit(self.image, (self.rect.x, self.rect.y))

		return action

main_new.py

import pygame
import button_new

pygame.init()

#创建游戏窗口
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Main Menu")

#定义游戏变量
game_paused = False
menu_state = "main"

#定义字体
font = pygame.font.SysFont("华文楷体", 40)

#定义颜色
TEXT_COL = (255, 255, 255)

#加载图片
resume_img = font.render("重新开始", True, TEXT_COL)
options_img = font.render("选择操作", True, TEXT_COL)
quit_img = font.render("退出程序", True, TEXT_COL)
video_img = font.render("难度设置", True, TEXT_COL)
audio_img = font.render("音乐设置", True, TEXT_COL)
keys_img = font.render("按键设置", True, TEXT_COL)
back_img = font.render("返回上节", True, TEXT_COL)

#创建实例
resume_button = button_new.Button(336, 125, resume_img, 1)
options_button = button_new.Button(336, 250, options_img, 1)
quit_button = button_new.Button(336, 375, quit_img, 1)
video_button = button_new.Button(336, 75, video_img, 1)
audio_button = button_new.Button(336, 200, audio_img, 1)
keys_button = button_new.Button(336, 325, keys_img, 1)
back_button = button_new.Button(336, 450, back_img, 1)

def draw_text(text, font, text_col, x, y):
  img = font.render(text, True, text_col)
  screen.blit(img, (x, y))

#游戏循环
run = True
while run:

  screen.fill((52, 78, 91))

  #判断游戏是否暂停
  if game_paused == True:
    #判断游戏状态
    if menu_state == "main":
      #在屏幕画按钮
      if resume_button.draw(screen):
        game_paused = False
      if options_button.draw(screen):
        menu_state = "options"
      if quit_button.draw(screen):
        run = False
    #判断操作按钮是否开启
    if menu_state == "options":
      #定义一个操作按钮
      if video_button.draw(screen):
        print("你点击了难度设置")
      if audio_button.draw(screen):
        print("你点击了音乐设置")
      if keys_button.draw(screen):
        print("你点击了按键设置")
      if back_button.draw(screen):
        menu_state = "main"
  else:

    draw_text("程序运行中(按空格键暂停)", font, TEXT_COL, 140, 250)

  #事件处理器
  for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_SPACE:
        game_paused = True
    if event.type == pygame.QUIT:
      run = False

  pygame.display.update()

pygame.quit()

运行效果图如下:

 

 

猜你喜欢

转载自blog.csdn.net/Abtxr/article/details/129110587