2

I am designing a basketball simulator in pygame.

I need it to work so that if the space ahead of a player on 1 team is clear he will move towards the hoop, if not he will attempt to get past said obstruction with a predetermined chance of successfully crossing the defense.

I first encountered an issue because I was using the offensive players x co-ordinate and simply moving the defense to (x + 30), like this:

for i in range(numPlayers):
            if possesion == ("team1"):
                myGame1.team2players[i].x = (myGame1.team1players[i].x + 30)
                myGame1.team2players[i].y = myGame1.team1players[i].y

However this raised issue because despite it working there was a brief moment for every cycle of the game loop where the defense is moved (the x changes) and the offense would see this as "wide open":

for i in range(numPlayers):
        if theBall.holder == myGame1.team2players[i] and myGame1.team1players[i].x != range(0,int(theBall.holder.x)):
            print("Wide")
        else:
            print("Nope")

Now I have changed my code to have the player only move when there is no obstruction, this does work but once he meets the defense he wont continue moving (even when I set his chances of crossing them to 100%)...

            for i in range(numPlayers):
                if (theBall.holder == myGame1.team2players[i] and myGame1.team1players[i].x <= theBall.holder.x) or (theBall.holder == myGame1.team1players[i] and myGame1.team2players[i].x > theBall.holder.x):
                    onballmove = Player.move(theBall.holder)
                else:
                    cross = random.choices((True, False), [1,0])
                    if cross == (True):
                        onballmove = Player.move(theBall.holder)

Is there a more efficient (working) means of solving this problem?

Please note:

  • The player must move when not obstructed
  • When obstructed the probability of continuning his path must be calculated
  • The defense must move in front of the offensive player not behind or above(must stop their x)

Here is a picture of the code full code running to show (myGame1.team2players[i] is every player on team 2; team 2 is always the team on the right hand side of the court (the red "CAVS"):

Here is a screenshot of the game. The color red is team 2 from above. The code should make sense with this.

Red is team 2 as explained above: code should make sense with this

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26
  • 4
    Whoever is going to answer this will probably need a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). – Micheal O'Dwyer Feb 13 '18 at 23:53

0 Answers0