How do i blit multiple same images but at different coordinates?

Viewed 160

im trying to create a game where multiple of the same images will blit randomly along the borders of my window. but I do not know how to blit it multiple times and also along the borders. Here's the code so far:

import pygame, sys

from pygame.locals import *
import random

pygame.init()
DisplayWidth = 700
DisplayHeight = 400
Display = pygame.display.set_mode((DisplayWidth, DisplayHeight))
Death = False
  
  
def PlaceElon():
  ElonX = random.randrange(0, 700, 700)
  ElonY = random.randrange(0, 400)
  x = []
  y = []
  Elonlist = [x, y]
  elon = pygame.image.load('elon.png')
  elonbig = pygame.transform.smoothscale(elon, (50, 54))
  for x in Elonlist:
    x.append(ElonX)
  for y in Elonlist:
    y.append(ElonY)
  Display.blit(elonbig, (Elonlist))
  pygame.display.update()
  
  
def RunGame():
  while not Death:
    background = pygame.image.load('background.png')
    BigBackground = pygame.transform.smoothscale(background, (DisplayWidth, DisplayHeight))
    Display.blit(BigBackground, (0,0))
    PlaceElon()
    
RunGame()
1 Answers

You need to generate a list of coordinates:

Elonlist = []
noOfElons = 10
for _ in range(noOfElons)
    x = random.randrange(0, 700 - elon.get_width())
    y = random.randrange(0, 400 - elon.get_height())
    Elonlist.appned((x, y))

Draw the images in a loop:

for ElonPos in Elonlist:
    Display.blit(elon, ElonPos)

However there are some more problems in your application.

Generate the positions and load the images before the application loop:

background = pygame.image.load('background.png')
BigBackground = pygame.transform.smoothscale(background, (DisplayWidth, DisplayHeight))
elon = pygame.image.load('elon.png')

The typical PyGame application loop has to:

# main application loop
run = True
while run:

    # limit frames per second
    clock.tick(60)

    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # draw background
    Display.blit(BigBackground, (0,0))

    # draw the scene   
    for elonPos in Elonlist:
        Display.blit(elon, elonPos)

    # update the display
    pygame.display.flip()

pygame.quit()
exit()
Related