When changing the value of a variable, all objects in the same class are changing

Asked

Viewed 91 times

1

I have a code that creates a class, with a location attribute, one (x, y). I ask the program to create 2 objects of this class with the random location attribute around a circle. However, they both have the same location.

import math
import pygame
from random import randint

inimigo_img = pygame.image.load('inimigo.png')
largura, altura = 640, 640


class Enemy:
    image = inimigo_img
    rect = inimigo_img.get_rect()
    rect.center = 0, 0


new_inimigo = Enemy()
ang = math.radians(randint(0, 359))
new_inimigo.rect.center = largura / 2 + 200 * math.cos(ang), altura / 2 + 200 * math.sin(ang)

new_inimigo2 = Enemy()
ang = math.radians(randint(0, 359))
new_inimigo2.rect.center = largura / 2 + 200 * math.cos(ang), altura / 2 + 200 * math.sin(ang)

print(new_inimigo2.rect.center)
print(new_inimigo.rect.center)

The result is always the same number. How to make the result give different numbers to different objects?

  • new_enemy = Enemy() guess missing parenthesis to start object

2 answers

2


Their variables image and rect are being defined as class variables. This means that every object created based on that class will have the same value as image and rect. To solve this using OOP, you can define variables image and rect different for each created object of a class. It looks like you can do this:

class Enemy:    
    def __init__(self):
        self.image = inimigo_img
        self.rect = inimigo_img.get_rect()
        self.rect.center = 0, 0

Here, every time you create a class object Enemy, you are assigning a variable image and a variable rect specifically for that object. In the old way, variables were being shared between class objects.

The complete code with modifications:

import math
import pygame
from random import randint

inimigo_img = pygame.image.load('teste.jpg')
largura, altura = 640, 640


class Enemy:    
    def __init__(self):
        self.image = inimigo_img
        self.rect = inimigo_img.get_rect()
        self.rect.center = 0, 0


new_inimigo = Enemy()
ang = math.radians(randint(0, 359))
new_inimigo.rect.center = largura / 2 + 200 * math.cos(ang), altura / 2 + 200 * math.sin(ang)

new_inimigo2 = Enemy()
ang = math.radians(randint(0, 359))
new_inimigo2.rect.center = largura / 2 + 200 * math.cos(ang), altura / 2 + 200 * math.sin(ang)

print(new_inimigo2.rect.center)
print(new_inimigo.rect.center)
  • thank you very much, it helped a lot

0

Dude, I was able to do 2 different results changing the name of one of the variables to rreecctt, also excludes the rect.center variable from her class, because she was basically a dead weight

import math
import pygame
from random import randint

inimigo_img = pygame.image.load('inimigo.png')
largura, altura = 640, 640


class Enemy:
    image = inimigo_img
    rect = inimigo_img.get_rect()



new_inimigo = Enemy()
ang = math.radians(randint(0, 359))
new_inimigo.rreecctt = largura / 2 + 200 * math.cos(ang), altura / 2 + 200 * math.sin(ang)

new_inimigo2 = Enemy()
ang = math.radians(randint(0, 359))
new_inimigo2.rreecctt = largura / 2 + 200 * math.cos(ang), altura / 2 + 200 * math.sin(ang)

print(new_inimigo2.rreecctt)
print(new_inimigo.rreecctt)
  • not that way - see @Rafaelbarros' answer to understand the difference of class attribute and instance in Python.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.