Matrix - Python

Asked

Viewed 264 times

-2

I’m doing a college paper that requires me to do the Space Invaders game. I was able to make the game matrix and now I need to put the ships inside it, for now it’s like this: inserir a descrição da imagem aqui

I need to print the ships so they stay that way:

V V V V

V V V V

Note: I assign 'V' to the ship.

Could someone give me an idea of how I could do this?

Matrix:

matriz = []
for i in range(LINHA_MAXIMA+1):
    matriz.append([' ']*(COLUNA_MAXIMA+1))

Matrix printing:

for linha in matriz:
    print("|", end="")
    for posicao in linha:
        print(posicao, end="")
    print("|", end="")
    print("")
  • Do you fill the matrix ? Or run these two blocks in sequence ?

  • Fills the matrix

  • How you’re filling in the matrix ?

1 answer

0

Suppose you have a list of tuples that indicate the position of each ship

pos_naves = [(4,3), (5,3), (6,3)]

Where pos_naves[1] = (4,3) gives the position of the ship 1 (x=4, y=3), as you already have the matrix, just put change the characters (values) of these positions by 'V'

for x, y in pos_naves:
  matriz[x][y] = 'V'

Then, just draw the matrix again, it would be good to put the snippet of code that does this in a function, since every time you update the state of the game, you need to update the interface (this design pattern is known as MVC: https://en.wikipedia.org/wiki/Model–view–controller)

For the ships to stay in the formation you want:

0..1.. 2.3 <-Columns

V V V V <--- Line 0

V V V V <--- Line 1

You’d have a list like that

pos_naves = [(0,0),(0,1),(0,2),(0,3),(1,0),(1,1),(1,2),(1,3)]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.