gravity.py

Created by antarctus

Created on July 03, 2022

1.23 KB

Simulateur de gravitée, qui n’aboutit qu’à des bretzels !


from math import *
from kandinsky import *
from ion import *
from time import *


BACK=(0,)*3

def dist(x1,y1,x2,y2):
  return sqrt((x1-x2)**2+(y1-y2)**2)

def gravity(a1,a2):
  return (a1.w*a2.w)/((a1.x-a2.x)**2+(a1.y-a2.y)**2)

class Pv():
  def __init__(self,x,y):
    self.x=x
    self.y=y

pv=Pv(0,0)

class Astro():
  def __init__(self,w,x,y,vx=0,vy=0,c=(255,0,0)):
    global astres
    astres+=[self,]
    self.w=w
    self.x=x
    self.y=y
    self.vx=vx
    self.vy=vy
    self.c=c
    self.c2=(c[0]//2,c[1]//2,c[2]//2)
    self.width=3

  def draw(self):
    fill_rect(int(self.x-pv.x)-self.width+160,int(self.y-pv.y)-self.width+111,self.width*2+1,self.width*2+1,self.c)

  def undraw(self):
    fill_rect(int(self.x-pv.x)-self.width+160,int(self.y-pv.y)-self.width+111,self.width*2+1,self.width*2+1,self.c2)

astres=[]

fill_rect(0,0,320,222,BACK)

#Astro(1,0,0,5)
Astro(10,0,30,0,0,(0,0,255))
Astro(-1,0,60,-5,0,(0,255,0))
pv=astres[0]

while not keydown(KEY_EXE):

  for i in astres:
    i.undraw()
    for ii in astres:
      if ii==i:
        continue
      i.vx+=(ii.x-i.x)*gravity(i,ii)/i.w
      i.vy+=(ii.y-i.y)*gravity(i,ii)/i.w

  for i in astres:
    i.x+=i.vx
    i.y+=i.vy

  for i in astres:
    i.draw()

#  sleep(0.05)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.