大球吃小球 import pygame import random class Color: @classmethod def random_color(cls): red = random.randint(0, 255 ) green = random.randint(0, 255 ) blue = random.randint(0, 255 ) return (red, green, blue) class Ball: def __init__ (self, cx, cy, radius, sx, sy, color): self.cx = cx self.cy = cy self.radius = radius self.sx = sx self.sy = sy self.color = color self.is_alive = True # 球的默认存活状态 # 行为: # 移动 : 在哪里移动 def move(self, window): self.cx += self.sx self.cy += self.sy # 圆心点发生变化 需要做边界判断 # 横向出界 if self.cx - self.radius <= 0 or self.cx + self.radius >= window.get_width(): self.sx = - self.sx # 纵向出界