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

How to load various images using a loop

Im making a calculator using Pygame, and to simplify the code, im trying to load all those variables in an easier way.

one = pygame.image.load("img/1.png")
two = pygame.image.load("img/2.png")
three = pygame.image.load("img/3.png")
four = pygame.image.load("img/4.png")
five = pygame.image.load("img/5.png")
six = pygame.image.load("img/6.png")
seven = pygame.image.load("img/7.png")
eight = pygame.image.load("img/8.png")
nine = pygame.image.load("img/9.png")
zero = pygame.image.load("img/0.png")

I thought of this:

numeros = ["uno","dos","tres","cuatro","cinco","seis","siete","ocho","nueve","cero"]
for x in numeros:
    def load(x):
        x = pygame.image.load("img/" + x +".png")
        time.sleep(0.1)
        print(x)

but when i try to screen.blit(uno),for example, it ways "uno is not defined",even though i defined it in the function.

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

For anyone curious, this is the rest of my code. Any advice would be greatly appreciated on how to make it less messy, im a beginner.

import pygame
from sys import exit
import random
import time
#starts pygame (images and sounds)
pygame.init()
pygame.mixer.init()
#create screen variable
width = 560
height = 700
screen = pygame.display.set_mode((width,height))

#ponerle titulo
pygame.display.set_caption("Calculator")

#reloj/framerate
clock = pygame.time.Clock()
fondo = pygame.Surface((560,700))
fondo.fill("gray")
one = pygame.image.load("img/1.png")
two = pygame.image.load("img/2.png")
three = pygame.image.load("img/3.png")
four = pygame.image.load("img/4.png")
five = pygame.image.load("img/5.png")
six = pygame.image.load("img/6.png")
seven = pygame.image.load("img/7.png")
eight = pygame.image.load("img/8.png")
nine = pygame.image.load("img/9.png")
zero = pygame.image.load("img/0.png")
suma = pygame.image.load("img/suma.png")
resta = pygame.image.load("img/resta.png")
multiplicacion = pygame.image.load("img/multiplicacion.png")
division = pygame.image.load("img/division.png")
igual = pygame.image.load("img/igual2.png")
raton = pygame.Surface((1,1))

number1active = "Active"
number2active = "Inactive"
signo = "nada"
number1 = ""
number2 = ""
number1score = pygame.font.Font("fonts/Pixeltype.ttf",100)
number2score = pygame.font.Font("fonts/Pixeltype.ttf",100)
resultadoscore = pygame.font.Font("fonts/Pixeltype.ttf",100)
resultado = 0
resultadofinal = False
while True: 
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.QUIT()
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if resultadofinal == 1:
                    resultadofinal = 0
                    number1 = ""
                    number2 = ""
                    resultado = 0
                    number1active ="Active"
                if 0 < xraton < 130 and 100 < yraton < 240:
                    if number1active == "Active":
                        number1 = number1 + "1"
                    elif number2active == "Active":
                        number2 = number2 + "1"
                if 140 < xraton < 270 and 100 < yraton < 240:
                    if number1active == "Active":
                        number1 = number1 + "2"
                    elif number2active == "Active":
                        number2 = number2 + "2" 
                if 280 < xraton < 410 and 100 < yraton < 240:
                    if number1active == "Active":
                        number1 = number1 + "3"
                    elif number2active == "Active":
                        number2 = number2 + "3"
                if 0 < xraton < 130 and 250 < yraton < 390:
                    if number1active == "Active":
                        number1 = number1 + "4"
                    elif number2active == "Active":
                        number2 = number2 + "4"
                if 140 < xraton < 270 and 250 < yraton < 390:
                    if number1active == "Active":
                        number1 = number1 + "5"
                    elif number2active == "Active":
                        number2 = number2 + "5"
                if 280 < xraton < 410 and 250 < yraton < 390:
                    if number1active == "Active":
                        number1 = number1 + "6"
                    elif number2active == "Active":
                        number2 = number2 + "6"
                if 0 < xraton < 130 and 400 < yraton < 540:
                    if number1active == "Active":
                        number1 = number1 + "7"
                    elif number2active == "Active":
                        number2 = number2 + "7"
                if 140 < xraton < 270 and 400 < yraton < 540:
                    if number1active == "Active":
                        number1 = number1 + "8"
                    elif number2active == "Active":
                        number2 = number2 + "8"
                if 280 < xraton < 410 and 400 < yraton < 540:
                    if number1active == "Active":
                        number1 = number1 + "9"
                    elif number2active == "Active":
                        number2 = number2 + "9"
                if 0 < xraton < 130 and 550 < yraton < 690:
                    if number1active == "Active":
                        number1 = number1 + "0"
                    elif number2active == "Active":
                        number2 = number2 + "0"
                if 420 < xraton < 550 and 100 < yraton < 240:
                    signo = "+"
                    number1active = "Inactive"
                    number2active = "Active"
                if 420 < xraton < 550 and 250 < yraton < 390:
                    signo = "-"
                    number1active = "Inactive"
                    number2active = "Active"
                if 420 < xraton < 550 and 400 < yraton < 540:
                    signo = "*"
                    number1active = "Inactive"
                    number2active = "Active"
                if 420 < xraton < 550 and 550 < yraton < 690:
                    signo = "/"
                    number1active = "Inactive"
                    number2active = "Active"
                if 140 < xraton < 410 and 550 < yraton < 690:
                    resultadofinal = 1
                    number2active = "Inactive"
                    if signo == "+":
                        resultado = int(number1) + int(number2)
                    elif signo == "-":
                        resultado = int(number1) - int(number2)
                    elif signo == "*":
                        resultado = int(number1) * int(number2)
                    elif signo == "/":
                        resultado = int(number1) / int(number2)

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    if number1active == "Active":
                        number1 = number1[:-1]
                    elif number2active == "Active":
                        number2 = number2[:-1]
    
    (xraton,yraton) = pygame.mouse.get_pos()
    screen.blit(fondo,(0,0))
    screen.blit(one,(0,100))
    screen.blit(two,(140,100))
    screen.blit(three,(280,100))
    screen.blit(four,(0,250))
    screen.blit(five,(140,250))
    screen.blit(six,(280,250))
    screen.blit(seven,(0,400))
    screen.blit(eight,(140,400))
    screen.blit(nine,(280,400))
    screen.blit(zero,(0,550))
    screen.blit(suma,(420,100))
    screen.blit(resta,(420,250))
    screen.blit(multiplicacion,(420,400))
    screen.blit(division,(420,550))
    screen.blit(igual,(140,550))
    number1surface = number1score.render(number1, False, (0, 0, 0))
    number2surface = number2score.render(number2, False, (0, 0, 0))
    resultadosurface = resultadoscore.render(str(resultado), False, (0, 0, 0))
    if number1active == "Active":
        screen.blit(number1surface,(10,30))  
    elif number2active == "Active":
        screen.blit(number2surface,(10,30))  
    if resultadofinal == 1:
        screen.blit(resultadosurface,(10,30))
        
    screen.blit(raton,(xraton,yraton))
    
    pygame.display.update()

I would very much appreciate some guidance in this problem 🙂

>Solution :

I suggest to store the images in a list, instead of using a separate variable for each image:

numbers = []
for i in range(10):
    image = pygame.image.load("img/" + str(i) + ".png") 
    numbers.append(image) 

or even shorter

numbers = [pygame.image.load(f'img/{i}.png') for i in range(10)]

You can access the number images via subscription: numbers[0], numbers[1], …

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