import sys
import traceback
import pygame
from rich import print
from rich.console import Console
# 定义一个“游戏规则”按钮的类
class ImgSpite_game_description(pygame.sprite.Sprite):
def __init__(self,image,position_game_description): # 传入游戏规则按钮的图片,按钮的位置
super().__init__() #初始化
# 加载图片进来并给了图片编号,使在实例化多了之后不会搞混
self.img_game_description = pygame.image.load(image).convert_alpha()
# 获取这个游戏规则按钮的矩形
self.rect_game_description = self.img_game_description.get_rect()
# 将位置传给这个按钮的最左边和最顶部,确定位置
self.rect_game_description.left,self.rect_game_description.top = position_game_description
# 定义一个开始游戏按钮的类
class ImgSpite_start_the_game(pygame.sprite.Sprite):
def __init__(self,image,position_start_the_game): # 传入2048按钮图片,按钮的位置
super().__init__() # 初始化
# 加载20开始游戏按钮图片,给编号
self.img_start_the_game = pygame.image.load(image).convert_alpha()
# 获取这个按钮的矩形
self.rect_start_the_game = self.img_start_the_game.get_rect()
# 将位置传给这个按钮的最左边和最顶部,确定位置
self.rect_start_the_game.left,self.rect_start_the_game.top = position_start_the_game
class Xuanze_button(pygame.sprite.Sprite):
def __init__(self,image,speed,bg_size):
super().__init__()
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.speed = speed
self.width,self.height = bg_size[0],bg_size[1]
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.right < 0:
self.rect.left = self.width
elif self.rect.left > self.width:
self.rect.right = 0
elif self.rect.bottom < 0:
self.rect.top = self.height
elif self.rect.top > self.height:
self.rect.bottom = 0
def main(): # 定义主函数
# 初始化
pygame.init()
# 载入背景图片
background = pygame.image.load("images/background.png")
# 窗口的大小等于背景图片的大小
bg_size = background.get_width(),background.get_height()
# 另一个窗口大小
game1_background = pygame.image.load("images/游戏选择.png")
bg_size1 = background.get_width(),background.get_height()
# 绘制窗口
screen = pygame.display.set_mode((bg_size))
# 设置窗口标题
pygame.display.set_caption("思维大咖")
# 定义变量
transparent = (0,0,0)
#--------------------华丽的变量分割线-----------------
running = True
# --------------------华丽的变量分割线-----------------
imgb = False
# --------------------华丽的变量分割线-----------------
fps = 1000
#创建一个时间对象
fcclock = pygame.time.Clock()
# 两个按钮的位置
position_game_description = 400,600
position_start_the_game = 200,600
# 创建"数学思维","逻辑思维","返回菜单"这三个按钮的列表
buttons = []
group = pygame.sprite.Group()
# 图片
game_description = "images/游戏说明.png"
img_start_the_game = "images/开始游戏.png"
img_math_button = "images/数学思维.png"
img_logic_button = "images/逻辑思维.png"
# 实例化对象
button_game_description = ImgSpite_game_description(game_description,position_game_description)
button_start_the_game = ImgSpite_start_the_game(img_start_the_game,position_start_the_game)
math_button = Xuanze_button(img_math_button,(3,5),bg_size)
logic_button = Xuanze_button(img_logic_button,(3,10),bg_size)
console = Console()
# 添加
buttons.append(math_button)
buttons.append(logic_button)
group.add(math_button)
group.add(logic_button)
while running:
event_list = pygame.event.get() # 获取全部事件(事件清单)
for event in event_list:
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN: # 判断鼠标是否被按下(注:鼠标有三个键哦)
p1, p2, p3 = pygame.mouse.get_pressed()
if p1 == 1: #如果左键被点击
if button_game_description.rect_game_description.collidepoint(event.pos): #如果点到游戏规则那(判断点击区域是否在其区域内)
console.print("游戏规则按钮被点击了","style= bold red!", style="bold red")
if button_start_the_game.rect_start_the_game.collidepoint(event.pos): # 如果点到开始游戏那(判断点击区域是否在其区域内)
console.print("开始游戏按钮被点击了","style= bold red!", style="bold red")
screen = pygame.display.set_mode(bg_size1) # 将窗口改变
imgb = True # 改变变量
if imgb: # 如果这个按钮被点击了,则执行下面的操作
screen.blit(game1_background, (0, 0))
#移动图片
for each in buttons:
each.move()
screen.blit(game1_background, (0, 0))
screen.blit(each.image, each.rect)
fcclock.tick(fps)
# 绘制图片,背景
if imgb == False:
screen.blit(background ,(0,0))
screen.blit(button_game_description.img_game_description,position_game_description)
screen.blit(button_start_the_game.img_start_the_game,position_start_the_game)
# 刷新
pygame.display.flip()
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()