1

I have a rotating player around a point. I want it to look like the player is attatched to the the string. Here is my player and string class

class Player:
    def __init__(self):
        self.pos = [0, 0]
        self.orbital_angle = 0
        self.player = pygame.Surface((20, 20))
        self.player.fill((100, 200, 100))
        self.radius = [200, 200]
        self.center = [600, 300]
        self.speed = 0.005

    def draw(self):
        D.blit(self.player, (self.pos[0], self.pos[1]))

    def orbit(self):
        self.orbital_angle += self.speed
        self.pos[0] = self.radius[0] * math.cos(self.orbital_angle) + self.center[0]
        self.pos[1] = self.radius[1] * math.sin(self.orbital_angle) + self.center[1]

class String:
    def __init__(self, x, y):
        self.pos = [x, y]

    def get_angle(self):
        return math.degrees((-player.orbital_angle) + math.radians(90))

    def get_length(self):
        dx = player.pos[0] - self.pos[0]
        dy = player.pos[1] - self.pos[1]
        length = math.hypot(dx, dy)
        return length

    def surface(self):
        string = pygame.Surface((2, self.get_length())).convert_alpha()
        string.fill((0, 255, 0))
        return string

    def rotated_surface(self):
        return pygame.transform.rotate(self.surface(), (self.get_angle()))

    def draw(self):
        self.rect = self.rotated_surface().get_rect(center = self.pos)
        D.blit(self.rotated_surface(), (self.rect.centerx, self.rect.centery))

The string class just takes the orbital_angle of the player, which is in radians, converts it to degrees and uses that angle to rotate the string(which is just a pygame.Surface). According to my knowledge, this should work but the angle is off. The position of the string appears to change in the screen for some reason although i never changed it in the code and they are constant when i print the values out as well. Thanks for helping

Copy and paste code to see whats happening:

import math, sys, os, pygame
from pygame.locals import *

pygame.init()

win = pygame.display
D = win.set_mode((1200, 600))

class Player:
    def __init__(self):
        self.pos = [0, 0]
        self.orbital_angle = 0
        self.player = pygame.Surface((20, 20))
        self.player.fill((100, 200, 100))
        self.radius = [200, 200]
        self.center = [600, 300]
        self.speed = 0.005

    def draw(self):
        D.blit(self.player, (self.pos[0], self.pos[1]))

    def orbit(self):
        self.orbital_angle += self.speed
        self.pos[0] = self.radius[0] * math.cos(self.orbital_angle) + self.center[0]
        self.pos[1] = self.radius[1] * math.sin(self.orbital_angle) + self.center[1]

class String:
    def __init__(self, x, y):
        self.pos = [x, y]

    def get_angle(self):
        return math.degrees((-player.orbital_angle) + math.radians(90))

    def get_length(self):
        dx = player.pos[0] - self.pos[0]
        dy = player.pos[1] - self.pos[1]
        length = math.hypot(dx, dy)
        return length

    def surface(self):
        string = pygame.Surface((2, self.get_length())).convert_alpha()
        string.fill((0, 255, 0))
        return string

    def rotated_surface(self):
        return pygame.transform.rotate(self.surface(), (self.get_angle()))

    def draw(self):
        self.rect = self.rotated_surface().get_rect(center = self.pos)
        D.blit(self.rotated_surface(), (self.rect.centerx, self.rect.centery))

player = Player()
string = String(600, 300)

while True:
    D.fill((0, 0, 0))
    pygame.event.get()

    string.draw()

    player.draw()
    player.orbit()

    pygame.draw.circle(D, (255, 255, 255), (string.pos[0], string.pos[1]), 2)

    win.flip()
  • 1
    Having the methods of your classes access global variables like that is asking for trouble. One of the main reasons the use of global variables is discourage is because it can make debugging problems even harder. – martineau Mar 01 '20 at 12:57
  • 4
    See [How can you rotate an image around an off center pivot in Pygame](https://stackoverflow.com/questions/59909942/how-can-you-rotate-an-image-around-an-off-center-pivot-in-pygame/59909946#59909946) – Rabbid76 Mar 01 '20 at 13:47
  • @matrineau So whats the right way of doing it? It hasent been long since i started using classes. –  Mar 01 '20 at 18:26

0 Answers0