gpt4 book ai didi

Python 类语法错误

转载 作者:行者123 更新时间:2023-12-01 02:06:58 24 4
gpt4 key购买 nike

我一直在遵循指南来制作我的游戏。但是,当我尝试运行该程序时,我收到了 Class Circle 的语法错误,不知道为什么?我无法弄清楚原因,并且我确信类(class)圈之后还有更多问题,但无法修复。

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

最佳答案

您的错误实际上在于上面的行。请注意您的代码缺少关闭 rect() 函数的括号。永远记住要计算像这样的长函数的括号数量。

def draw(self):
pygame.draw.rect(self.window.get_surface(), self.color, Rect((100, 200), (20, 30))) #<-

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

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