gravity.py

Created by golem64

Created on May 04, 2023

1.7 KB

Un petit script très simple simulant la gravité sur du texte (imparfait). Touche OK pour lancer les textes aléatoirement, touche Supprimer pour arrêter les vélocités de chaque texte


from kandinsky import *
from time import *
from ion import *
from random import *
bg = (255, 255, 255)
class mass:
  def __init__(self, txt):
    self.txt = txt
    self.vx = 0
    self.vy = 0
    self.x = 0
    self.y = 0
  def collisions(self):
    if(self.vx):
      mx = int(self.vx/abs(self.vx))
      for i in range(0,self.vx,mx):
        self.x += mx
        if(self.x > 320 - 10*len(self.txt) or self.x < 0):
          self.x -= mx
          self.vx = -self.vx
          break
      self.vx -= mx
    if(self.vy):
      my = int(self.vy/abs(self.vy))
      for i in range(0,round(self.vy),my):
        self.y += my
        if(self.y > 222 - 18 or self.y < 0):
          self.y -= my
          self.vy = -self.vy
          break
      self.vy -= my
    '''The following is to define
    where the center of mass is'''
    # down
    if(self.y < 203): self.vy += 2
    # up
#    if(self.y > 1): self.vy -= 2
    # right
#    if(self.x < 319 - 10*len(self.txt)): self.vx += 2
    # left
#    if(self.x > 1): self.vx -= 2
  def refresh(self):
    fill_rect(round(self.x),round(self.y),10 * len(self.txt),18,bg)
    self.collisions()
    draw_string(self.txt,round(self.x),round(self.y))
#    draw_string(str(self.vx),0,200)

fill_rect(0,0,320,222,bg)
#a = mass("A")
#b = mass("B")
#c = mass("C")
#d = mass("D")
#e = mass("E")
#f = mass("F")
lettres = [mass(chr(i)) for i in range(65, 91)]
kd = False
while True:
  if keydown(KEY_OK) and not kd:
    for i in lettres:
      i.vx += randint(-30, 30)
      i.vy += randint(-30, 30)
    kd = True
  elif not keydown(KEY_OK):
    kd = False
  if keydown(KEY_BACKSPACE):
    for i in lettres:
      i.vx = 0
      i.vy = 0
  for i in lettres:
    i.refresh()
#  sleep(0.01)