Reputation: 55
I would like to know how to set different sprites as different numbers in a matrix. So for example if I had a matrix of:
[[ 2 -1 1 0 1 1]
[-1 0 2 0 3 1]
[ 0 -1 0 -1 1 2]]
How would I add a certain enemy for the 1's, 2's, 3's, etc. in the certain spot that the number appears.
The numbers in the matrix are randomly generated. Here is my code so far:
from random import *
import numpy as np
rows = 3
cols = 6
matrix = np.matrix(np.random.randint(-1,4, size=(rows, cols)))
What I want to do:
def create_enemies():
for int in matrix:
if int == 1:
enemy = Enemy()
enemy_group.add(enemy)
if int == 2:
enemy2 = Enemy2()
enemy_group.add(enemy2)
Upvotes: 1
Views: 60
Reputation: 569
for i in range(rows):
for j in range(cols):
position = matrix[i][j]
if position == 1:
enemy = Enemy()
matrix[i][j] = enemy
enemy_group.add(enemy)
elif position == 2:
enemy = Enemy2()
matrix[i][j] = enemy
enemy_group.add(enemy)
else:
enemy = Enemy3()
matrix[i][j] = enemy
enemy_group.add(enemy)
A better solution would be to just set the random sprites in the matrix, and avoid setting the numbers altogether. Something like:
import random
class Dog:
def __init__(self):
self.x = 1
class Cat:
def __init__(self):
self.x = 1
class Pig:
def __init__(self):
self.x = 1
rows = 6
cols = 3
choices = [Dog(), Cat(), Pig()]
matrix = [[random.choice(choices) for i in range(rows)] for j in range(cols)]
print(matrix)
Upvotes: 2