Я делал программу для астероидов, но когда я начал порождать астероиды, все они были собраны вместе и плавали. Я хочу, чтобы каждый из этих астероидов двигался отдельно по своему собственному пути, как будто они должны
Это код для моего класса астероидов
class Asteroid:
def __init__(self):
self.ang_change = randint(1, 5)
self.ang = randint(0, 90) * (pi / 180)
y_values = [1, 599]
self.sx = randint(0, 800)
self.sy = y_values[randint(0, 1)]
# If object spawns from the top, it moves down instead of moving up and de-spawning immediately
if self.sy == y_values[0]:
self.neg = -1
else:
self.neg = 1
self.speed = randint(5, 10)
self.asteroid_angle = randint(0, 80)
def generate(self):
self.ang += self.ang_change
asteroid_img = pygame.image.load("sprites/asteroid.png")
asteroid_copy = pygame.transform.rotate(asteroid_img, self.ang)
window.blit(asteroid_copy,
(asteroid.sx - (asteroid_copy.get_width()) / 2, asteroid.sy - (asteroid_copy.get_height()) / 2))
, и это весь код, если кто-нибудь потребности
import pygame
from math import sin, cos, pi
from random import randint
scr_width = 800
scr_height = 600
window = pygame.display.set_mode((scr_width, scr_height))
pygame.display.set_caption("Asteroids")
clock = pygame.time.Clock()
space_img = pygame.image.load("sprites/space.jpg")
class Ship:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 0
self.vel = 0
self.angle = 0
def draw(self):
ship_img = pygame.image.load("sprites/ship_off.png")
ship_img_copy = pygame.transform.rotate(ship_img, self.angle)
window.blit(ship_img_copy,
(self.x - (ship_img_copy.get_width()) / 2, self.y - (ship_img_copy.get_height()) / 2))
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
ship_img = pygame.image.load("sprites/ship_on.png")
ship_img_copy = pygame.transform.rotate(ship_img, self.angle)
window.blit(ship_img_copy,
(self.x - (ship_img_copy.get_width()) / 2, self.y - (ship_img_copy.get_height()) / 2))
def move(self, vel):
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
vel += 1
self.x += vel * cos(self.angle * (pi / 180) + (90 * pi / 180))
self.y -= vel * sin(self.angle * (pi / 180) + 90 * (pi / 180))
# So that if it leaves one side it comes from the other
if self.y < 0:
self.y = (self.y - vel) % 600
elif self.y > 600:
self.y = (self.y + vel) % 600
elif self.x < 0:
self.x = (self.x - vel) % 800
elif self.x > 800:
self.x = (self.x + vel) % 800
if keys[pygame.K_a]:
self.angle += 7
if keys[pygame.K_d]:
self.angle -= 7
class Asteroid:
def __init__(self):
self.ang_change = randint(1, 5)
self.ang = randint(0, 90) * (pi / 180)
y_values = [1, 599]
self.sx = randint(0, 800)
self.sy = y_values[randint(0, 1)]
# If object spawns from the top, it moves down instead of moving up and de-spawning immediately
if self.sy == y_values[0]:
self.neg = -1
else:
self.neg = 1
self.speed = randint(5, 10)
self.asteroid_angle = randint(0, 80)
def generate(self):
self.ang += self.ang_change
asteroid_img = pygame.image.load("sprites/asteroid.png")
asteroid_copy = pygame.transform.rotate(asteroid_img, self.ang)
window.blit(asteroid_copy,
(asteroid.sx - (asteroid_copy.get_width()) / 2, asteroid.sy - (asteroid_copy.get_height()) / 2))
class Projectiles:
def __init__(self, x, y, angle):
self.x = x
self.y = y
self.angle = angle
self.vel = 20
def draw(self):
pygame.draw.rect(window, (255, 0, 0), (self.x, self.y, 5, 5))
def redraw():
window.blit(space_img, (0, 0))
ship.draw()
for asteroid in asteroids:
asteroid.generate()
for bullet in bullets:
bullet.draw()
pygame.display.update()
run = True
ship = Ship(375, 225)
bullets = []
asteroids = []
while run:
keys = pygame.key.get_pressed()
pygame.time.delay(35)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if keys[pygame.K_SPACE]:
if len(bullets) < 11:
bullets.append(
Projectiles(round(ship.x + ship.width - 6.5 // 2), round(ship.y + ship.width - 6.5 // 2), ship.angle))
for bullet in bullets:
if 800 > bullet.x > 0 and 600 > bullet.y > 0:
bullet.x += bullet.vel * cos(bullet.angle * (pi / 180) + 90 * (pi / 180))
bullet.y -= bullet.vel * sin(bullet.angle * (pi / 180) + 90 * (pi / 180))
else:
bullets.pop(bullets.index(bullet))
if len(asteroids) < 5:
asteroids.append(Asteroid())
for asteroid in asteroids:
if 800 > asteroid.sx > 0 and 600 > asteroid.sy > 0:
asteroid.sx += asteroid.speed * cos(asteroid.asteroid_angle * (pi / 180) + 90 * (pi / 180))
asteroid.sy -= asteroid.speed * sin(asteroid.asteroid_angle * (pi / 180) + 90 * (pi / 180)) * asteroid.neg
if asteroid.sx < 0:
asteroid.sx = (asteroid.sx - asteroid.speed) % 800
elif asteroid.sx > 800:
asteroid.sx = (asteroid.sx + asteroid.speed) % 800
else:
asteroids.pop(asteroids.index(asteroid))
window.fill((0, 0, 0))
ship.move(0)
redraw()
clock.tick(60)
pygame.quit()