Entradas

Mostrando entradas de octubre, 2025

PYTHON PURO: M.c.m y m.c.d

def calcular_mcd(a, b): while b!=0: ta=a a=b b=ta%b return abs(a) def calcular_mcm(a, b): return abs(a*b) // calcular_mcd(a, b) num1 = int(input("Primer número: ")) num2 = int(input("Segundo número: ")) mcm = calcular_mcm(num1, num2) Mcd = calcular_mcd(num1, num2) print ( f "mcm( {num1} , {num2} ) = {mcm} \nMcd( {num1} , {num2} ) = {Mcd} " )  

PYTHON: bolas que reboten cuando se chocan: array con objetos

N = 8 pelotas = [] def setup():     size(800, 600)     frameRate(300)          global pelotas     pelotas = []     for i in range(N):         r = random(15, 40)         x = random(r, width - r)         y = random(r, height - r)         dx = random(-2, 2)         dy = random(-2, 2)         pelotas.append(Pelota(x, y, dx, dy, r)) def draw():     background(220)     for pelota in pelotas:         pelota.mover()         pelota.rebotar()         pelota.dibujar()     for i in range(N):         for j in range(i + 1, N):             pelotas[i].choque(pelotas[j]) class Pelota:     def __init__(self, Tx, Ty, Tdx, Tdy, Tr):         self.x = Tx ...

PYTHON: bolas que reboten cuando choquen

x1 = 0 y1 = 0 dx1 = 0 dy1 = 0 x2 = 0 y2 = 0 dx2 = 0 dy2 = 0 r = 30 def setup():     size(600, 400)     frameRate(500)     global x1, y1, dx1, dy1, x2, y2, dx2, dy2          x1 = random(60, 500)     y1 = random(60, 300)     x2 = random(60, 500)     y2 = random(60, 300)     dx1 = random(-1, 1)     dy1 = random(-1, 1)     dx2 = random(-1, 1)     dy2 = random(-1, 1) def draw():     global x1, y1, dx1, dy1, x2, y2, dx2, dy2          background(220)     ellipse(x1, y1, r * 2, r * 2)     ellipse(x2, y2, r * 2, r * 2)     x1 = x1 + dx1     y1 = y1 + dy1     x2 = x2 + dx2     y2 = y2 + dy2     if x1 - r < 0 or x1 + r > width:         dx1 = dx1 * -1     if y1 - r < 0 or y1 + r > height:         dy1 = dy1 * -1 ...

PYTHON: bolas que cuando choquen se pare el programa

x1 = 0 y1 = 0 dx1 = 0 dy1 = 0 x2 = 0 y2 = 0 dx2 = 0 dy2 = 0 r = 30 def setup():     size(600, 400)     frameRate(300)          global x1, y1, x2, y2, dx1, dy1, dx2, dy2          x1 = random(60, 500)     y1 = random(60, 300)     x2 = random(60, 500)     y2 = random(60, 300)          dx1 = random(-1, 1)     dy1 = random(-1, 1)     dx2 = random(-1, 1)     dy2 = random(-1, 1) def draw():     global x1, y1, dx1, dy1, x2, y2, dx2, dy2          background(220)     ellipse(x1, y1, r * 2, r * 2)     ellipse(x2, y2, r * 2, r * 2)          x1 = x1 + dx1     y1 = y1 + dy1     x2 = x2 + dx2     y2 = y2 + dy2          if x1 - r < 0 or x1 + r > width:         dx1 = -1*dx1     if y1...

PYTHON: coches que se chocan por una bola movida por mi

r = False l = False u = False d = False x = 60 y = 60 numCoches = 30 myCars = [] def setup():     size(800, 600)     global myCars     myCars = []          for i in range(numCoches):         c = color(random(255), random(255), random(255))         xpos = random(width - 5)         ypos = random(height - 5)         xspeed = random(-6, 6)                  if xspeed == 0:             xspeed = 1                  myCars.append(Car(c, xpos, ypos, xspeed)) def draw():     background(255)     muevebola()     dibujabola()          for car in myCars:         car.drive()                  if dist(x, y, car.xpos, car.ypos) < 27: ...

PYTHON: coches con array

numCoches = 40 myCars = [] def setup():     size(800, 600)          global myCars     myCars = []          for i in range(numCoches):         c = color(random(255), random(255), random(255))         xpos = random(width - 10)         ypos = random(height - 10)         xspeed = random(-6, 6)                  if xspeed == 0:             xspeed = 1                      myCars.append(Car(c, xpos, ypos, xspeed)) def draw():     background(255)          for car in myCars:         car.drive()         car.display() # Clase Car class Car:     def __init__(self, c, xpos, ypos, xspeed):         self.c = c     ...

PYTHON: coches en vertical

myCar1 = 0 myCar2 = 0 def setup():     size(800, 600)     global myCar1, myCar2     myCar1 = Car(color(255, 0, 0), 500, 400, -5)     myCar2 = Car(color(0, 0, 255), 300, 300, 5) def draw():     background(255)          myCar1.drive()     myCar1.display()     myCar2.drive()     myCar2.display() class Car:     def __init__(self, c, xpos, ypos, yspeed):         self.c = c         self.xpos = xpos         self.ypos = ypos         self.yspeed = yspeed          def display(self):         stroke(0)         fill(self.c)         rectMode(CENTER)         rect(self.xpos, self.ypos, 20, 10)          def drive(self):         self.ypos = self.ypos + self.yspeed ...

PYTHON: coches en horizontal

def setup():     size(800, 600)     global myCar1, myCar2     myCar1 = Car(color(255, 0, 0), 800, 400, -5)     myCar2 = Car(color(0, 0, 255), 0, 300, 5) def draw():     background(255)     myCar1.drive()     myCar1.display()     myCar2.drive()     myCar2.display() class Car:     def __init__(self, tempC, tempXpos, tempYpos, tempXspeed):         self.c = tempC         self.xpos = tempXpos         self.ypos = tempYpos         self.xspeed = tempXspeed     def display(self):         stroke(0)         fill(self.c)         rectMode(CENTER)         rect(self.xpos, self.ypos, 20, 10)     def drive(self):         self.xpos = self.xpos + self.xspeed         if self.xpos > width: ...

PYTHON: dibujar una línea y calcular su valor mediante una función

def setup():     size(800,600)     fill(255)     frameRate(100)     strokeWeight(4)     stroke(255)     textSize(60) def draw():     background(0)     l=linea(100,100,500,400)     line(100,100,500,400)     text(str(l),400,300) def linea(cx,cy,cx2,cy2):     h=sqrt((cx2-cx)*(cx2-cx)+(cy2-cy)*(cy2-cy))     return h

PYTHON: un segmento y calcular su longitud

x =100 x2 =150 y =500 y2 =400 def setup():     size(800,600)     strokeWeight(5)     textSize(40) def draw():     line(x,x2,y,y2)     h=sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y))     text(h,400,300)

Bolas que reboten cuando se chocan: array de objetos

int N = 8;                 Pelota[] pelotas;        void setup() {   size(800, 600);   frameRate(300);      pelotas = new Pelota[N];      for (int i = 0; i < N; i++) {     float r = random(15, 40);     pelotas[i] = new Pelota(random(r, width - r), random(r, height - r), random(-2, 2), random(-2, 2), r);   } } void draw() {   background(220);   for (int i = 0; i < N; i++) {     pelotas[i].mover();     pelotas[i].rebotar();     pelotas[i].dibujar();   }   for (int i = 0; i < N; i++) {     for (int j = i + 1; j < N; j++) {       pelotas[i].choque(pelotas[j]);     }   } } class Pelota {   float x, y;     float dx, dy;     float r;             Pelota(float Tx, float Ty, float Tdx, float Tdy, flo...

Bolas que reboten cuando se chocan: transfiriéndose toda la energia

float x1, y1, dx1, dy1; float x2, y2, dx2, dy2; float r = 30; void setup() {   size(600, 400);   frameRate(500);   x1 = random(60,500);   y1 = random(60,300);   x2 = random(60,500);   y2 = random(60,300);   dx1 = random(-1,1);   dy1 = random(-1,1);   dx2 = random(-1,1);   dy2 = random(-1,1); } void draw() {   background(220);   ellipse(x1, y1, r*2, r*2);   ellipse(x2, y2, r*2, r*2);   x1 = x1 + dx1;   y1 = y1 + dy1;   x2 = x2 + dx2;   y2 = y2 + dy2;  if (x1-r < 0) {     dx1 = -1*dx1;   }   if (x1+r > width) {     dx1 = -1*dx1;   }   if (y1-r < 0) {     dy1 = -1*dy1;   }   if (y1+r > height) {     dy1 = -1*dy1;   }      if (x2-r < 0) {     dx2 = -1*dx2;   }   if (x2+r > width) {     dx2 = -1*dx2;   }   if (y2-r < 0) {     dy2 = -1*dy2; ...

Bolas que cuando choquen se pare el programa

float x1, y1, dx1, dy1; float x2, y2, dx2, dy2; float r = 30; void setup() {   size(600, 400);   frameRate(300);   x1 = random(60,600);   y1 = random(60,400);   x2 = random(60,600);   y2 = random(60,400);   dx1 = random(-1,1);   dy1 = random(-1,1);   dx2 = random(-1,1);   dy2 = random(-1,1); } void draw() {   background(220);   ellipse(x1, y1, r*2, r*2);   ellipse(x2, y2, r*2, r*2);   x1 += dx1;   y1 += dy1;   x2 += dx2;   y2 += dy2;     if (x1-r < 0) {     dx1 = -1*dx1;   }   if (x1+r > width) {     dx1 = -1*dx1;   }   if (y1-r < 0) {     dy1 = -1*dy1;   }   if (y1+r > height) {     dy1 = -1*dy1;   }      if (x2-r < 0) {     dx2 = -1*dx2;   }   if (x2+r > width) {     dx2 = -1*dx2;   }   if (y2-r < 0) {     dy2 = -1*dy2;   }...

Coches que se chocan con una bola movida por mi

boolean r,l,u,d; int x,y=60; Car[] myCars; int numCoches = 30; void setup() {   size(800, 600);   myCars = new Car[numCoches];       for (int i = 0; i < numCoches; i++) {     color c = color(random(255), random(255), random(255));     float xpos = random(width-5);     float ypos = random(height-5);     float xspeed = random(-6, 6);            if (xspeed == 0){         xspeed = 1;}     myCars[i] = new Car(c, xpos, ypos, xspeed);   } } void draw() {   background(255);   muevebola();   dibujabola();      for (int i = 0; i < numCoches; i++) {     myCars[i].drive();          if(dist(x,y,myCars[i].xpos,myCars[i].ypos)<27){      noLoop();      }          myCars[i].display();   } } void muevebola(){   if (r){     x=...