I was following this guys tutorial on how to make an object rotate around another, as I wanted to make a sword rotate around the player and follow the mouse. On his machine the code works great and is exactly what I want to achieve, but on my machine it starts to consume a ton of RAM extremely fast and crashes the program.
class Sword():
def __init__(self, pivot:Vector2):
self.pivot = Vector2(pivot)
self.pos = pivot + Vector2(20, 0)
self.image_original = sword_1
self.image = self.image_original
self.rect = self.image.get_rect()
def update(self):
mouse_pos = Vector2(pygame.mouse.get_pos())
mouse_offset = mouse_pos - self.pivot
mouse_angle = -math.degrees(math.atan2(mouse_offset.y, mouse_offset.x))
self.image, self.rect = rotate_on_pivot(self.image, mouse_angle, self.pivot, self.pos)
def draw(self):
screen.blit(self.image, self.rect)
sword = Sword((character.x_pos, character.y_pos))
This is the code for creating the sword, updating its position based on the mouse, and then drawing it. There is also another function for rotating it around a pivot point.
def rotate_on_pivot(image, angle, pivot, origin):
surf = pygame.transform.rotate(image, angle)
offset = pivot + (origin - pivot)
rect = surf.get_rect(center = offset)
return surf, rect
I’ve found out that the issues begin when I call the update method. This happens even without using the draw method. I’ve always wondered how to manage my memory better and how to optimize my game, as I am running Acer Aspire A515 with only 8gb of RAM with Ubuntu 23.10, and I am relying on integrated performance. I also didn’t care about school up until recently, so now I’m trying to incorporate a bunch of math into my game and I don’t understand any of it, so if anyone could point me into the right direction for learning math from a computer science and game dev standpoint that would also be very helpful.
If anyone wants anymore information please let me know.
I tried commenting out the draw method to see if that was the problem, and I also checked to make sure I’m not creating a bunch of sword objects on accident.