Dec-26-2020, 05:51 PM
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:
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]
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]