python大作业——扫雷游戏

主函数:输入宽高和雷的数量

# main.py
import sys

import pygame as pg
import pygame.font
# import game

# import Button
pg.init()
screen = pg.display.set_mode((640, 480))
COLOR_INACTIVE = pg.Color('lightskyblue3')
COLOR_ACTIVE = pg.Color('dodgerblue2')
FONT = pg.font.Font(None, 32)
font = pygame.font.Font('resources/a.TTF', 30)
set_up = dict()


class Attribute:
    def __init__(self):
        global set_up
        self._BLOCK_WIDTH = set_up[0]
        self._BLOCK_HEIGHT = set_up[1]
        self._MINe_COUNT = set_up[2]

    def get_Width(self):
        return self._BLOCK_WIDTH

    def set_Width(self, w):
        self._BLOCK_WIDTH = w

    BLOCK_WIDTH = property(fget=get_Width, fset=set_Width)  # 修饰类中属性get,set

    def get_Height(self):
        return self._BLOCK_HEIGHT

    def set_Height(self, h):
        self._BLOCK_HEIGHT = h

    BLOCK_HEIGHT = property(fget=get_Height, fset=set_Height)

    def get_MINe_COUNT(self):
        return self._MINe_COUNT

    def set_MINe_COUNT(self, m):
        self._MINe_COUNT = m

    MINe_COUNT = property(fget=get_MINe_COUNT, fset=set_MINe_COUNT)  # 修饰类中属性get,set





class Button:
    def __init__(self, x, y, w, h):
        self.rect = pg.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = "START"
        self.text_surface = FONT.render(self.text, True, self.color)
        self.active = False

    def handle_event(self, event, BOX1, BOX2, BOX3):

        if event.type == pg.MOUSEBUTTONDOWN:
            # 用户选择盒子时
            if self.rect.collidepoint(event.pos):
                # 将盒子的状态改为选中
                self.active = not self.active
            else:
                self.active = False
            # 改变盒子颜色
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pg.MOUSEBUTTONUP:
            if self.active == True:
                set_up[BOX1.id] = int(BOX1.text)
                set_up[BOX2.id] = int(BOX2.text)
                set_up[BOX3.id] = int(BOX3.text)

                return True
                # import game
                # game.start_play()
    def draw(self, screen):
        # Blit the text.
        screen.blit(self.text_surface, (self.rect.x + 10, self.rect.y + 10))
        # Blit the rect.
        pg.draw.rect(screen, self.color, self.rect, 2)


class InputBox:

    def __init__(self, x, y, w, h, id, F_text, text='20'):
        self.rect = pg.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.id = id
        self._F_text = FONT.render(F_text, True, self.color)
        self.txt_surface = FONT.render(text, True, self.color)
        self.active = False

    def handle_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN:
            # 用户选择盒子时
            if self.rect.collidepoint(event.pos):
                # 将盒子的状态改为选中
                self.active = not self.active
            else:
                self.active = False
            # 改变盒子颜色
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pg.KEYDOWN:
            if self.active:
                if event.key == pg.K_RETURN:  # 回车
                    set_up[self.id] = self.text
                elif event.key == pg.K_BACKSPACE:  # 删除
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                # Re-render the text.
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        # 文本框可加长.
        width = max(200, self.txt_surface.get_width() + 10)
        self.re
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值