简体   繁体   English

类函数中的可选参数

[英]Optional arguments in Class Functions

I am Trying to make a 2048 game in pygame using python. 我正在尝试使用python在pygame中制作2048游戏。 I want the function stackright and stackleft to operate on the self.data if no parameters are given, and return a stackrighted or stacklefted matrix if a matrix is given in the parameters.how would i go about doing this. 如果没有给出参数,我希望函数stackright和stackleft对self.data进行操作,如果参数中给出了矩阵,则返回一个stackrighted或stacklefted矩阵。我将如何去做。 I am using python 2.7 here is my code: 我正在使用python 2.7,这是我的代码:

import random
def getnull(): # returns null matrix
    data = [
    [0,0,2,2],
    [0,4,0,2],
    [0,2,0,0],
    [2,0,2,0]
    ]
    return data

class Data:
    def fillrandom(self): #updates data by adding 2/4 randomly
        exit = False
        while not exit:
            y = random.randint(0,3)
            x = random.randint(0,3)
            if self.data[y][x] == 0:
                if random.randint(1,10) == 1:
                    self.data[y][x] = 4
                    exit = True
                else:
                    self.data[y][x] = 2
                    exit = True
    def alignright(self):   #
        list1 = [[],[],[],[]]
        for i in range(4):          # per row loop
            for count in range(4):   # per column loop
                if self.data[i][count] != 0:
                    list1[i] += [self.data[i][count]]
            list1[i] = [0]*(4-len(list1[i])) + list1[i]
        self.data = list1
    def alignleft(self):
        list1 = [[],[],[],[]]
        for i in range(4):          # per row loop
            for count in range(4):   # per column loop
                if self.data[i][count] != 0:
                    list1[i] += [self.data[i][count]]
            list1[i] = list1[i] + [0]*(4-len(list1[i]))
        self.data = list1
    def printstate(self):
        print(self.data[0])
        print(self.data[1])
        print(self.data[2])
        print(self.data[3])
        print("-------")
    def checkfilled(self):
        for count in range(4):
            for i in range(4):
                if self.data[count][i] == 0:
                    return False
        return True
    def stackright(self):
        for i in range(4):
            if self.data[i][3] == self.data[i][2]:
                if self.data[i][1] == self.data[i][0]:
                    self.data[i][3] = self.data[i][3] *2
                    self.score += self.data[i][3]
                    self.data[i][2] = self.data[i][1] *2
                    self.score += self.data[i][2]
                    self.data[i][0] , self.data[i][1] = 0,0
                else:
                    self.data[i][3] = self.data[i][3] *2
                    self.score += self.data[i][3]
                    self.data[i][2] = self.data[i][1]
                    self.data[i][1] = self.data[i][0]
                    self.data[i][0] = 0
            elif self.data[i][2] == self.data[i][1]:
                    self.data[i][2] = self.data[i][2] *2
                    self.score += self.data[i][2]
                    self.data[i][1] = self.data[i][0]
                    self.data[i][0] = 0
            elif self.data[i][1] == self.data[i][0]:
                self.data[i][1] = self.data[i][1] *2
                self.score += self.data[i][1]
                self.data[i][0] = 0
    def stackleft(self):
        for i in range(4):
            if self.data[i][0] == self.data[i][1]:
                if self.data[i][2] == self.data[i][3]:
                    self.data[i][0] = self.data[i][0] *2
                    self.score += self.data[i][0]
                    self.data[i][1] = self.data[i][2] *2
                    self.score += self.data[i][1]
                    self.data[i][3] , self.data[i][2] = 0,0
                else:
                    self.data[i][0] = self.data[i][0]*2
                    self.score += self.data[i][0]
                    self.data[i][1] = self.data[i][2]
                    self.data[i][2] = self.data[i][3]
                    self.data[i][3] = 0
            elif self.data[i][1] == self.data[i][2]:
                    self.data[i][1] = self.data[i][1] *2
                    self.score += self.data[i][1]
                    self.data[i][2] = self.data[i][3]
                    self.data[i][3] = 0
            elif self.data[i][2] == self.data[i][3]:
                self.data[i][2] = self.data[i][2] *2
                self.score += self.data[i][2]
                self.data[i][3] = 0
    def alignup(self):
        col = [[],[],[],[]]
        for i in range(4): #per col loop
            for count in range(4): #per row loop
                if self.data[count][i] != 0:
                    col[i] += [self.data[count][i]]
            col[i] = col[i] + [0]*(4-len(col[i]))
        for i in range(4):      # convert column to row
            for count in range(4):
                self.data[count][i] = col[i][count]
    def aligndown(self):
        col = [[],[],[],[]]
        for i in range(4): #per col loop
            for count in range(4): #per row loop
                if self.data[count][i] != 0:
                    col[i] += [self.data[count][i]]
            col[i] = [0]*(4-len(col[i])) + col[i]
        for i in range(4):       # convert column to row
            for count in range(4):
                self.data[count][i] = col[i][count]
    def __init__(self):
        self.data = getnull()
        self.score = 0
data = Data()
data.aligndown()
data.printstate()
print(data.score)



while True:
    pass

You can have default parameters. 您可以具有默认参数。 for example: 例如:

def func(self, matrix=None):
    if matrix is None:
        #do stuff with self.data
    else:
        #do stuff

That way if no parameters are given then the default value is None 这样,如果未提供任何参数,则默认值为无

So then you know that if the value of matrix is None then the caller hasn't specified a value so you do something with self.data . 因此,您知道,如果matrix值为None,则调用者未指定值,因此您可以对self.data but if he does specify a value( else ) then it means that the caller specified a value and you could do something with the matrix. 但是,如果他确实指定了一个值( else ),则意味着调用者指定了一个值,您可以对矩阵进行处理。

Or if you want to use them as the same value you could do the following: 或者,如果要将它们用作相同的值,则可以执行以下操作:

def func(self, matrix=None):
    if matrix is None: matrix = self.data
    #do stuff with the variable 'data'

Now data is whatever you want it to be 现在data就是您想要的

Something like this could do the trick: 这样的事情可以解决问题:

def stackright(self, *args):
    if len(args) == 0:
        #do things with no arguments
    else:
        #do things with arguments
        print(args[0], args[1], ...)

You can also replace *args with **kwargs if you want to be able to call the parameters by their name. 如果您希望能够通过参数名称调用参数,也可以用**kwargs替换*args You can even use both: 您甚至可以同时使用:

def f(self, madatoryArgument1, mandatoryArgument2, *args, **kwargs)

The advantage of this over passing arguments with a None default value, is that it simplifies it when the number of arguments grows. 与传递带有默认值None参数相比,此方法的优势在于,当参数数量增加时,它将简化该参数。

It seems like for what you are asking, your functions would look like this: 对于您的要求,您的函数看起来像这样:

def stackright(self, data=None):
    if data is None: data = self.data
    for i in range(4):
        if data[i][3] == data[i][2]:
            if data[i][1] == data[i][0]:
                data[i][3] = data[i][3] *2
                self.score += data[i][3]
                data[i][2] = data[i][1] *2
                self.score += data[i][2]
                data[i][0] , data[i][1] = 0,0
            else:
                data[i][3] = data[i][3] *2
                self.score += data[i][3]
                data[i][2] = data[i][1]
                data[i][1] = data[i][0]
                data[i][0] = 0
        elif data[i][2] == data[i][1]:
                data[i][2] = data[i][2] *2
                self.score += data[i][2]
                data[i][1] = data[i][0]
                data[i][0] = 0
        elif data[i][1] == data[i][0]:
            data[i][1] = data[i][1] *2
            self.score += data[i][1]
            data[i][0] = 0
    return data
def stackleft(self, data=None):
    if data == None: data = self.data
    for i in range(4):
        if data[i][0] == data[i][1]:
            if data[i][2] == data[i][3]:
                data[i][0] = data[i][0] *2
                self.score += data[i][0]
                data[i][1] = data[i][2] *2
                self.score += data[i][1]
                data[i][3] , data[i][2] = 0,0
            else:
                data[i][0] = data[i][0]*2
                self.score += data[i][0]
                data[i][1] = data[i][2]
                data[i][2] = data[i][3]
                data[i][3] = 0
        elif data[i][1] == data[i][2]:
                data[i][1] = data[i][1] *2
                self.score += data[i][1]
                data[i][2] = data[i][3]
                data[i][3] = 0
        elif data[i][2] == data[i][3]:
            data[i][2] = data[i][2] *2
            self.score += data[i][2]
            data[i][3] = 0
    return data

And then calling data.stackright() will operate on the self data, but calling data.stackright(matrix2) will operate on matrix2 (while adding the score to data, unless you change that part above) 然后调用data.stackright()将对自身数据进行操作,但是调用data.stackright(matrix2)将对matrix2进行操作(将分数添加到数据中,除非您在上面更改了该部分)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM