Выполнение задания по программированию Pygame Python

Рейтинг: -1Ответов: 1Опубликовано: 27.01.2023
import pygame as pg
import random

pg.init()
SIZE = W, H = 500, 500
screen = pg.display.set_mode((SIZE))
pg.display.set_caption('Кружки')

FPS = 35

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (225, 0, 0)
GREEN = (0, 225, 0)
BLUE = (0, 0, 225)
YELLOW = (225, 225, 0)

clock = pg.time.Clock()
list_colot = [BLACK, WHITE, RED, GREEN, BLUE, YELLOW]

class Circle:
    def __init__(self, x, y, rad,  color):
        self.x = x
        self.y = y
        self.rad = rad
        self.color = color

    def horizontal_movement(self):
        if self.dir == 'right':
            self.x += 1
            if self.x > W:
                self.dir = 'left'
        else:
            self.x -= 1
            if self.x < 0:
                self.dir = 'right'

s_c = []
for i in range(100):
    s_c.append(Circle(i * 10, i * 5, 30, tuple(random.choices(range(256), k=3))))



while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            quit()
    screen.fill(WHITE)
    clock.tick(FPS)
    pg.display.flip()
    pg.display.update()

Надо сделать передвижение как на картинке введите сюда описание изображения

Тобиш с центра его догоняют остальные и отталкиваются поочереди от левой до правой части и обратно. Вообще не понимаю как это сделать

Ответы

▲ 0

Если я правильно понял, то вот. Но у тебя изначально неверно спавн. Пиши, если что

import pygame
import pygame as pg
import random

pg.init()
SIZE = W, H = 500, 500
screen = pg.display.set_mode((SIZE))
pg.display.set_caption('Кружки')

FPS = 35

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (225, 0, 0)
GREEN = (0, 225, 0)
BLUE = (0, 0, 225)
YELLOW = (225, 225, 0)

clock = pg.time.Clock()
list_colot = [BLACK, WHITE, RED, GREEN, BLUE, YELLOW]

class Circle:
    def __init__(self, x, y, rad,  color):
        self.x = x
        self.y = y
        self.rad = rad
        self.color = color
        self.dir = 'right'

    def horizontal_movement(self):
        if self.dir == 'right':
            self.x += 1
            if self.x > W:
                self.dir = 'left'
        else:
            self.x -= 1
            if self.x < 0:
                self.dir = 'right'

    def draw(self):
        circle = pygame.rect.Rect(self.x-self.rad, self.y-self.rad, self.rad*2, self.rad*2)
        pygame.draw.ellipse(screen, self.color, circle)

s_c = []
for i in range(100):
    s_c.append(Circle(i * 10, i * 5, 30, tuple(random.choices(range(256), k=3))))



while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            quit()
    screen.fill(WHITE)
    for i in s_c:
        i.draw()
        i.horizontal_movement()
    clock.tick(FPS)
    pg.display.flip()
    pg.display.update()