当前位置: 首页 > article >正文

Python精选200Tips:166-170

实战项目

        • P166--测试记忆力的益智游戏
          • 技术栈:5分钟内通关
        • P167--好用的中英文翻译器
          • 技术栈:googletrans的使用
        • P168--图片压缩为字符串
          • 技术栈:更高级的加密算法
        • P169--电影推荐系统
          • 技术栈:协同过滤的基本实现
        • P170--Python网页测试题
          • 技术栈:Flask的使用

运行系统: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

http://www.kler.cn/news/319208.html

相关文章:

  • Redis中String命令的基础操作
  • 简单工厂模式
  • SQL常用语法详解
  • SelMatch:最新数据集蒸馏,仅用5%训练数据也是可以的 | ICML‘24
  • 【深度学习】聊一聊正则化
  • C++之 string(中)
  • 1.1 elasticsearch分布式集群基本搭建(centos7.x + elaticsearch7.11.1)
  • 【代码随想录训练营第42期 Day60打卡 - 图论Part10 - Bellman_ford算法系列运用
  • 什么是集成学习?
  • 如何使用ssm实现基于VUE.js的在线教育系统+vue
  • Go调试工具—— Delve
  • 2024 ICPC ShaanXi Provincial Contest —— C. Seats(个人理解)拓扑+dfs
  • 深度学习(4):torch.nn.Module
  • flink 的 Barrier 对齐 的优劣详解:
  • PHP 中 empty() 函数的作用
  • PAT甲级-1083 List Grades
  • 如何选择渲染集群管理软件?
  • css基础知识笔记
  • 【Pyside】pycharm2024配置conda虚拟环境
  • Jmeter 线程组解析
  • 产品经理如何转到AI赛道?优势在哪?待遇如何?
  • C++系列-STL容器中统计算法count, count_if
  • uniapp调用安卓service实现后台运行
  • 华为OD机试真题-最少交换次数-2024年OD统一考试(E卷)
  • fastadmin前端切换成英文,后台中文,修改JS文件
  • Milvus - 从数据库到 Partition Key 实现多租户
  • STM32 使用 CubeMX 实现按键外部中断
  • flink 为啥使用MemorySegment 来管理内存
  • 性能测试1初步使用Jmeter
  • el-table中根据状态改单元格样式