软件实习项目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
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)
一、游戏的逻辑
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