Этот код выдает ошибку, указывающую важный размер матрицы вращения и важный размер вектора, описывающего вершину. В нем говорится, что матрица вращения имеет размерность (2, 2, 1). Это тестовый код, моя полная программа больше, но содержит еще одну функцию «Астероиды», которая отлично работает, но эта не работает. Может кто-нибудь помочь?
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)
Извините, я забыл сообщение об ошибке
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>