简体   繁体   English

PyGame没有正确绘制圆圈

[英]PyGame is not drawing circles properly

def collide(p1, p2):
    dx = p1.x - p2.x
    dy = p1.y - p2.y
    dist = math.hypot(dx, dy)

    if dist < p1.size + p2.size:
        for i in range (0,1):
            neutrList.append(Neutron(xxx,yyy))
        for k,neutroon in enumerate(neutrList):
            neutroon.verletUpdate(dt)
            neutroon.bounce()
            neutroon.display()       

screen = pygame.display.set_mode((width, height))
screen.fill(background_colour)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill(background_colour)

    for k, at in enumerate(partList):
        neutron.verletUpdate(dt)
        neutron.bounce()
        collide(at, neutron)
        if not len(neutrList):
            neutron.display()
        at.display()

    pygame.display.flip()
pygame.quit()

This is a part of my program. 这是我程序的一部分。 I wanted to draw green circle moving away from blue circle.Blue circle is static and is fine. 我想画一个远离蓝色圆圈的绿色圆圈。蓝色圆圈是静态的并且很好。 But instead of green circle I get something like this 但是我得到的不是绿色圆圈 在此处输入图片说明 What should I change to make my code working properly? 我应该更改些什么以使我的代码正常工作?

Neutron class 中子级

class Neutron():
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.vx = 2.5
        self.vy = 0
        self.A = 235
        self.Z = 92
        self.size = 20
        self.colour = (0, 255, 0)
        self.thickness = 0

    def verletUpdate(self,dt):
        self.x = self.x + dt * self.vx
        self.y = self.y + dt * self.vy

    def display(self):
       pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)

    def bounce(self):
        if self.x > width - self.size:
            self.x = 2*(width - self.size) - self.x
            self.vx = -self.vx

        elif self.x < self.size:
            self.x = 2*self.size - self.x
            self.vx = -self.vx

        if self.y > height - self.size:
            self.y = 2*(height - self.size) - self.y
            self.vy= -self.vy

        elif self.y < self.size:
            self.y = 2*self.size - self.y
            self.vy = -self.vy

I think that the reason you are seeing the smear is because you are drawing a whole bunch of neutrons. 我认为您看到该涂片的原因是因为您正在吸引一大堆中子。 You are creating a new one when running the collide() function. 您在运行collide()函数时正在创建一个新的。 See this line: 看到这一行:

neutrList.append(Neutron(xxx,yyy))

You are appending a new neutron to your list, and when you go to draw them, you are drawing all of them, each with a slightly different position. 您要在列表中添加一个新的中子,然后绘制它们时,就绘制了所有中子,每个位置略有不同。 You should check this by printing len(neutrlist) every frame and see if it is constantly growing. 您应该通过每帧打印len(neutrlist)来检查它,看看它是否一直在增长。

Also, why is your collide() fuction drawing and creating neutrons? 另外,为什么要使用collide()函数绘制并创建中子? Should it just adjust their position/velocity based on collisions? 是否应该仅根据碰撞来调整其位置/速度? It is not a good design choice to have a collision check function draw and create the objects that are colliding. 具有碰撞检查功能绘制并创建碰撞对象的方法不是一个好的设计选择。 Neutron drawing and creating should happen elsewhere in your program. 中子绘制和创建应该在程序的其他地方进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM