This code gives an error stating the important dimension of the rotation matrix and the important dimension of the vector describing the vertex is different. It states that the rotation matrix has dimension (2, 2, 1). This is test code, my full program is bigger but contains another 'Asteroids' function that works fine but this one doesn't. Can someone help?
import pygame
import pygame.locals
import numpy
import random
asteroids = []
amount = 50
screen = (1600, 1200)
pygame.init()
display = pygame.display.set_mode(screen)
clock = pygame.time.Clock()
def rotation(angle):
rotation = numpy.array([[numpy.cos(angle), -numpy.sin(angle)], [numpy.sin(angle), numpy.cos(angle)]])
return rotation
def Asteroids(angle):
offset2 = numpy.array([[-10.0, 0.0], [-8.0, -5.0], [-5.0, -8.0], [0.0, -10.0], [6.0, -8.0], [9.0, -4.0], [4.0, -2.0], [10.0, 0.0], [7.0, 7.5], [2.5, 4.0], [0.0, 10.0], [-6.0, 8.0], [-9.0, 3.0], [-4.0, 0.0]])
asteroid = []
for n in range(len(offset2)):
offset2[n] = rotation(angle).dot(offset2[n])
asteroid.append(offset2[n])
return asteroid
def asteroid_creator(amount):
for i in range(amount):
i = Boid()
vec = numpy.random.random_sample(2) - numpy.array([0.5, 0.5])
i.position = numpy.random.random_sample(2) * screen
i.velocity = vec / numpy.linalg.norm(vec)
i.acceleration = numpy.array([0.0, 0.0])
i.angle = numpy.random.random_sample(1) * 2 * numpy.pi
asteroids.append(i)
def drawing_asteroid_creator(amount):
for n in range(amount):
#pygame.draw.circle(display, (255 - (n / amount * 255), 255 - (n / amount * 255), 255 - (n / amount * 255)), (int(asteroids[n].position[0]), int(asteroids[n].position[1])), 21)
#pygame.draw.circle(display, (n / amount * 255, n / amount * 255, n / amount * 255), (int(asteroids[n].position[0]), int(asteroids[n].position[1])), 19)
asteroid_angle = asteroids[n].angle
vertices = asteroids[n].position + numpy.array([(Asteroids(asteroid_angle)[0]), (Asteroids(asteroid_angle)[1]), (Asteroids(asteroid_angle)[2]), (Asteroids(asteroid_angle)[3]), (Asteroids(asteroid_angle)[4]), (Asteroids(asteroid_angle)[5]), (Asteroids(asteroid_angle)[6]), (Asteroids(asteroid_angle)[7]), (Asteroids(asteroid_angle)[8]), (Asteroids(asteroid_angle)[9]), (Asteroids(asteroid_angle)[10]), (Asteroids(asteroid_angle)[11]), (Asteroids(asteroid_angle)[12]), (Asteroids(asteroid_angle)[13])])
pygame.draw.polygon(display, (255, 255, 255), (vertices), 2)
asteroids[n].update()
class Boid:
def _init_(self):
self.position = position
self.velocity = velocity
self.acceleration = acceleration
self.angle = angle
def update(self):
self.position += self.velocity
self.velocity += self.acceleration
self.acceleration = 0
asteroid_creator(amount)
while True:
display.fill((0, 0, 0))
drawing_asteroid_creator(amount)
Excuse me, i forgot the error message
Traceback (most recent call last):
File "c:/Users/Marieke/Documents/Python stuff/Boid2.py", line 176, in <module>
drawing_asteroid_creator(amount)
File "c:/Users/Marieke/Documents/Python stuff/Boid2.py", line 135, in drawing_asteroid_creator
vertices = asteroids[n].position + numpy.array([(Asteroids(asteroid_angle)[0]), (Asteroids(asteroid_angle)[1]), (Asteroids(asteroid_angle)[2]), (Asteroids(asteroid_angle)[3]), (Asteroids(asteroid_angle)[4]), (Asteroids(asteroid_angle)[5]), (Asteroids(asteroid_angle)[6]), (Asteroids(asteroid_angle)[7]), (Asteroids(asteroid_angle)[8]), (Asteroids(asteroid_angle)[9]), (Asteroids(asteroid_angle)[10]),
(Asteroids(asteroid_angle)[11]), (Asteroids(asteroid_angle)[12]), (Asteroids(asteroid_angle)[13])])
File "c:/Users/Marieke/Documents/Python stuff/Boid2.py", line 48, in Asteroids
offset2[i] = rotation(angle).dot(offset2[i])
ValueError: shapes (2,2,1) and (2,) not aligned: 1 (dim 2) != 2 (dim 0)
PS C:\Users\Marieke\Documents\Python stuff>
numpy.random.random_sample
always returns an array of random values.
So numpy.random.random_sample(1)
does not return a single number, it returns an array with 1 element. That causes that rotation
creates a 2x2x1 array rather than a 2x2 array.
If you want to generate a single random value, you have to get the first element of the numpy.array
which is generated by numpy.random.random_sample
:
i.angle = numpy.random.random_sample(1)[0] * 2 * numpy.pi
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.