phy_force.py

Created by antarctus

Created on February 25, 2022

1.28 KB

Visualiser des lignes de champs de forces (gravité, etc…) sur un objet.
Utiliser Ok pour opposer la gravité de l’objet


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

px,py=0,0
sens=1
#===/\
#set it to 1 or -1
#and test 0, -2, 3, 2.5...
space=32
#===/\
#set also it to 20 or 40 to test
color=(128+(127*sens),0,128-(127*sens))


try:
  draw_line(0,0,0,0,(0,0,0))
except:
  def draw_line(x1,y1,x2,y2,c):
    width=x2-x1
    height=y2-y1
    if abs(width)>=abs(height):
      div=height/width
      for i in range(0,width,(width>0)*2-1):
        set_pixel(x1+i,y1+int(div*i),c)
    else:
      div=width/height
      for i in range(0,height,(height>0)*2-1):
        set_pixel(x1+int(div*i),y1+i,c)


def dist(x,y):
  return ((x-px)**2+(y-py)**2)**0.5

def draw_all():
  for x in range(space//2,330,space):
    for y in range(space//2,230,space):
      d=int(dist(x,y))
      try:
        i_d=sens/d**2*1000
      except:
        i_d=0

      fill_rect(x-1,y-1,3,3,(180,)*3)
      if i_d:
        draw_line(x,y,x+int((px-x)*i_d),y+int((py-y)*i_d),(180,)*3)
  fill_rect(px-3,py-3,7,7,color)


while True:
  for i in range(4):
    if keydown(i):
      fill_rect(0,0,320,222,(255,)*3)
      px+=[-1,0,0,1][i]*5
      py+=[0,-1,1,0][i]*5
      px%=320
      py%=220
  if keydown(KEY_OK):
    fill_rect(0,0,320,222,(255,)*3)
    sens*=-1
    color=(128+(127*sens),0,128-(127*sens))
  draw_all()
  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.