让我们考虑以下代码(中心和速度随机的球碰撞,屏幕表面边界相互碰撞):import pygame,sys,mathfrom pygame.locals import *from random import randrangeWIDTH = 500HEIGHT = 500WHITE = (255,255,255)BLUE = (0, 0, 255)RADIUS = 10FPS = 30DISPLAYSURF = pygame.display.set_mode((WIDTH,HEIGHT),0,32)TAB = []fpsClock = pygame.time.Clock()pygame.display.set_caption('Animation')class Point: def __init__(self, x, y): self.x = x self.y = y def distance(self, A): return math.sqrt((A.x-self.x)**2 + (A.y-self.y)**2) def getTouple(self): return (self.x,self.y)class Vector: def __init__(self, x, y): self.x = x self.y = y def norm(self): return math.sqrt(self.x**2 + self.y**2)class Ball: def __init__(self, center, radius, velocity): self.center = center self.radius = radius self.velocity = velocity def __init__(self): self.radius = RADIUS self.center = Point(randrange(RADIUS,WIDTH-RADIUS), randrange(RADIUS,HEIGHT-RADIUS)) vx = randrange(-5,5) vy = randrange(-5,5) while vx == 0 or vy == 0: vx = randrange(-5,5) vy = randrange(-5,5) self.velocity = Vector(vx,vy)这段代码有效但不正确:有些球被边界“抓住”而其他球“粘在一起”。我认为问题出在Ball课堂方法上draw。对于如何改进该代码的任何想法,我将不胜感激。
添加回答
举报
0/150
提交
取消