So basically, I’m making a game of golf in Python. This mostly just revolves around the math side of things, but if you really need to know, I’m using python Turtle.
This program is an advanced method of getting the angle at which to launch the ball. Simple enough. Originally, I had the user enter an angle from 1 to 360, which was functionally sound but just not intuitive for the user to play. So I decided on a new method for getting the angle from the user, which involved the WASD keys. The user would enter a string with these 4 characters, and depending how many of each character the user enters, the more the ball will head in that direction. For example, if I enter “wwa”, the ball will head North-NorthWest.
The problem I’m having is that if the user enters “wd”, the w and the d will average out to be 225 (which is the exact OPPOSITE of the desired result.) Is there any way to prevent this? The problem is obviously in the rounding, but I’m not sure where to start.
North = 360 , East = 90 , South = 180 and West = 270
No matter what you do, there will always be two values that are 270º apart from each other and cause this problem.
ballinput = input("Angle: ")
ballangle = 0
degreelist = [] #Each of the below statements appends the degrees
for i in range(ballinput.count("w")): #to the degree list
degreelist.append(360)
for i in range(ballinput.count("a")):
degreelist.append(270)
for i in range(ballinput.count("s")):
degreelist.append(180)
for i in range(ballinput.count("d")):
degreelist.append(90)
for item in degreelist:
ballangle += int(item) #which are added together so they can be
ballangle /= len(ballinput) #averaged out into the final ball angle.