0

so i was wondering if i could quickly spawn a bunch of different shapes in pygame and have them all move randomly and spawn at different places without needing to write a bunch of if's. Here's the code: import pygame import random import time

# Initializing Pygame
pygame.init()

# Initializing surface
surface = pygame.display.set_mode((5000,5000))
x = 300
y = 100
x2 = 150
y2 = 150
# Initialing Color
color2 = (0,255,0)
color = (255,0,0)
pygame.draw.rect(surface, color, pygame.Rect(30, 30, 60, 60))
# Drawing Rectangle

pygame.display.flip()

while True:
time.sleep(0.2)
var = random.randint(1,4)
var2 = random.randint(1,4)
if (var == 1):
  x += 20
if (var == 2):
  x -= 20
if (var == 3):
  y += 20
if (var == 4):
  y -= 20

if (var2 == 1):
  x2 += 20
if (var2 == 2):
  x2 -= 20
if (var2 == 3):
  y2 += 20
if (var2 == 4):
  y2 -= 20


 surface.fill((0,0,0))
 pygame.draw.rect(surface, color, pygame.Rect(x, y, 60, 60))
 pygame.draw.rect(surface, color2, pygame.Rect(x2, y2, 60, 60))
 if (x == x2 or y == y2 and color == (255,0,0)):
  color2 = color
  print("oof")
 pygame.display.update()
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
coderboi
  • 1
  • 1
  • A way to spawn multiple objects is, make a list, fill the list with `rect` but each `rect` has a random position. Now that you have a list full of `rect`, in the main loop, you can iterate through the list and blit it to the screen – PyxlDavon Oct 22 '21 at 00:26
  • thanks for the idea if you could just give a small example thx – coderboi Oct 22 '21 at 00:54

0 Answers0