Seite 1 von 1

Pygame Sprite Objekt zum Maus Zeiger bewegen

Verfasst: Freitag 4. September 2015, 10:54
von ibrahim00
Ich habe folgendes Sprite Objekt:

Code: Alles auswählen

class Bullet(pygame.sprite.Sprite):
    def __init__(self, *groups):
        Sprite.__init__(self, *groups)
        self.image = pygame.image.load('images/baz.png')
        self.rect = self.image.get_rect()
        self.damage = 1

    def update(self):
        self.rect.x += 4

...

                elif event.key == pygame.K_SPACE:
                    print(pygame.mouse.get_pos())
                    bullet = Bullet(sprites)
                    bullet.rect.x = player.rect.right - bullet.rect.width
                    bullet.rect.y = player.rect.y
                    bullets.add(bullet)

...
Im Moment ist es so, dass dieser Bullet von der x und y Position des Spielers abgefeuert wird und sich +4 auf der x-Achse bewegt. Nun will ich es aber so machen, dass dieser Bullet da hin fliegt wo sich meine Maus befunden hat zu dem Zeitpunkt als er abgeschossen wurde. Ich habe gelesen, dass man dafür Vektoren benutzen soll und ich habe auch schon mal einen Test gemacht und habe den Vektor Abstand und einen Unit Vektor berechnet, aber irgendwie weiß ich nicht wie mir das jetzt helfen soll :K

Code: Alles auswählen

import math

class Vector(object):
    def __init__(self, x, y):
        self.diff = (y[0] - x[0], y[1] - x[1])
        print(self.diff)

    def distance(self):
        self.a = self.diff[0]
        self.b = self.diff[1]
        return math.sqrt(self.a**2 + self.b**2)

    def unit(self):
        distance = self.distance()
        self.a_unit = self.a/distance
        self.b_unit = self.b/distance
        return self.a_unit, self.b_unit


x = (20.0, 25.0)
y = (40.0, 55.0)

thing = Vector(x, y)

print thing.distance()
print thing.unit()
Kann mir jemand auf die Sprünge helfen? :lol:

Re: Pygame Sprite Objekt zum Maus Zeiger bewegen

Verfasst: Freitag 4. September 2015, 13:44
von ibrahim00
Ich weiß leider nicht wie man den Post editieren kann deswegen poste ich das mal als Antwort. Ich habe diesen code Schnipsel mal geschrieben. Für mich ist der sinnergebend aber offensichtlich falsch.. Kann mir jemand sagen wo mein Denkfehler liegt?

Code: Alles auswählen

player_x = 20
player_y = 25

mouse_x = 40
mouse_y = 55

x_speed = 2
y_speed = 2

while True:
    x_distance = mouse_x - player_x;
    y_distance = mouse_y - player_y;
    distance = math.sqrt(x_distance**2 + y_distance**2)
    if (distance > 1):
        player_x += x_speed
        player_y += y_speed
        print("x: " + str(x_distance))
        print("y: " + str(y_distance))
        print("distance: " + str(distance))

Re: Pygame Sprite Objekt zum Maus Zeiger bewegen

Verfasst: Freitag 4. September 2015, 14:15
von Sirius3
@ibrahim00: ohne die mathematischen Grundlagen der Vektorrechnung zu verstehen, wirst Du hier nicht weiter kommen.

Re: Pygame Sprite Objekt zum Maus Zeiger bewegen

Verfasst: Montag 7. September 2015, 14:01
von ibrahim00
Ja ich habe das jetzt so gemacht.

Code: Alles auswählen

import math

player_x = 20
player_y = 25

mouse_x = 40
mouse_y = 55

x_speed = 2
y_speed = 2

while True:
    x_distance = mouse_x - player_x;
    y_distance = mouse_y - player_y;
    distance = math.sqrt(x_distance**2 + y_distance**2)
    normalized_x = x_distance / distance
    normalized_y = y_distance / distance
    if (distance > 1):
        player_x += normalized_x * x_speed
        player_y += normalized_y * y_speed
        print("x: " + str(x_distance))
        print("y: " + str(y_distance))
        print("distance: " + str(distance))