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
– Elton Nunes