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)
bgcolor.b = RGBChannel(min(speed, speed) * 255 / max(speed, speed))
这行少了个括号 上面一行少好多括号
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)
xiaosi4081 发表于 2020-5-31 11:34
错了几个单词,为什么复制你的就能用,我自己的把错的单词改回来还是不行{:9_220:} 弎叁 发表于 2020-5-31 12:46
错了几个单词,为什么复制你的就能用,我自己的把错的单词改回来还是不行
你发下你的代码看看 Twilight6 发表于 2020-5-31 23:06
你发下你的代码看看
昨天用不了,今天又能用了,奇怪。 弎叁 发表于 2020-6-1 10:57
昨天用不了,今天又能用了,奇怪。
发代码 直接帮你改 本帖最后由 弎叁 于 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)
弎叁 发表于 2020-6-1 11:00
改好了,谢谢,这是改好的。{:9_import pygame, sys
import pygame, sys
忘了加了 弎叁 发表于 2020-6-1 11:00
改好了,谢谢,这是改好的。{:9_import pygame, sys
import pygame, sys
没问题呀 代码 我看半天{:10_250:} Twilight6 发表于 2020-6-1 11:05
没问题呀 代码 我看半天
发代码多了个2,点了编辑,又变成文字了{:9_220:}
页:
[1]