Python Forum
Newbie Turtle if/then coordinates Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie Turtle if/then coordinates Problem
#1
I'm new to Python & I'm making a Pong-style hockey game and have a problem with the (goal scoring OR puck bouncing) coordinates. When I code the following (see below), the only thing that happens is the puck bounces no matter what the y-coordinate is. I have tried changing the y-coordinates to <= for one or the other; changed the parens placements; (unsuccessfully) tried if/else instead of two if statements; changed the order of the two conditions. In all cases (excepting the if/else which just didn't work at all), the puck bounced, never scored. I am at a loss. Everything else works fine. Thanks for any help! (Problem area highlighted)

if ball.xcor() > 390 and ball.ycor() < 200: # Problem area
		ball.setx(390)
		ball.dx *= -1
		winsound.PlaySound("puck.wav", winsound.SND_ASYNC)

	if ball.xcor() > 390 and ball.ycor() > 200: # Problem area
		winsound.PlaySound("horn.wav ", winsound.SND_ASYNC)
		ball.goto(0, 0)
		time.sleep(2)
		ball.dx *= -1
		score_a += 1
		pen.clear()
		pen.write(f"Habs: {score_a}                   Leafs: {score_b}", align="center", font=("Lucida Console", 24, "bold"))
Yoriz write Jun-22-2021, 09:09 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The second if block will never be executed. If ball.xcor () returns anything greater that 390 then ball.setx () will reset it to 390. So, when you get to the second if then xcor will always be exactly 390 or below.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020