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

Python实现连连看

# coding:utf-8
import pygame, sys, random, time, easygui
from pygame.locals import *
# 初始化pygame环境
pygame.init()
# 创建窗口
canvas = pygame.display.set_mode((1000, 600))
# 加载图片
bg = pygame.image.load("imgs/bg.jpg")
win = pygame.image.load("imgs/win.png")
# 设置标题
pygame.display.set_caption("连连看")
# 图片位置数组
p = [[60, 120], [170, 120], [280, 120], [390, 120], [500, 120], [610, 120], [720, 120], [830, 120],
     [60, 230], [170, 230], [280, 230], [390, 230], [500, 230], [610, 230], [720, 230], [830, 230],
     [60, 340], [170, 340], [280, 340], [390, 340], [500, 340], [610, 340], [720, 340], [830, 340],
     [60, 450], [170, 450], [280, 450], [390, 450], [500, 450], [610, 450], [720, 450], [830, 450]]

# 定义变量存储点击的卡片信息
first_num = 0
first_x = 0
first_y = 0

# 创建handleEvent方法
def handleEvent():
    # 全局变量
    global first_num, first_x, first_y
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
            sys.exit()
            pygame.quit()
        # 判断点击鼠标左键
        if event.type == MOUSEBUTTONDOWN and event.button == 1:
            # 获取鼠标坐标
            mouse_x = event.pos[0]
            mouse_y = event.pos[1]
            # 调用Click方法获取点击到的卡片信息
            if Click(mouse_x, mouse_y):
                sec_num = Click(mouse_x, mouse_y)[0]
                sec_x = Click(mouse_x, mouse_y)[1]
                sec_y = Click(mouse_x, mouse_y)[2]
                # 创建列表存储点击标号相同卡片的下标
                dellist = []
                # 给第一次点击的卡片信息赋值
                if first_num == 0:
                    first_num = sec_num
                    first_x = sec_x
                    first_y = sec_y
                else:
                    # 第二次点击卡片标号相同判断坐标
                    if sec_num == first_num  :
                        if not(first_x == sec_x and first_y == sec_y) :
                            # 获取标号相同的卡片在cards列表中的下标
                            for i in range(len(cards)):
                                if cards[i].num == sec_num:
                                    dellist.append(i)
                            # 删除卡片
                            for j in range(2):
                                if j == 0:
                                    cards.pop(dellist[j])
                                else:
                                    cards.pop(dellist[j] - 1)
                            first_num = 0
                            first_x = 0
                            first_y = 0
                    # 第二次点击卡片标号不同时给点击卡片信息重新赋值
                    else:
                        first_num = sec_num
                        first_x = sec_x
                        first_y = sec_y
                
# 创建Card类
class Card ():
    def __init__(self, num , x, y):
        self.num = num
        self.img = pygame.image.load('imgs/' + str(num) + '.png')
        self.x = x
        self.y = y
    def paint(self):
        canvas.blit(self.img, (self.x, self.y))
        
# 创建cards列表存储所有卡片
cards = []
for i  in range(0, 32):
    n = int(i % 16 + 1)
    m = int(random.randint(0, (31 - i)))
    x = p[m][0]
    y = p[m][1]
    cards.append(Card(n , x, y))
    p.pop(m)
    
# 添加点击事件获取点击到的卡片信息
def  Click(mouse_x, mouse_y):
    x1 = mouse_x
    y1 = mouse_y
    # 获取所有卡片信息
    for i in range(0, len(cards)):
        imgx = cards[i].x
        imgy = cards[i].y
        # 判断鼠标点击位置是否在卡片上
        if imgx + 100 > x1  and x1 > imgx and imgy + 100 > y1 and y1 > imgy:
            img = cards[i].num
            return img , imgx, imgy
    return False

#获取开始时间
start_time = time.time()
while True:
    # 画出游戏背景
    #canvas.blit(bg, (0, 0))
    #添加游戏结束背景
    if len(cards) == 0:
        canvas.blit(win,(0,0))
    else:
        canvas.blit(bg,(0,0))
    
    # 画出所有图片
    for i in range(0, len(cards)):
        cards[i].paint()
    #获取游戏时间
    if len(cards) != 0:
        end_time = time.time()
    timing = int(end_time - start_time)
    #easygui.msgbox('耗时:' + str(timing))
    Time = pygame.font.SysFont('华文琥珀',40)
    TextTime = Time.render('time:' + str(timing),True,(255,255,255))
    canvas.blit(TextTime,(720,40))
    # 更新屏幕内容
    pygame.display.update()
    # 调用handleEvent方法
    handleEvent()































































































































































































































































































原文地址:https://blog.csdn.net/weixin_38114487/article/details/136778557
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/271637.html

相关文章:

  • linuxOPS基础_linux命令合集
  • 解决:IDEA编译Java程序时报编译失败
  • 【Linux】信号量和线程池
  • 【深度学习】深度估计,Depth Anything Unleashing the Power of Large-Scale Unlabeled Data
  • C#控制台贪吃蛇
  • Redis数据存储的细节
  • upload-labs通关方式
  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的自动驾驶目标检测系统详解(深度学习+Python代码+PySide6界面+训练数据集)
  • 1.python安装
  • Unity Timeline学习笔记(3) - SignalTrack信号轨道和自定义带参数的Marker信号和轨道
  • 基于Keras的模型剪枝(Pruning)
  • cms垃圾回收
  • MySQL-----事务
  • 差分逻辑电平 — LVDS、CML、LVPECL、HCSL互连
  • SQLiteC/C++接口详细介绍之sqlite3类(五)
  • em、px、rem、vh、vw 的区别
  • 掌握C#: 从基础到精通 - 中级实战练习集
  • mac启动skywalking报错
  • 亚马逊云科技 Lambda 运行selenium
  • 基于Springboot+Redis+mysql实现的闲置二手交易网站管理系统