Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pong game
#1
I am trying to build a pong game for practice and I am haviing trouble with my LeftPaddle and RightPaddle methods. I am gettting this error when I run the code:
Traceback (most recent call last):
File "main.py", line 292, in <module>
Game()
File "main.py", line 256, in __init__
self.left_paddle = LeftPaddle()
File "main.py", line 242, in __init__
Paddle.__init__(self, x, y)
File "main.py", line 234, in __init__
turtle.Turtle.__init__(self)
File "/usr/lib/python3.8/turtle.py", line 3813, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "/usr/lib/python3.8/turtle.py", line 2541, in __init__
TNavigator.__init__(self, screen.mode())
File "/usr/lib/python3.8/turtle.py", line 1523, in __init__
self._angleOffset = self.DEFAULT_ANGLEOFFSET
AttributeError: 'LeftPaddle' object has no attribute 'DEFAULT_ANGLEOFFSET'

I am not sure hoow to fix the issue here. I am by no means done, but I am stuck here. Here is my code so far:

import turtle
import random
import math
class Ball(turtle.Turtle):
def __init__(self, x, y, x_vel, y_vel):
turtle.Turtle.__init__(self)
self.x = x
self.y = y
self.vx = x_vel
self.vy = y_vel
self.penup()
self.speed(0)
self.setpos(x, y)

class Paddle:
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.x = x
self.y = y
self.penup()
self.setpos(x, y)

class LeftPaddle(Paddle):
def __init__(self, x, y):
Paddle.__init__(self, x, y)

class RightPaddle(Paddle):
def __init__(self, x, y):
Paddle.__init__(self, x, y)

class Game:
def __init__(self):
turtle.setworldcoordinates(0, 0, 1000, 1000)
turtle.delay(0)
self.ball = Ball(random.uniform(50, 950), random.uniform(50, 950), random.choice([-5,5]), random.uniform(5, -5))
self.ball.turtlesize(1)
self.ball.shape('circle')
self.ball.color('blue')
self.left_paddle = LeftPaddle(5, 500)
self.left_paddle.turtlesize(1)
self.left_paddle.color('black')
self.left_paddle.shape("square")
self.left_paddle.shapesize(stretch_wid=6, stretch_len=2)
self.score = 0
self.gameloop()
# turtle.onkeypress(self.player.thrust, 'Up')
turtle.listen()
turtle.mainloop()

def move(self):
self.ball.setx(self.ball.xcor()+self.ball.vx)
self.ball.sety(self.ball.ycor()+self.ball.vy)
if self.ball.xcor() <= 0:
self.ball.setx(0)
self.ball.vx *= -1

if self.ball.xcor() >= 1000:
self.ball.setx(1000)
self.ball.vx *= -1

if self.ball.ycor() <= 0:
self.ball.sety(0)
self.ball.vy *= -1

if self.ball.ycor() >= 1000:
self.ball.sety(1000)
self.ball.vy *= -1

# self.ball.setpos(new_x, new_y)

def gameloop(self):
self.move()
turtle.Screen().ontimer(self.gameloop, 30)

Game()
Reply
#2
That's an odd error. That value should be set within the turtle library. Do you have the ability to try another python distribution? It looks more like a problem with the turtle library than with your code...
Reply
#3
(Dec-22-2020, 03:57 AM)bowlofred Wrote: That's an odd error. That value should be set within the turtle library. Do you have the ability to try another python distribution? It looks more like a problem with the turtle library than with your code...

I have been using repl.it but I have tried on IDLE using distro 3.8.5 and 3.9.1 with the same result.
Reply
#4
(Dec-22-2020, 07:28 AM)buss0140 Wrote:
(Dec-22-2020, 03:57 AM)bowlofred Wrote: That's an odd error. That value should be set within the turtle library. Do you have the ability to try another python distribution? It looks more like a problem with the turtle library than with your code...

I have been using repl.it but I have tried on IDLE using distro 3.8.5 and 3.9.1 with the same result.

I switched it to this now:
import turtle
import random
import math
class Ball(turtle.Turtle):
def __init__(self, x, y, x_vel, y_vel):
turtle.Turtle.__init__(self)
self.x = x
self.y = y
self.vx = x_vel
self.vy = y_vel
self.penup()
self.speed(0)
self.setpos(x, y)

class Paddle:
def __init__(self, x, y):
Ball.__init__(self, x, y)

class LeftPaddle(Paddle):
def __init__(self, x=5, y=500):
Paddle.__init__(self, x, y)

class RightPaddle(Paddle):
def __init__(self, x=995, y=500):
Paddle.__init__(self, x, y)

But I am getting this error now.

Traceback (most recent call last):
File "main.py", line 289, in <module>
Game()
File "main.py", line 253, in __init__
self.left_paddle = LeftPaddle()
File "main.py", line 238, in __init__
Paddle.__init__(self, x, y)
File "main.py", line 234, in __init__
Ball.__init__(self, x, y)
TypeError: __init__() missing 2 required positional arguments: 'x_vel' and 'y_vel'

I am not sure why it wants the other two arguments when I am only inheriting two of the four parameters from class Ball.
Reply
#5
I started over completely and I first wrote the code with all global variables to make sure it works. I am trying to practice doing OOP though, so I put everything into classes and I keep getting this error with the turtle library for some reason. Please help. Here is the code with the error:
import turtle
import random
#import winsound

wn = turtle.Screen()
wn.title("The Classic Ping Pong")
wn.bgcolor("black")
#wn.bgpic("ehbbbb.png")
wn.setup(width=800, height=600)
wn.tracer(0)

# Paddle A
class PaddleA(turtle.Turtle):
    def __init__(self, x, y):
        turtle.Turtle.__init__(self)
        self.x = x
        self.y = y
        self.speed(0)
        self.shape("square")
        self.color("blue")
        self.shapesize(stretch_wid=5,stretch_len=1)
        self.penup()
        self.goto(-350, 0)

    def paddle_a_up(self):
        y = self.ycor()
        y += 20
        self.sety(y)

    def paddle_a_down(self):
        y = self.ycor()
        y -= 20
        self.sety(y)
        
# Paddle B
class PaddleB(turtle.Turtle):
    def __init__(self, x, y):
        turtle.Turtle.__init__(self)
        self.x = x
        self.y = y
        self.speed(0)
        self.shape("square")
        self.color("blue")
        self.shapesize(stretch_wid=5,stretch_len=1)
        self.penup()
        self.goto(x, y)

    def paddle_b_up(self):
        y = self.ycor()
        y += 20
        self.sety(y)

    def paddle_b_down(self):
        y = self.ycor()
        y -= 20
        self.sety(y)
        
# Ball
class Ball(turtle.Turtle):
    def __init__(self, x, y, vx, vy):
        turtle.Turtle.__init__(self)
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.speed(0)
        self.shape("circle")
        self.color("white")
        self.penup()
        self.goto(x, y)    


# Pen
class Pen:
  def __init__(self, x=0, y=260):
    turtle.Turtle.__init__(self)
    self.x = x
    self.y = y
    self.speed(0)
    self.shape("square")
    self.color("white")
    self.penup()
    self.hideturtle()
    self.goto(x, y) 

class Game:
  def __init__(self):
    self.score_a = 0
    self.score_b = 0
    self.ball = Ball(0, 0, random.choice([-5,5]), random.choice([random.uniform(-5, -1), random.uniform(1, 5)]))
    self.paddle_a = PaddleA(-350, 0)
    self.paddle_b = PaddleB(350, 0)
    self.pen = Pen()
    self.pen.write("Player A: 0  Player B: 0", align="center", font=("Courier", 24, "normal"))
# Keyboard bindings
    wn.listen()
    wn.onkeypress(PaddleA.paddle_a_up, "w")
    wn.onkeypress(PaddleA.paddle_a_down, "s")
    wn.onkeypress(PaddleB.paddle_b_up, "Up")
    wn.onkeypress(PaddleB.paddle_b_down, "Down")

# Main game loop
  def gameloop(self):
    while True:
        wn.update()
        
        # Move the ball
        newx=self.ball.xcor() + self.ball.vx
        newy=self.ball.ycor() + self.ball.vy

        self.ball.setx(newx)
        self.ball.sety(newy)

        # Border checking

        # Top and bottom
        if self.ball.ycor() > 290:
            self.ball.sety(290)
            self.ball.dy *= -1
            #winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
        
        elif self.ball.ycor() < -290:
            self.ball.sety(-290)
            self.ball.dy *= -1
            #winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)

        # Left and right
        if self.ball.xcor() > 350:
            self.score_a += 1
            self.pen.clear()
            self.pen.write("Player A: {}  Player B: {}".format(self.score_a, self.score_b), align="center", font=("Courier", 24, "normal"))
            self.ball.goto(0, 0)
            self.ball.vx *= -1

        elif self.ball.xcor() < -350:
            self.score_b += 1
            self.pen.clear()
            self.pen.write("Player A: {}  Player B: {}".format(self.score_a, self.score_b), align="center", font=("Courier", 24, "normal"))
            self.ball.goto(0, 0)
            self.ball.vx *= -1

        # Paddle and ball collisions
        if (-350 < self.ball.xcor() < -340) and self.ball.ycor() < self.paddle_a.ycor() + 50 and self.ball.ycor() > self.paddle_a.ycor() - 50:
            self.ball.vx *= -1 
            #winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
        
        elif (350 > self.ball.xcor() > 340) and self.ball.ycor() < self.paddle_b.ycor() + 50 and self.ball.ycor() > self.paddle_b.ycor() - 50:
            self.ball.vx *= -1
            #winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)

Game()
[error]
Traceback (most recent call last):
File "/Users/brady/CSCI.1133/new_CS_pong.py", line 161, in <module>
Game()
File "/Users/brady/CSCI.1133/new_CS_pong.py", line 103, in __init__
self.pen = Pen()
File "/Users/brady/CSCI.1133/new_CS_pong.py", line 76, in __init__
turtle.Turtle.__init__(self)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 3814, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 2542, in __init__
TNavigator.__init__(self, screen.mode())
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 1524, in __init__
self._angleOffset = self.DEFAULT_ANGLEOFFSET
AttributeError: 'Pen' object has no attribute 'DEFAULT_ANGLEOFFSET'
[/errror]
Reply
#6
Looks like too many things go wrong in the code. If you want to use class, you might want to start with simple practice.
Reply
#7
(Dec-26-2020, 10:24 PM)MK_CodingSpace Wrote: Looks like too many things go wrong in the code. If you want to use class, you might want to start with simple practice.

That is not helpful. Firstly, I am in the middle of coding this game so it is not done. Secondly, my code is not that far off. I have built simple games like this using classes before and have never had this problem. Please tell me one thing wrong with the code that is related to the error I got. If it is not related to the error, I can likely work it out myself after I get this error figured out
Reply
#8
Your traceback doesn't seem to align with the code you've posted - it's referring to line 161 and there are only 151 lines in your post. Please make sure you post the full and correct code.

It also seems like you're misunderstanding classes. Why do you have two classes PaddleA and PaddleB? There's one concept there - a paddle. So, you should have one class and two instances.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to send a pong on websocket-client tomtom 0 3,653 Aug-15-2022, 05:58 AM
Last Post: tomtom
  Problem with my pong game code Than999 8 3,867 May-15-2022, 06:40 AM
Last Post: deanhystad
  scoring issues in pong wildbill 1 2,210 Aug-05-2019, 01:48 AM
Last Post: metulburr
  PING PONG GAME akea 0 5,694 May-08-2019, 04:30 PM
Last Post: akea
  Python project "pong" without any makefile sylas 5 5,006 Nov-28-2017, 05:55 PM
Last Post: Larz60+
  ping and pong run both well sylas 1 3,180 Sep-24-2017, 05:14 PM
Last Post: sylas

Forum Jump:

User Panel Messages

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