OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why is the bat/board not moving up or down in the Pong Game, when the screen.listen() is called using the Turtle module in Python?

  • Thread starter Thread starter chris
  • Start date Start date
C

chris

Guest
Using the turtle module to make a pong game. For this part, when I press the up/down keys the board doesn't respond to the onkey listen function.

I created the board/bat (or bat specs function from turtle called

Code:
bat_specs_p2()

) and set the positions of the board/bat (x and y) on the screen. All is fine at this point. I know that the y-coord will have to change once the up/down keys are pressed. Testing with print shows that the y-coords are been updated everytime up/down is pressed, but the board/bat is not moving on the screen. I think it is still holding the starting position of the board/bat, somewhere the y-coord is not updating

Bat class​


Code:
from turtle import Turtle

P1_x = -200
P1_y = 0
P2_x = 200
P2_y = 0


class Bat:

    def __init__(self):

        self.p2_position = (P2_x, P2_y)
        self.p1_position = (P1_x, P1_y)

    def bat_specs_p2(self):
        global P2_y
        bat = Turtle()
        bat.penup()
        bat.shape("square")
        bat.color("white")
        bat.shapesize(stretch_wid=4, stretch_len=1)
        bat.setposition(self.p2_position)


    def p2move_up(self):
        global P2_y
        P2_y += 20
        self.bat_specs_p2()

        print(P2_x, P2_y)

    def p2move_down(self):
        global P2_y
        P2_y -= 20
        self.bat_specs_p2()

        print(P2_x, P2_y)

main.py​


Code:
from turtle import Screen
import time
from bat import Bat

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Pong Game")

screen.tracer(0)  

bat = Bat()

bat.bat_specs_p2()

screen.listen()
screen.onkey(bat.p2move_up, "Up")
screen.onkey(bat.p2move_down, "Down")


screen.update() 

screen.exitonclick()
<p>Using the turtle module to make a pong game. For this part, when I press the up/down keys the board doesn't respond to the onkey listen function.</p>
<p>I created the board/bat (or bat specs function from turtle called</p>
<pre><code>bat_specs_p2()
</code></pre>
<p>) and set the positions of the board/bat (x and y) on the screen. All is fine at this point.
I know that the y-coord will have to change once the up/down keys are pressed. Testing with print shows that the y-coords are been updated everytime up/down is pressed, but the board/bat is not moving on the screen. I think it is still holding the starting position of the board/bat, somewhere the y-coord is not updating</p>
<h1>Bat class</h1>
<pre><code>from turtle import Turtle

P1_x = -200
P1_y = 0
P2_x = 200
P2_y = 0


class Bat:

def __init__(self):

self.p2_position = (P2_x, P2_y)
self.p1_position = (P1_x, P1_y)

def bat_specs_p2(self):
global P2_y
bat = Turtle()
bat.penup()
bat.shape("square")
bat.color("white")
bat.shapesize(stretch_wid=4, stretch_len=1)
bat.setposition(self.p2_position)


def p2move_up(self):
global P2_y
P2_y += 20
self.bat_specs_p2()

print(P2_x, P2_y)

def p2move_down(self):
global P2_y
P2_y -= 20
self.bat_specs_p2()

print(P2_x, P2_y)
</code></pre>
<h1>main.py</h1>
<pre><code>from turtle import Screen
import time
from bat import Bat

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Pong Game")

screen.tracer(0)

bat = Bat()

bat.bat_specs_p2()

screen.listen()
screen.onkey(bat.p2move_up, "Up")
screen.onkey(bat.p2move_down, "Down")


screen.update()

screen.exitonclick()
</code></pre>
 

Latest posts

A
Replies
0
Views
1
Ashrik Ahamed
A
A
Replies
0
Views
1
Ashrik Ahamed
A

Online statistics

Members online
1
Guests online
4
Total visitors
5
Top