0

I'm making a tower defense game in pygame where I'm trying to make a Drag&Drop interface where the player can pick the tower and place it in any desired place. I then advanced the game by making a circle around each tower which denotes the range of it. Now the problem here is that I need the circle to be in the center. As I have used classes for the towers, adding to the x and y coordinates did not help because I had different sized towers. I just need to know how to find the center of an image in pygame. Help will be appreciated. Thanks!

Here is the classes part of my code

class DragProperty():
    def __init__(self, img, x, y):
        self.img = img
        self.x = x
        self.y = y
        self.width = img.get_width()
        self.height = img.get_height()
        self.dragging = False

    def draw(self):
        screen.blit(self.img, (self.x, self.y))

    def startdrag(self):
        x = self.x
        y = self.y

        if mousex > self.x and mousex < self.x + self.width:
            if mousey > self.y and mousey < self.y + self.height:
                self.dragging = True
                self.offset_x = x - mousex
                self.offset_y = y - mousey

    def beingdragged(self):
        if self.dragging:
            self.x = mousex + self.offset_x
            self.y = mousey + self.offset_y

    def stopdrag(self):
        global balance
        global canDrag

        self.dragging = False
        canDrag = False

        balance -= 250

class AttackModule(DragProperty):
    def EnenmyinRange(self):
        pygame.draw.circle(screen, (0, 0, 0, 0), (self.x + 20, self.y + 20), 100)

    def attack(self):
        global balance
        
        balance += 50

#objects
MenuArcher = AttackModule(archertower, 5, 580)
PradMaster
  • 83
  • 10

1 Answers1

1

Since you already have width and height defined, you can do: self.x + self.width / 2 and self.y + self.height / 2

Starbuck5
  • 1,649
  • 2
  • 7
  • 13