Checking multiple conditions in a single if statement /u/case_steamer Python Education

I know that somewhere in my brain I already know this answer, but I’ve spent three days on this particular solution to a problem I’ve been working on for two weeks. The project is Angela Yu’s Turtle Crossing game (basically Frogger), and I’m trying to get the program to recognize when the user turtle is within A pixel distance on either the X or the Y axis. So I have this function (inside the game manager class) which runs at the end of every game loop:

def sense(self, player, npcs): for n in npcs: print(n.ycor()) if abs(player.ycor() - n.ycor()) <= 40: ydist = abs(player.ycor() - n.ycor()) else: ydist = None if abs(player.xcor() - n.xcor()) <= 40: xdist = abs(player.xcor() - n.xcor()) else: xdist = None if ydist is None or xdist is None: return None elif ydist <= 40 and xdist <= 40: return True 

Then I have this corresponding execution code inside my main game loop:

score = game.sense(user, game.npcs) if score: game_is_on = False 

My test is setting my user directly on top of a npc; it only runs as intended about 1/4 of the time. The rest of the time nothing happens. How do I get line 15 of the sense() function to run consistently? What am I missing?

submitted by /u/case_steamer
[link] [comments]

​r/learnpython I know that somewhere in my brain I already know this answer, but I’ve spent three days on this particular solution to a problem I’ve been working on for two weeks. The project is Angela Yu’s Turtle Crossing game (basically Frogger), and I’m trying to get the program to recognize when the user turtle is within A pixel distance on either the X or the Y axis. So I have this function (inside the game manager class) which runs at the end of every game loop: def sense(self, player, npcs): for n in npcs: print(n.ycor()) if abs(player.ycor() – n.ycor()) <= 40: ydist = abs(player.ycor() – n.ycor()) else: ydist = None if abs(player.xcor() – n.xcor()) <= 40: xdist = abs(player.xcor() – n.xcor()) else: xdist = None if ydist is None or xdist is None: return None elif ydist <= 40 and xdist <= 40: return True Then I have this corresponding execution code inside my main game loop: score = game.sense(user, game.npcs) if score: game_is_on = False My test is setting my user directly on top of a npc; it only runs as intended about 1/4 of the time. The rest of the time nothing happens. How do I get line 15 of the sense() function to run consistently? What am I missing? submitted by /u/case_steamer [link] [comments] 

I know that somewhere in my brain I already know this answer, but I’ve spent three days on this particular solution to a problem I’ve been working on for two weeks. The project is Angela Yu’s Turtle Crossing game (basically Frogger), and I’m trying to get the program to recognize when the user turtle is within A pixel distance on either the X or the Y axis. So I have this function (inside the game manager class) which runs at the end of every game loop:

def sense(self, player, npcs): for n in npcs: print(n.ycor()) if abs(player.ycor() - n.ycor()) <= 40: ydist = abs(player.ycor() - n.ycor()) else: ydist = None if abs(player.xcor() - n.xcor()) <= 40: xdist = abs(player.xcor() - n.xcor()) else: xdist = None if ydist is None or xdist is None: return None elif ydist <= 40 and xdist <= 40: return True 

Then I have this corresponding execution code inside my main game loop:

score = game.sense(user, game.npcs) if score: game_is_on = False 

My test is setting my user directly on top of a npc; it only runs as intended about 1/4 of the time. The rest of the time nothing happens. How do I get line 15 of the sense() function to run consistently? What am I missing?

submitted by /u/case_steamer
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *