Ich versuche, ein 2048 Spiel in Pygame mit Python zu machen. Ich möchte, dass die Funktion stackright und stackleft auf der self.data operiert, wenn keine Parameter angegeben sind, und eine stackrighted oder stacklefted Matrix zurückgibt, wenn in den Parametern eine Matrix angegeben ist. Wie würde ich das machen? Ich verwende Python 2.7 hier ist mein Code:Optionale Argumente in Class Functions
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