Lukas528491
Lukas528491

Reputation: 1

Particle in a Box not moving

I'm currently trying to create a 2d Box with one particle inside, which moves with a certain velocity and "bounces off" at the walls. I'm using python and Matplotlib. Everytime I try to execute my code in Jupyter Notebook, it prints the 2d box with the particle inside, but it is not moving in any direction.

import numpy as np
import matplotlib.pyplot as plt
import random
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
ax.axis([-1, 1, -1, 1])
ax.set_aspect('equal')
point,= ax.plot(0,1, marker = 'o')

class particle:
    def __init__(self):
        self.x = random.uniform(-1, 1)
        self.y = random.uniform(-1, 1)
        self.vx = np.random.random_sample()/5
        self.vy = np.random.random_sample()/5

    def movement(self):
        if self.x < -1 or self.x >= 1:
            self.vx *= -1
        if self.y < -1 or self.y >= 1:
            self.vy *= -1
        self.x += self.vx
        self.y +=self.vy

p = particle()
def update(frame):
    p.movement()
    
    point.set_data([p.x], [p.y])
    return point,


anim = FuncAnimation(fig, update, frames = 100, interval = 5, blit = True)
plt.show

Upvotes: 0

Views: 69

Answers (0)

Related Questions