Python:为什么在调用ppBall类时出现无效的语法错误?

时间:2014-04-23 14:36:50

标签: python pygame

一般信息

我正在尝试使用pygame重制Pong。当我尝试运行程序时(我现在只有弹跳球部分),我得到了无效的语法错误。

错误

这是我的错误:

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

我的代码

#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()

这是我对班级的呼吁:

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

为什么要这样做?

1 个答案:

答案 0 :(得分:5)

您在上面一行中缺少右括号:

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

每当你遇到像这样的奇怪语法错误时,你应该总是检查前一行。通常情况下,这个问题很简单。