alles ok soweit?

Code-Stücke können hier veröffentlicht werden.
Antworten
Costi
User
Beiträge: 545
Registriert: Donnerstag 17. August 2006, 14:21

ich schreib grad einen tetris programm, wo eine figur eine klasse ist.
bevor ich aber nun alle figuren nach einen bestimmten muster schrebe und feststellen kann das dieser anderes sein solte frage ich euch lieber was ihr davon hält:

Code: Alles auswählen

class Dre():
	def __init__(self, x, y):
		self.x = x
		self.y = y
		self.figur = None
		self.position = 1

		
	def down(self):
		self.y -=1
	
	def right(self):
		self.x +=1

	def left(self):
		self.x -= 1
	
	def turn(self):
		if self.position == 4:
			self.position = 1
		else:
			self.position = self.position + 1
		
	def get(self):
		#self.figur = self._form()
		self.figurAlt = self.figur

		if self.position == 1:
			self.figur = [
										[self.x,self.y+1],
						[self.x-1,self.y], [self.x,self.y], [self.x+1,self.y], 
						]
		elif self.position == 2:
			self.figur = [
						[self.x,self.y+1],
						[self.x,self.y], [self.x+1,self.y],
						[self.x,self.y-1], 
						]
		
		elif self.position == 3:
			self.figur = [
						[self.x-1,self.y], [self.x,self.y], [self.x+1,self.y], 
										[self.x,self.y-1]
						]
		
		elif self.position == 4:
			self.figur = [
										[self.x,self.y+1],
						[self.x-1,self.y], [self.x,self.y],
										[self.x,self.y-1] 
						]
		
		return {"new": self.figur, "old": self.figurAlt}
		
vieleciht ist das auch noch umd die klassen figur zu verstehen wichtig:

Code: Alles auswählen

def show(fieldObj, figurObj, char):
	z = figurObj.get()
	if z["old"]:
		for i in z["old"]:
			fieldObj.set(i[0], i[1], " ")
	for i in z["new"]:
		fieldObj.set(i[0], i[1], char)
	fieldObj.show()

danke
cp != mv
BlackJack

Das mit dem `old`/`new` Werten finde ich etwas schräg. Ich würde das eher in den Verantwortungsbereich des Quelltextes schieben der die Figur Zeichnet. Also erst löschen, dann bewegen, dann neuzeichnen.

Die Form eines Objekts ist so auch nicht besonders übersichtlich. Da würde ich im Konstruktur alle Formen in relativen Positionen angeben und in einer Liste speichern. `position` wäre dann von 0-3 in diesem Fall, bzw. von 0-len(formenliste) im allgemeinen Fall. So kann man verschiedene Formen mit einer Klasse realisieren. Was sich ändert sind nur die Koordinatenlisten. Vorsicht, nahezu ungetestet:

Code: Alles auswählen

class Shape(object):
    def __init__(self, x, y, shapes):
        self.x = x
        self.y = y
        self.shapes = shapes
        self.rotation = 0
    
    def move_down(self):
        self.y += 1
    
    def move_left(self):
        self.x -= 1
    
    def move_right(self):
        self.x += 1
    
    def turn(self):
        self.rotation = (self.rotation + 1) % len(self.shapes)
    
    def get_block_coordinates(self):
        return [(self.x + x, self.y + y)
                for (x, y) in self.shapes[self.rotation]]


def shape2coordinates(shape):
    result = list()
    for y, row in enumerate(shape):
        for x, character in enumerate(row):
            if not character.isspace():
                result.append((x, y))
    return result


def main():
    l_shape = Shape(0, 0, map(shape2coordinates, (('# ',
                                                   '# ',
                                                   '##'),
                                                  ('###',
                                                   '#  '),
                                                  ('##',
                                                   ' #',
                                                   ' #'),
                                                  ('  #',
                                                   '###'))))
    box_shape = Shape(0, 0, [shape2coordinates(('##', '##'))])
Costi
User
Beiträge: 545
Registriert: Donnerstag 17. August 2006, 14:21

thx, besonders das mit shape2coordinates() ist ne superr idee!

edit:
ich würde es allerdings so screiben:

Code: Alles auswählen


from string import count
st = """
qq   
q    
q    
"""
c = 0
retval = []
for i in st.replace("\n", ""):
	z = count(st, "\n")
	if i != " ":
		y = c/z
		x = c%z
		retval.append([x, y])
	c = c + 1
print retval


cp != mv
Antworten