自学内容网 自学内容网

Python精选200Tips:166-170


运行系统:macOS Sequoia 15.0
Python编译器:PyCharm 2024.1.4 (Community Edition)
Python版本:3.12

往期链接:

1-5 6-10 11-20 21-30 31-40 41-50
51-60:函数 61-70:类 71-80:编程范式及设计模式
81-90:Python编码规范 91-100:Python自带常用模块-1
101-105:Python自带模块-2 106-110:Python自带模块-3
111-115:Python常用第三方包-频繁使用 116-120:Python常用第三方包-深度学习
121-125:Python常用第三方包-爬取数据 126-130:Python常用第三方包-为了乐趣
131-135:Python常用第三方包-拓展工具1 136-140:Python常用第三方包-拓展工具2

Python项目实战

141-145 146-150 151-155 156-160 161-165
P166–测试记忆力的益智游戏
技术栈:5分钟内通关
import random
import pygame
import sys
from pygame.locals import *

# 常量定义
Frame_Speed = 20
Window_Width = 640
Window_Height = 480
Speed_Reveal = 3
Box_Size = 40
Gap_Size = 10
Border_Width = 10
Border_Height = 7

# 检查棋盘的方块数是否为偶数
assert (Border_Width * Border_Height) % 2 == 0, '棋盘需要有偶数的方块以形成配对。'
X_margin = int((Window_Width - (Border_Width * (Box_Size + Gap_Size))) / 2)
Y_margin = int((Window_Height - (Border_Height * (Box_Size + Gap_Size))) / 2)

# 颜色定义
BackGround_color = (200, 200, 255)  # 浅蓝色背景
Box_Color = (255, 255, 255)          # 白色方块
HighLight_Color = (255, 215, 0)      # 金色高亮
Light_BackGround_color = (93, 46, 100)


# 形状定义
CIRCLE = 'circle'
SQUARE = 'square'
DIAMOND = 'diamond'
LINES = 'lines'
OVAL = 'oval'

# 修改为更丰富的颜色和形状
All_Colors = [
    (255, 0, 0),      # 红色
    (0, 255, 0),      # 绿色
    (0, 0, 255),      # 蓝色
    (255, 255, 0),    # 黄色
    (255, 128, 0),    # 橙色
    (128, 0, 255),    # 紫色
    (0, 255, 255)     # 青色
]
All_Shapes = [CIRCLE, SQUARE, DIAMOND, LINES, OVAL]

# 检查形状和颜色是否足够
assert len(All_Colors) * len(All_Shapes) * 2 >= Border_Width * Border_Height, "棋盘对于定义的形状/颜色来说太大。"

# 主函数
def main():
    global Frame_Speed_Clock, DIS_PlaySurf
    pygame.init()
    Frame_Speed_Clock = pygame.time.Clock()
    DIS_PlaySurf = pygame.display.set_mode((Window_Width, Window_Height))

    X_mouse = 0
    Y_mouse = 0
    pygame.display.set_caption('记忆游戏')

    Board = Randomized_Board()
    Boxes_revealed = GenerateData_RevealedBoxes(False)

    first_Selection = None

    DIS_PlaySurf.fill(BackGround_color)
    Start_Game(Board)

    while True:
        mouse_Clicked = False

        DIS_PlaySurf.fill(BackGround_color)
        Draw_Board(Board, Boxes_revealed)

        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEMOTION:
                X_mouse, Y_mouse = event.pos
            elif event.type == MOUSEBUTTONUP:
                X_mouse, Y_mouse = event.pos
                mouse_Clicked = True

        x_box, y_box = Box_Pixel(X_mouse, Y_mouse)
        if x_box is not None and y_box is not None:
            if not Boxes_revealed[x_box][y_box]:
                Draw_HighlightBox(x_box, y_box)
            if not Boxes_revealed[x_box][y_box] and mouse_Clicked:
                Reveal_Boxes_Animation(Board, [(x_box, y_box)])
                Boxes_revealed[x_box][y_box] = True
                if first_Selection is None:
                    first_Selection = (x_box, y_box)
                else:
                    icon1shape, icon1color = get_Shape_Color(Board, first_Selection[0], first_Selection[1])
                    icon2shape, icon2color = get_Shape_Color(Board, x_box, y_box)

                    if icon1shape != icon2shape or icon1color != icon2color:
                        pygame.time.wait(1000)  # 等待1000毫秒 = 1秒
                        Cover_Boxes_Animation(Board, [(first_Selection[0

原文地址:https://blog.csdn.net/qq_32882309/article/details/142461613

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!