Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pong game
#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


Messages In This Thread
Pong game - by buss0140 - Dec-22-2020, 02:19 AM
RE: Pong game - by bowlofred - Dec-22-2020, 03:57 AM
RE: Pong game - by buss0140 - Dec-22-2020, 07:28 AM
RE: Pong game - by buss0140 - Dec-22-2020, 07:44 AM
RE: Pong game - by buss0140 - Dec-26-2020, 05:51 PM
RE: Pong game - by MK_CodingSpace - Dec-26-2020, 10:24 PM
RE: Pong game - by buss0140 - Dec-27-2020, 02:00 AM
RE: Pong game - by ndc85430 - Dec-27-2020, 07:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to send a pong on websocket-client tomtom 0 4,029 Aug-15-2022, 05:58 AM
Last Post: tomtom
  Problem with my pong game code Than999 8 4,196 May-15-2022, 06:40 AM
Last Post: deanhystad
  scoring issues in pong wildbill 1 2,326 Aug-05-2019, 01:48 AM
Last Post: metulburr
  PING PONG GAME akea 0 5,796 May-08-2019, 04:30 PM
Last Post: akea
  Python project "pong" without any makefile sylas 5 5,149 Nov-28-2017, 05:55 PM
Last Post: Larz60+
  ping and pong run both well sylas 1 3,278 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