Dec-22-2020, 02:19 AM
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()
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()