软件实习项目2——贪吃喵(猫吃鱼版贪吃蛇)(代码实现)

类变量的定义以及类的初始化__init__

class CatWindow(QWidget):
    row = 0  # 行数
    column = 0  # 列数
    direction = 0  # 猫头方向
    head_row = 0  # 猫头的行
    head_column = 0  # 猫头的列
    head_position = ''  # 猫头位置
    coordinate = []  # 猫头和鱼骨的位置列表
    fish_row = 0  # 鱼的行
    fish_column = 0  # 鱼的列
    fish_position = ''  # 鱼的位置
    score = 0  # 计分

    # 速度默认值为500
    speed = 500

    # 猫咪类型默认值是英短蓝白
    head_pic = 'head.png'
    dead_pic = 'dead.png'

    # 设置几个字体
    font1 = QtGui.QFont("Calibri", 20)
    font2 = QtGui.QFont("Calibri", 15)
    font3 = QtGui.QFont("Calibri", 13)
    font4 = QtGui.QFont("幼圆", 20)
    font5 = QtGui.QFont("幼圆", 13)

    # 所有按钮都按照这个样式
    buttons_style = "QPushButton{background-color: rgb(255, 255, 255); border-radius:10px}" \
                    "QPushButton:hover{Background-color:qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 #cdced1, stop:1 #f6f7fa);}" \
                    "QPushButton:pressed{Background-color:qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 #f6f7fa, stop:1 #cdced1);}"

    # 初始化
    def __init__(self):
        QWidget.__init__(self)
        self.setWindowTitle('贪吃喵')  # 窗口标题
        self.setStyleSheet('background-color:#deebf7')  # 游戏背景颜色
        self.setWindowIcon(QIcon('head.png'))  # 设置窗口图标
        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")  # 任务栏图标
        self.resize(850, 850)  # 游戏窗口大小
        self.row = 12  # 行数
        self.column = 12  # 列数
        pygame.mixer.init()  # 初始化背景音乐
        self.GameStart()
        self.windowButton()
        self.timer = QTimer()  # 计时器
        self.timer.start(self.speed)  # 开始计时
        self.timer.timeout.connect(self.moveCat)  # 超时即调用moveCat方法

一、游戏的逻辑

1、猫头的生成

	# 随机生成猫头位置
    def head(self):
        self.backgroundMuisc()
        self.score = 0  # 分数清零
        self.coordinate.clear()  # 坐标清空
        self.head_row = random.randint(2, self.row - 2)  # 随机选取猫头的行
        self.head_column = random.randint(1, self.column - 2)  # 随机选取猫头的列
        self.coordinate = [str(self.head_row) + "," + str(self.head_column)]  # 把猫头位置写入坐标列表中
        self.fish()  # 调用生成鱼的函数
        self.direction = random.randrange(0, 4)  # 随机选择方向
        self.update()

2、鱼的生成

 	# 生成鱼的位置
    def fish(self):
        while True:
            self.fish_row = random.randint(1, self.row - 1)  # 随机选取鱼的行
            self.fish_column = random.randint(0, self.column - 1)  # 随机选取鱼的列
            self.fish_position = str(self.fish_row) + "," + str(self.fish_column)
            # 若随机生成的鱼的位置不在猫头和鱼骨的坐标列表中,则返回鱼的位置,否则重新生成
            if
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值