Diego
Diego

Reputation: 35

blit an array of images in pygame

I´m doing an easy game where I want to display some imgs at random locations with their respective rects, heres my code:

class Juego1:
    def __init__(self, next_scene):
        self.background = pygame.Surface(size)
        self.imagenes1_array = ['autobus.png','coche.png','barco.png','autobus2.png','grua.png','bici.png']
        for i in self.imagenes1_array:
            self.imagenes1 = pygame.image.load(i)
            self.imagenes1_rect = self.imagenes1.get_rect()
            self.imagenes1_rect = self.imagenes1_rect.move(random.randint(0,1600),random.randint(0,900))
        
    def start(self, gamestate):
        self.gamestate = gamestate
        
       
    def draw(self,screen):
        self.background = pygame.Surface(size)
        x = random.randint(0,1600)
        y = random.randint(0,900)
        coordinates = (x,y)
        for i in self.imagenes1_array:
            imagenes1 = pygame.image.load(i).convert()
            screen.blit(imagenes1,x,y)

    
    def update(self, events, dt):
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    return (self.next_scene, None)

but it keeps sending me this error: invalid destination position for blit

What am I doing wrong? Also, I don´t know think the way I´m trying to display both, img load and rects of the image array is good so let me know thanks

Here is the full code:

# Importamos las librerías necesarias
import sys, pygame, pygame.freetype, random
from pygame.locals import *

size = 1600, 900

# Colours    R    G    B
GRAY     = (100, 100, 100)
NAVYBLUE = ( 60,  60, 100)
BLACK    = (  0,   0,   0)
WHITE    = (255, 255, 255)
RED      = (255,   0,   0)
GREEN    = (  0, 255,   0)
BLUE     = (  0,   0, 255)
YELLOW   = (255, 255,   0)
ORANGE   = (255, 128,   0)
PURPLE   = (127,   0, 255)
CYAN     = (  0, 255, 255)

class PrimeraEscena:

    def __init__(self, next_scene):
        self.background = pygame.Surface(size)

        self.flechaImg = pygame.image.load('flecha.png')
        self.flechaImg_rect = self.flechaImg.get_rect()
        self.flechaImg_rect = self.flechaImg_rect.move(1300,600)

        self.next_scene = next_scene
        
    def draw(self,screen):
        font = pygame.font.SysFont("comicsansms",90)
        img = font.render('Memory',True, PURPLE)
        screen.blit(img, (620,400)) 
        screen.blit(self.flechaImg, (1300,600))
    
    def update(self, events, dt):
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = event.pos
                if self.flechaImg_rect.collidepoint(mouse_pos):
                    return (self.next_scene, None)


class Juego1:
    def __init__(self, next_scene):
        self.background = pygame.Surface(size)
        
        self.images = []
        self.rects = []
        self.imagenes1_array = ['autobus.png','coche.png','barco.png','autobus2.png','grua.png','bici.png']
        for i in self.imagenes1_array:
            self.images.append(pygame.image.load(i))
            s = pygame.Surface((20,20))
            self.rects.append(s.get_rect())

        print(self.images)
        print(self.imagenes1_array)
        

    def start(self, gamestate):
        self.gamestate = gamestate
        for rect in self.rects:
            x = random.randint(300,1000)
            y = random.randint(200,700)
            rect.x = x
            rect.y = y
        
       
    def draw(self,screen):
        self.background = pygame.Surface(size)
        
        for i in range(len(self.images)):
            screen.blit(self.images[i], (self.rects[i].x, self.rects[i].y))
        
    
    def update(self, events, dt):
        for event in events:
            if event.type == pygame.MOUSEBUTTONDOWN:
                for rect in self.rects:
                    if rect.collidepoint(event.pos):
                        print('works!')

def main():
    # Inicializamos pygame
    pygame.init()
    
    # Definimos las dimensiones de la ventana (1600 x 900px) y reloj
    screen = pygame.display.set_mode(size)
    clock = pygame.time.Clock()

    # Ponemos el título e icono y fondo de la ventana
    pygame.display.set_caption("Quiz Game")
    icon = pygame.image.load('icon.png')
    pygame.display.set_icon(icon)
    fondoImg = pygame.image.load('fondo_memory.png')

    dt = 0
    scenes = {
        'PORTADA': PrimeraEscena('SEGUNDA'),
        'SEGUNDA': Juego1('SEGUNDA'),


    }
    scene = scenes['PORTADA']
    # Comenzamos el bucle del juego
    run=True
    while run:
        # RGB - Red, Green, Blue
        screen.fill ((255,255,255))
        
        # Imagen fondo
        screen.blit(fondoImg, (0,0))

        # Eventos del mouse 
        events = pygame.event.get()

        # Capturamos los eventos que se han producido
        for event in events:
            
            # Definimos eventos:
            if event.type == pygame.QUIT: # Si el evento es salir de la ventana, terminamos
                run = False
            
        result = scene.update(events, dt)
        if result:
            next_scene, state = result
            if next_scene:
                scene = scenes[next_scene]
                scene.start(state)
        scene.draw(screen)
        pygame.display.flip()
        dt = clock.tick(60)
        #pygame.display.update()
   

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 1001

Answers (2)

marienbad
marienbad

Reputation: 1453

You are not storing the loaded images to a list, you just call load, assign it, and then go again. You shouldn't be loading the images in draw. Load them to a list in init, and then iterate through the list in draw.

init:

self.images = []
self.imagenes1_array = ['autobus.png','coche.png','barco.png','autobus2.png','grua.png','bici.png']
        for i in self.imagenes1_array:
            self.images.append(pygame.image.load(i))

draw:

def draw(self,screen):
        self.background = pygame.Surface(size)
        x = random.randint(0,1600)
        y = random.randint(0,900)
        coordinates = (x,y)
        for i in self.images:
            screen.blit(i, (x,y))

Upvotes: 0

Rabbid76
Rabbid76

Reputation: 210948

The 2nd argument oh pygame.Surface.blit is a a pair of coordinates (e.g. tuple or list) with the position:

screen.blit(imagenes1,x,y)

screen.blit(imagenes1, (x, y))

Upvotes: 1

Related Questions