我正在创建一个boid植绒模拟器并遇到一些错误。怀疑他们是由错误使用ABC发出的。但我无法修复错误,已经尝试了一天。
主要问题在Check_input类中。传单在某种程度上是未定义的,如果输入已注册,我试图将它们附加到FlyerList。
链接整个代码以获得完整的视角。
from precode import Vector2D, intersect_rectangle_circle, intersect_circles
from abc import ABC, abstractmethod
from config import *
import pygame
class Flyer(ABC):
@abstractmethod
def draw(self):
pass
@abstractmethod
def move(self):
pass
class Boids(Flyer):
def __init__(self):
self.radius = 7
self.pos = Vector2D(100,100)
self.speed = Vector2D(1,1)
pass
def draw(self,screen):
pygame.draw.circle(screen, black, (int(self.pos.x), int(self.pos.y)),
self.radius)
pass
def move(self):
self.pos += self.speed
if self.pos.x + self.radius >= screen_res[0]:
self.speed.x *= -1
if self.pos.x - self.radius <= 0:
self.speed.x = self.speed.x * -1
if self.pos.y - self.radius >= 0:
self.speed.y *= -1
if self.pos.y + self.radius < screen_res[1]:
self.speed.y *= -1
pass
class Hoiks(Flyer):
def __init__(self):
self.radius = 9
self.pos = Vector2D(200,200)
self.speed = Vector2D(8,8)
def draw(self,screen):
pygame.draw.circle(screen, red, (int(self.pos.x), int(self.pos.y)),
self.radius)
pass
def move(self):
self.pos += self.speed
if self.pos.x + self.radius >= screen_res[0]:
self.speed.x *= -1
if self.pos.x - self.radius <= 0:
self.speed.x *= -1
if self.pos.y - self.radius >= 0:
self.speed.y *= -1
if self.pos.y + self.radius < screen_res[1]:
self.speed.y *= -1
class FlyerList():
flyers = []
def move_all(self):
for flyer in self.flyers:
flyer.move()
def draw_all(self):
for flyer in self.flyers:
flyer.draw()
class Check_input():
def boidspawn(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
flyers.append(Boids)
def hoikspawn(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
flyers.append(Hoiks)
class Game:
def __init__(self):
pygame.init()
self.flyer_list = FlyerList()
self.check_input = Check_input()
def Gameloop(self,event):
while 1:
self.flyer_list.move_all()
self.flyer_list.draw_all()
self.check_input.boidspawn(event)
self.check_input.hoikspawn(event)
# self.check_collision()
def game_code():
# pygame.init()
game = Game()
boid = Boids()
hoik = Hoiks()
bats = FlyerList
check = Check_input()
screen = pygame.display.set_mode(screen_res)
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and
event.key == pygame.K_ESCAPE:
exit()
pygame.draw.rect(screen, (255,255,255), (0, 0, screen.get_width(),
screen.get_height()))
time_passed = clock.tick(30) # limit to 30FPS
time_passed_seconds = time_passed / 1000.0 # convert to seconds
x, y = pygame.mouse.get_pos()
game.Gameloop(event)
pygame.display.update()
if __name__ == '__main__':
game_code()