Python类语法错误
我一直在遵循一个指南来制作我的游戏。然而,当我试图运行该程序时,我得到一个类循环的语法错误,不知道为什么?我想不出原因,而且我确信在课程圈结束后会有更多的问题,但无法解决Python类语法错误,python,class,pygame,Python,Class,Pygame,我一直在遵循一个指南来制作我的游戏。然而,当我试图运行该程序时,我得到一个类循环的语法错误,不知道为什么?我想不出原因,而且我确信在课程圈结束后会有更多的问题,但无法解决 import pygame from pygame.locals import * from uagame import Window import time import random def main(): window = Window('Pong', 500, 400) window.set_au
import pygame
from pygame.locals import *
from uagame import Window
import time
import random
def main():
window = Window('Pong', 500, 400)
window.set_auto_update(False)
game = Game(window)
game.play()
window.close()
class Rect:
def __init__(self, center, radius, color, window):
self.center = center
self.radius = radius
self.color = color
self.window = window
def draw(self):
pygame.draw.rect(self.window.get_surface(), self.color, Rect((100, 200), (20, 30))
class Circle: # Syntax Error: class Circle:: <string>, line 28, pos 5
def __init__(self, center, radius, color, window):
self.center = center
self.radius = radius
self.color = color
self.window = window
def draw(self):
pygame.draw.circle(self.window.get_surface(), self.color, self.center,self.radius)
def get_color(self):
return self.color
def move(self):
window_size = (self.window.get_width() , self.window.get_height())
for index in range(0,2):
self.center[index] = (self.center[index] + 1) % window_size[index]
def enlarge(self,increment):
self.radius = self.radius + increment
class Game:
def __init__(self, window):
self.window = window
self.bg_color = pygame.Color('black')
self.fg_color = 'green'
self.rect_color = 'green'
self.pause_time = 0.02
self.close_clicked = False
self.continue_game = True
self.circle = Ball([200,200], 20, pygame.Color(self.fg_color),window)
self.rect = Rect([100,200], 50 , pygame.Color(self.fg_color),window)
self.radius_increment = 5
def play(self):
while not self.close_clicked:
self.handle_event()
self.draw()
if self.continue_game:
self.update()
self.decide_continue()
time.sleep(self.pause_time)
def handle_event(self):
event = pygame.event.poll()
if event.type == QUIT:
self.close_clicked = True
elif event.type == MOUSEBUTTONUP and self.continue_game:
self.handle_mouse_up()
def handle_mouse_up(self):
self.circle.enlarge(self.radius_increment)
def draw(self):
self.window.clear()
self.circle.draw()
self.rect.draw()
if not self.continue_game:
self.window.set_bg_color(self.fg_color)
self.window.clear()
self.window.update()
def update(self):
self.circle.move()
def decide_continue(self):
pass
main()
导入pygame
从pygame.locals导入*
从uagame导入窗口
导入时间
随机输入
def main():
窗口=窗口('Pong',500,400)
设置自动更新(错误)
游戏=游戏(窗口)
游戏
window.close()
类矩形:
定义初始(自身、中心、半径、颜色、窗口):
self.center=中心
自半径=半径
self.color=颜色
self.window=window
def牵引(自):
pygame.draw.rect(self.window.get_surface(),self.color,rect((100200),(20,30))
类圈:#语法错误:类圈::,第28行,位置5
定义初始(自身、中心、半径、颜色、窗口):
self.center=中心
自半径=半径
self.color=颜色
self.window=window
def牵引(自):
pygame.draw.circle(self.window.get_surface()、self.color、self.center、self.radius)
def get_颜色(自身):
返回自我颜色
def移动(自我):
window\u size=(self.window.get\u width(),self.window.get\u height())
对于范围(0,2)内的索引:
self.center[index]=(self.center[index]+1)%window_size[index]
def放大(自、增量):
自半径=自半径+增量
班级游戏:
定义初始化(自,窗口):
self.window=window
self.bg_color=pygame.color('black'))
self.fg_color='绿色'
self.rect_color='green'
self.pause\u time=0.02
self.close\u clicked=False
self.continue\u game=True
self.circle=Ball([200200],20,pygame.Color(self.fg_Color),窗口)
self.rect=rect([100200],50,pygame.Color(self.fg_Color),窗口)
自半径_增量=5
def播放(自我):
当未单击self.close时,单击:
self.handle_事件()
self.draw()
如果self.continue\u游戏:
self.update()
self.decision_continue()
时间。睡眠(自我暂停时间)
def句柄_事件(自身):
event=pygame.event.poll()
如果event.type==退出:
self.close\u clicked=True
elif event.type==MOUSEBUTTONUP和self.continue\u游戏:
self.handle\u mouse\u up()
def手柄\鼠标\向上(自身):
自圆放大(自半径_增量)
def牵引(自):
self.window.clear()
self.circle.draw()
self.rect.draw()
如果不是self.continue\u游戏:
self.window.set\u bg\u颜色(self.fg\u颜色)
self.window.clear()
self.window.update()
def更新(自我):
self.circle.move()
def decision_continue(self):
通过
main()
您的错误实际上就在上面这一行。请注意,您的代码缺少一个圆括号来关闭rect()函数。请始终记住在这样的长函数上计算圆括号
def draw(self):
pygame.draw.rect(self.window.get_surface(), self.color, Rect((100, 200), (20, 30))) #<-
def绘制(自):
pygame.draw.rect(self.window.get_surface(),self.color,rect((100200),(20,30))#发布错误消息