弎叁 发表于 2020-5-31 11:31:50

invalid syntax错误怎么办?

为什么这代码运行会显示错误呢?都是按照对方给出的代码和格式写的

import pygame, sys

pygame.init()
size = width, height = 600, 400
speed =
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption('壁球游戏')
ball = pygame.image.load('壁球.jpg')
ballrect = ball.get_rect()
fps = 1000
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color('balck')

def RGBChannel(a):
    return 0 if a<0 else (255 if a>255 else int(a))

while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            sys.exit()
      elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed = speed if speed == 0 else (abs(speed)-
                1)*int(speed/abs(speed))
            elif event.key == pygame.K_RIGHT:
                speed = speed + 1 if speed > 0 else speed -1
            elif event.key == pygame.K_UP:
                speed = speed + 1 if speed > 0 else speed -1
            elif event.key == pygame.K_DOWN:
                speed = speed if speed == 0 else (abs(speed) -
                1)*int(speed/abs(speed))
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
      elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                still = True
      elif event.type == pygame.MOUSEBUTTONUP:
            still = False
            if event.button == 1:
                ballrect = ballrect.move(event.pos - ballrect.left,
                event.pos - ballrect.top)
      elif event.type == pygame.MOUSEMOTION:
            if event.buttons == 1:
                ballrect = ballrect.move(event.pos - ballrect.left,
                event.pos - ballrect.top)
               
    if pygame.display.get_active() and not still:
      ballrect = ballrect.move(speed, speed)
    if ballrect.left < 0 or ballrect.right > width:
      speed = -speed
      if ballrect.right > width and ballrect.right + speed > ballrect.right:
            speed = - speed
    if ballrect.top < 0 or ballrect.bottom > height:
      speed = -speed
      if ballrect.bottom > height and ballrect.bottom + speed > ballrect.bottom:
            speed = - speed

    bgcolor.r = RGBChannel(ballrect.feft*255/width)
    bgcolor.g = RGBChannel(ballrect.top*255/height)
    bgcolor.b = RGBChannel(min(speed, speed)*255/max(speed,speed)

    screen.fill(bgcolor)
    screen.blit(ball, ballrect)
    pygame.display.update()
    fclock.tick(fps)


Twilight6 发表于 2020-5-31 11:33:54

    bgcolor.b = RGBChannel(min(speed, speed) * 255 / max(speed, speed))
这行少了个括号

qiuyouzhi 发表于 2020-5-31 11:34:01

上面一行少好多括号

xiaosi4081 发表于 2020-5-31 11:34:35




import pygame, sys

pygame.init()
size = width, height = 600, 400
speed =
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption('壁球游戏')
ball = pygame.image.load('壁球.jpg')
ballrect = ball.get_rect()
fps = 1000
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color('balck')

def RGBChannel(a):
    return 0 if a<0 else (255 if a>255 else int(a))

while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            sys.exit()
      elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed = speed if speed == 0 else (abs(speed)-
                1)*int(speed/abs(speed))
            elif event.key == pygame.K_RIGHT:
                speed = speed + 1 if speed > 0 else speed -1
            elif event.key == pygame.K_UP:
                speed = speed + 1 if speed > 0 else speed -1
            elif event.key == pygame.K_DOWN:
                speed = speed if speed == 0 else (abs(speed) -
                1)*int(speed/abs(speed))
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
      elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                still = True
      elif event.type == pygame.MOUSEBUTTONUP:
            still = False
            if event.button == 1:
                ballrect = ballrect.move(event.pos - ballrect.left,
                event.pos - ballrect.top)
      elif event.type == pygame.MOUSEMOTION:
            if event.buttons == 1:
                ballrect = ballrect.move(event.pos - ballrect.left,
                event.pos - ballrect.top)
               
    if pygame.display.get_active() and not still:
      ballrect = ballrect.move(speed, speed)
    if ballrect.left < 0 or ballrect.right > width:
      speed = -speed
      if ballrect.right > width and ballrect.right + speed > ballrect.right:
            speed = - speed
    if ballrect.top < 0 or ballrect.bottom > height:
      speed = -speed
      if ballrect.bottom > height and ballrect.bottom + speed > ballrect.bottom:
            speed = - speed

    bgcolor.r = RGBChannel(ballrect.feft*255/width)
    bgcolor.g = RGBChannel(ballrect.top*255/height)
    bgcolor.b = RGBChannel(min(speed, speed)*255/max(speed,speed))

    screen.fill(bgcolor)
    screen.blit(ball, ballrect)
    pygame.display.update()
    fclock.tick(fps)

弎叁 发表于 2020-5-31 12:46:28

xiaosi4081 发表于 2020-5-31 11:34


错了几个单词,为什么复制你的就能用,我自己的把错的单词改回来还是不行{:9_220:}

Twilight6 发表于 2020-5-31 23:06:24

弎叁 发表于 2020-5-31 12:46
错了几个单词,为什么复制你的就能用,我自己的把错的单词改回来还是不行

你发下你的代码看看

弎叁 发表于 2020-6-1 10:57:32

Twilight6 发表于 2020-5-31 23:06
你发下你的代码看看

昨天用不了,今天又能用了,奇怪。

Twilight6 发表于 2020-6-1 10:58:23

弎叁 发表于 2020-6-1 10:57
昨天用不了,今天又能用了,奇怪。

发代码 直接帮你改

弎叁 发表于 2020-6-1 11:00:27

本帖最后由 弎叁 于 2020-6-1 11:02 编辑

Twilight6 发表于 2020-6-1 10:58
发代码 直接帮你改

改好了,谢谢,这是改好的。{:9_import pygame, sys

import pygame, sys

pygame.init()
size = width, height = 600, 400
speed =
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption('壁球游戏')
ball = pygame.image.load('壁球.jpg')
ballrect = ball.get_rect()
fps = 1000
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color('black')

def RGBChannel(a):
    return 0 if a<0 else (255 if a>255 else int(a))

while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            sys.exit()
      elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed = speed if speed == 0 else (abs(speed)-
                1)*int(speed/abs(speed))
            elif event.key == pygame.K_RIGHT:
                speed = speed + 1 if speed > 0 else speed -1
            elif event.key == pygame.K_UP:
                speed = speed + 1 if speed > 0 else speed -1
            elif event.key == pygame.K_DOWN:
                speed = speed if speed == 0 else (abs(speed) -
                1)*int(speed/abs(speed))
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
      elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                still = True
      elif event.type == pygame.MOUSEBUTTONUP:
            still = False
            if event.button == 1:
                ballrect = ballrect.move(event.pos - ballrect.left,
                event.pos - ballrect.top)
      elif event.type == pygame.MOUSEMOTION:
            if event.buttons == 1:
                ballrect = ballrect.move(event.pos - ballrect.left,
                event.pos - ballrect.top)
               
    if pygame.display.get_active() and not still:
      ballrect = ballrect.move(speed, speed)
    if ballrect.left < 0 or ballrect.right > width:
      speed = -speed
      if ballrect.right > width and ballrect.right + speed > ballrect.right:
            speed = - speed
    if ballrect.top < 0 or ballrect.bottom > height:
      speed = -speed
      if ballrect.bottom > height and ballrect.bottom + speed > ballrect.bottom:
            speed = - speed

    bgcolor.r = RGBChannel(ballrect.left*255/width)
    bgcolor.g = RGBChannel(ballrect.top*255/height)
    bgcolor.b = RGBChannel(min(speed, speed)*255/max(speed,speed))

    screen.fill(bgcolor)
    screen.blit(ball, ballrect)
    pygame.display.update()
    fclock.tick(fps)

xiaosi4081 发表于 2020-6-1 11:04:49

弎叁 发表于 2020-6-1 11:00
改好了,谢谢,这是改好的。{:9_import pygame, sys

import pygame, sys


忘了加了

Twilight6 发表于 2020-6-1 11:05:07

弎叁 发表于 2020-6-1 11:00
改好了,谢谢,这是改好的。{:9_import pygame, sys

import pygame, sys


没问题呀 代码 我看半天{:10_250:}

弎叁 发表于 2020-6-1 11:06:17

Twilight6 发表于 2020-6-1 11:05
没问题呀 代码 我看半天

发代码多了个2,点了编辑,又变成文字了{:9_220:}
页: [1]
查看完整版本: invalid syntax错误怎么办?