Python: Why am I getting an invalid syntax error when calling the ppBall class? [closed]

StackOverflow https://stackoverflow.com/questions/23247597

  •  08-07-2023
  •  | 
  •  

Question

General Info

I'm trying to make a remake of Pong using pygame. When I try to run the program (I only have the bouncing ball part for now), I get an invalid syntax error.

The Error

Here's my error:

C:\Programming\PyGame\Pong>python PyngPongEmbeddedClasses.py
  File "PyngPongEmbeddedClasses.py", line 87
    pyngBall = ppBall((100,100), BALLSIZE, 0, 1)
           ^
SyntaxError: invalid syntax

My Code

#PyngPong by Ben Lippincott
#This game is not affiliated with the game Pong in any way

import pygame, sys, math
from pygame.locals import *

# Globals!
FPS = 30
ANIMATIONSPEED = 1   # pixels per frame
WINDOWHEIGHT   = 600 # pixels
WINDOWWIDTH    = 800 # pixels
VERSION        = 'v1.0'
BASICFONTSIZE  = 20 # 20 pt font

# screencenter tuple
SCREENCENTERX  = WINDOWWIDTH / 2
SCREENCENTERY  = WINDOWHEIGHT / 2
SCREENCENTER   = (SCREENCENTERX, SCREENCENTERY)

BALLSIZE       = 15 # radius of circle
BGCOLOR        = BLACK

# Color Globals
#         R    G    B
WHITE = (255, 255, 255)
BLACK = (  0,   0,   0)


def addVectors((angle1, length1), (angle2, length2)):
    x  = math.sin(angle1) * length1 + math.sin(angle2) * length2
    y  = math.cos(angle1) * length1 + math.cos(angle2) * length2

    angle = 0.5 * math.pi - math.atan2(y, x)
    length  = math.hypot(x, y)

    return (angle, length)

class ppBall:
    def __init__(self, (x, y), size, angle, speed):
        self.x = x
        self.y = y
        self.size = size
        self.color = (255, 255, 255)
        self.thickness = 0
        self.speed = angle
        self.angle = speed


    def draw(self):    
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.size, self.thickness)


    def move(self):
        (self.angle, self.speed) = addVectors((self.angle, self.speed))
        self.x += math.sin(self.angle) * self.speed
        self.y -= math.cos(self.angle) * self.speed

    def bounce(self):
        if self.x > width - self.size:
            self.x = 2*(width - self.size) - self.x
            self.angle = - self.angle

        elif self.x < self.size:
            self.x = 2*self.size - self.x
            self.angle = - self.angle

        if self.y > height - self.size:
            self.y = 2*(height - self.size) - self.y
            self.angle = math.pi - self.angle

        elif self.y < self.size:
            self.y = 2*self.size - self.y
            self.angle = math.pi - self.angle


def main():
    global FPSCLOCK, DISPLAYSURF, BASICFONT

    # Initialization time!
    pygame.init()
    FPSCLOCK = pygame.time.clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    pygame.display.set_caption('Pyng Pong by PhatLipp ' + VERSION)
    BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE

    pyngBall = ppBall((100,100), BALLSIZE, 0, 1)

    while True: # Main game loop!
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        DISPLAYSURF.fill(BGCOLOR)
        pyngBall.move()
        pyngBall.bounce()
        pyngBall.draw()

        pygame.display.flip()

if __name__ == "__main__":
    main()

Here's my call to the class:

pyngBall = ppBall((100,100), BALLSIZE, 0, 1)

Why is it doing this?

Was it helpful?

Solution

You are missing a closing parenthesis on the line above:

BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE
#                                                       here--^

Whenever you get a strange syntax error like this, you should always check the preceding line. More often than not, the problem is a simple missing bracket.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top