0

im currently trying to create the Pong Game in Pycharm using the turtle module. I decieded to go as far as can myself, once i got stuck i actually realized that the course is using the x and y pos and the goto() function, whereas i was using a while loop and a forward() function to move the ball. So feeling i have failed completely as i was not supoused to do it like this, i want to believe there can be a way to change the ball heading using the degrees as the input. Allow me to share my Code.

def bottom_wall_whack(self):
    self.core_difference = 0

    if self.heading() > 90:
        self.core_difference = self.heading() - 90
        self.og_heading = 90 + self.core_difference
        self.setheading(self.og_heading)
    elif self.heading() < 90:
        self.core_difference = 90 - self.heading()
        self.og_heading = 90 + self.core_difference
        self.setheading(self.og_heading)

def top_wall_whack(self):
    if self.heading() < 90:
        self.og_heading -= 90
        self.setheading(self.og_heading)
        self.forward(10)
    elif self.heading() > 90:
        self.og_heading += 90
        self.setheading(self.og_heading)
        self.forward(10)

enter image description here

So for the arguments sake, lets say the ball is heading from the left side of the screen towards the right, going downwards.. lets say at an 135 degree angle, if it whacks the bottom floor of the screen and triggers the bottom_wall_whack, that should change the self.og_heading to be 45 degrees so it can go up.

So the real question is, how do i get bottom_wall_whack and top_wall_whack respectably to give back a value which would mirror the current angle its coliding with the wall onto the opposing quadrant of the square?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 3
    Note that you haven't [actually asked a question](/help/how-to-ask) yet: you've explained what you set out to do, you showed your code, but what _problem_ does this code have that you didn't expect, and how have you tried debugging this so far? Wall collision based on angles is a pretty well documented process, _lots_ of tutorials on the web about it in basically every programming language under the sun, but you already got there on your own: line comes in at heading X, lines goes out at heading (X, reflected over a vertical line). – Mike 'Pomax' Kamermans Dec 31 '22 at 17:35
  • The question is how to create a function that will, once invoked take the current heading of the ball, and based on it return a value which will be the new heading for the ball as if it bounced off in the Pong Game. Appologies for lack of context as i am still quite new to this :) – Did_Vidurina Dec 31 '22 at 17:37
  • 1
    Don't put that in a comment: _that's your question_, so [edit] your post, and make it clear that's the question =) But then also bear in mind that almost none of this code is relevant there: you want to know how to get angles in a fairly simple setup: you have a "thing" with a trajectory defined by a location and heading, you have a line that acts as wall, and you want to know what the trajectory is going to be after a collision. That just needs a picture. But, in reducing the problem to its core, _first_ we can search the web/SO for solutions to _that_ problem. Maybe you don't need this post. – Mike 'Pomax' Kamermans Dec 31 '22 at 17:41
  • Okay Mike, thanks for the input. im going to create an image to represent the problem, do you reckon i should of just put the relevant functions in? – Did_Vidurina Dec 31 '22 at 17:42
  • You should search first, now that you know what you're really looking for. – Mike 'Pomax' Kamermans Dec 31 '22 at 17:43
  • I havent found anyone using degrees to solve this in Python yet, i think this is the first time someone is using heading instead of coordinates. – Did_Vidurina Dec 31 '22 at 17:54
  • It really isn't, because it doesn't matter whether you encode your vector as a x/y pair or as an angle and amplitude, it's the same vector, and conversion between the two is trivial. In coordinates, the `y` flips sign, and `x` stays the same, in angles: the angle reflects over the x-axis. Draw it out (yourself, on paper) and then implement the code to do that. – Mike 'Pomax' Kamermans Jan 01 '23 at 01:23
  • Does this answer your question? [How to calculate reflection angle of a ball colliding with a wall](https://stackoverflow.com/questions/55626092/how-to-calculate-reflection-angle-of-a-ball-colliding-with-a-wall) – Mike 'Pomax' Kamermans Jan 01 '23 at 19:06
  • Realize that Pong was created when circuits were much more primitive - the rules don't rely on angles at all. The ball will have a delta x and delta y for each interval of time. If it hits a vertical surface you flip the sign on the delta x, and if it hits a horizontal surface you flip the sign on delta y. – Mark Ransom Jan 01 '23 at 19:31
  • @MarkRansom I actually managed to do it how i envisioned it. Granted feel dumb after figguring out it was only swapping x and y but ooh well, many ways to skin a Cat. Dont know if it was done like this in Python before – Did_Vidurina Jan 03 '23 at 14:49
  • 1
    If you figured it out, you should post an answer. – Mark Ransom Jan 03 '23 at 15:20

1 Answers1

0

EDIT: I managed to do it:

    
    **def ball_logic(self):
        self.forward(10)
    def whack(self):
        if self.heading() >= 0 and self.heading() < 90:
            self.offset = 90 - self.heading()
            self.og_heading = 90 + self.offset + random.randint(-40,40)
            self.setheading(self.og_heading)
            self.forward(10)
        elif self.heading() <= 360 and self.heading() > 270:
            self.offset = 360 - self.heading()
            self.og_heading = 180 + self.offset + random.randint(-40,40)
            self.setheading(self.og_heading)
            self.forward(10)
        elif self.heading() > 90 and self.heading() <= 270:
            if self.heading() <= 180:
                self.offset = 180 - self.heading()
                self.og_heading = self.offset + random.randint(-40,40)
                self.setheading(self.og_heading)
                self.forward(10)
            elif self.heading() > 180:
                self.offset = self.heading() - 180
                self.og_heading = 360 - self.offset + random.randint(-40,40)
                if self.og_heading > 360:
                    self.og_heading - 360
                self.setheading(self.og_heading)
    def bounce(self):
        if self.heading() > 0 and self.heading() < 90:
            self.offset = 90 - self.heading()
            self.og_heading = 270 + self.offset
            self.setheading(self.og_heading)
            self.forward(10)
        elif self.heading() >= 90 and self.heading() < 180:
            self.offset = 180 - self.heading()
            self.og_heading = 180 + self.offset
            self.setheading(self.og_heading)
            self.forward(10)
        elif self.heading() >= 180 and self.heading() < 270:
            self.offset = 270 - self.heading()
            self.og_heading = 180 - self.offset
            self.setheading(self.og_heading)
            self.forward(10)
        elif self.heading() >= 270 and self.heading() < 360:
            self.offset = 360 - self.heading()
            self.og_heading = self.offset
            self.setheading(self.og_heading)
            self.forward(10)**

``