gpt4 book ai didi

python - Python/pygame 中的语法错误

转载 作者:行者123 更新时间:2023-12-01 05:38:47 26 4
gpt4 key购买 nike

我正在学习 python/pygame,并且正在做物理教程。不幸的是,我认为它是用旧版本的 python 制作的。我完全复制了视频中的代码,但是当我运行它时,它返回“无法运行脚本 - 语法错误 - 无效语法(physical.py,第 7 行)”。我确信这只是我错过的一些愚蠢而明显的事情,但任何答案都会对我大有帮助!

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

screen_size = screen_width, screen_height = 600, 400

class MyCircle:
def __init__(self, (x, y), size, color = (255,255,255), width = 1):
self.x = x
self.y = y
self.size = size
self.color = color
self.width = width

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

screen = pygame.display.set_mode(screen_size)

my_circle = MyCircle((100,100), 10, red)
my_circle_2 = MyCircle((200,200), 30, blue)
my_circle_3 = MyCircle((300,150), 40, green, 4)
my_circle_4 = MyCircle((450,250), 120, black, 0)

fps_limit = 60
run_me = True
while run_me:
clock.tick(fps_limit)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run_me = False

my_circle.display()
my_circle_2.display()
my_circle_3.display()
my_circle_4.display()

pygame.display.flip()

pygame.quit()
sys.exit()

最佳答案

您可能使用的是 Python 3。元组参数解包已在 3.X 中删除,因此您必须更改此设置:

def __init__(self, (x, y), size, color = (255,255,255), width = 1):
self.x = x
self.y = y
self.size = size
self.color = color
self.width = width

致:

def __init__(self, position, size, color = (255,255,255), width = 1):
self.x, self.y = position
self.size = size
self.color = color
self.width = width

关于python - Python/pygame 中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18200249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com