Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I am trying to make an object with very random movement which is fine, that works. But if I want the same object to append to the screen 10 times then it does not work or i dont know how to do that. The object is called ant. I am sorry if its a bad, unreadable code im still a beginner in pygame. So the ant object(which is a small red square), need to be visible on the screen 10 times, each ant need to move randomly. I appreciate all your answers thanks.

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have to create a list of coordinates instead a list of indices:

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

Move and draw the ants in loops:

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

The move function changes the coordinates of an ant:

def move(ants):
    ant_x, ant_y = ants

    # change ant_x and ant_y 
    # [...]

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

Complete example (start with space bar)

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading