Python PyGame 用不同的移动多次在屏幕上附加对象

Python PyGame append object on the screen multiple times with different movement

我正在尝试制作一个运动非常随机的对象,这很好,很有效。但是如果我想让同一个对象附加到屏幕 10 次,那么它就不起作用或者我不知道该怎么做。该对象称为蚂蚁。我很抱歉,如果它是一个糟糕的、不可读的代码,我仍然是 pygame 的初学者。所以蚂蚁对象(它是一个红色的小方块)需要在屏幕上出现 10 次,每只蚂蚁需要随机移动。感谢您的所有回答。

import pygame as pg
import random as rd
import math
import sys

from pygame.time import Clock

pg.init()

screen = pg.display.set_mode((500,500))
title = pg.display.set_caption("TEST")

#REDRAWING SCREEN
def redraw():
    screen.fill((0,0,0))
    pg.display.update()

#FOOD
def food():
    food_x = 200
    food_y = 200
    FOOD_COLOR = (255,255,255,0)
    FWIDTH = 15
    fooddraw = pg.draw.circle(screen, FOOD_COLOR, (food_x, food_y), FWIDTH)

#ANT VARIABLES
ant_x = 200
ant_y = 200
COLOR = (220,20,60,0)
ANT_WIDTH = 5

#RANDOM MOVES USING VECTOR
def move():
    global ant_x,ant_y
    x_boundary = 1000
    y_boundary = 1000
    pos = pg.math.Vector2(ant_x, ant_y)
    ant_x = rd.randrange(0, x_boundary)
    ant_y = rd.randrange(0, y_boundary)
    speed = 1
    maxdist = 1
    dist = rd.randrange(maxdist)
    direction = pg.math.Vector2(1, 2).rotate(rd.randrange(360))

    pos += direction * speed
    dist -= speed
    ant_x, ant_y = round(pos.x), round(pos.y)

    if dist <= 0:
        dist = rd.randrange(maxdist)
        direction = pg.math.Vector2(1, 0).rotate(rd.randrange(360))
    else:
        pass
    if ant_x >= screen.get_width(): #IF ANTS X IS OFF THE SCREEN WIDTH THEN RESET THE X POSITION
        ant_x = 200
        dist = rd.randrange(maxdist)
        direction = pg.math.Vector2(1, 0).rotate(rd.randrange(360))
    else:
        pass
    if ant_y >= screen.get_height(): #RESET POSITION IF ANTS Y IS OFF THE SCREEN HEIGHT
        ant_x = 200
        dist = rd.randrange(maxdist)
        direction = pg.math.Vector2(1, 0).rotate(rd.randrange(360))
    else:
        pass

#TRIED TO DUPLICATE ANT 10 TIMES WITH DIFFERENT MOVES BUT DOESNT WORK, I STILL SEE ONLY 1 ANT ON THE SCREEN

ant_spawn = []

for i in range(10):
    for j in range(2):
        ant_x = 250
        ant_y = 250
        ant_spawn.append(j)

    
while True:
    FPS = pg.time.Clock()
    redraw()
    for event in pg.event.get(): 
        if event.type == pg.QUIT:
            pg.quit()
    #APPEND TO SCREEN
    for ants in ant_spawn:
        antdraw = pg.draw.rect(screen, COLOR, (ant_x, ant_y,3,3))

    #MOVE FUNCTION ACTIVATE
    move()
    pg.display.update()
    FPS.tick(60)

您必须创建坐标列表而不是索引列表:

ant_spawn = []
for i in range(10):
    ant_x = 250
    ant_y = 250
    ant_spawn.append([ant_x, ant_y])

循环移动并绘制蚂蚁:

for ants in ant_spawn:
    move(ants)
for ants in ant_spawn:
    pg.draw.rect(screen, COLOR, (*ants, 3, 3))

移动函数改变了一只蚂蚁的坐标:

def move(ants):
    ant_x, ant_y = ants

    # change ant_x and ant_y 
    # [...]

    ants[0] = ant_x
    ants[1] = ant_y

完整示例(从space栏开始)

import pygame as pg
import random as rd

pg.init()

screen = pg.display.set_mode((500,500))
title = pg.display.set_caption("TEST")

ant_x = 200
ant_y = 200
COLOR = (220,20,60,0)
ANT_WIDTH = 5

def move(ants):
    ant_x, ant_y = ants

    x_boundary = 1000
    y_boundary = 1000
    pos = pg.math.Vector2(ant_x, ant_y)
    c = rd.randrange(0, x_boundary)
    ant_y = rd.randrange(0, y_boundary)
    speed = 1
    maxdist = 1
    dist = rd.randrange(maxdist)
    direction = pg.math.Vector2(1, 2).rotate(rd.randrange(360))

    pos += direction * speed
    dist -= speed
    ant_x, ant_y = round(pos.x), round(pos.y)

    if dist <= 0:
        dist = rd.randrange(maxdist)
        direction = pg.math.Vector2(1, 0).rotate(rd.randrange(360))
    else:
        pass
    if ant_x >= screen.get_width(): #IF ANTS X IS OFF THE SCREEN WIDTH THEN RESET THE X POSITION
        ant_x = 200
        dist = rd.randrange(maxdist)
        direction = pg.math.Vector2(1, 0).rotate(rd.randrange(360))
    else:
        pass
    if ant_y >= screen.get_height(): #RESET POSITION IF ANTS Y IS OFF THE SCREEN HEIGHT
        ant_x = 200
        dist = rd.randrange(maxdist)
        direction = pg.math.Vector2(1, 0).rotate(rd.randrange(360))
    else:
        pass
    
    ants[0] = ant_x
    ants[1] = ant_y

ant_spawn = []
for i in range(10):
    ant_x = 250
    ant_y = 250
    ant_spawn.append([ant_x, ant_y])

move_ants = False
run = True   
while run:
    FPS = pg.time.Clock()
    for event in pg.event.get(): 
        if event.type == pg.QUIT:
            run = False
        if event.type == pg.KEYDOWN and event.key == pg.K_SPACE:
            move_ants = not move_ants

    if move_ants:
        for ants in ant_spawn:
            ants = move(ants)

    screen.fill((0,0,0))
    for ants in ant_spawn:
        #pg.draw.rect(screen, COLOR, (*ants, 3, 3))
        pg.draw.circle(screen, COLOR, ants, 5)
    pg.display.update()
    FPS.tick(60)

pg.quit()