Python精选200Tips:166-170
运行系统:macOS Sequoia 15.0
Python编译器:PyCharm 2024.1.4 (Community Edition)
Python版本:3.12
往期链接:
Python项目实战
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
原文地址:https://blog.csdn.net/qq_32882309/article/details/142461613
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!