实战项目
-
-
-
- P166--测试记忆力的益智游戏
-
- P167--好用的中英文翻译器
-
- P168--图片压缩为字符串
-
- P169--电影推荐系统
-
- P170--Python网页测试题
-
运行系统: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)
Cover_Boxes_Animation(Board, [(first_Selection[0