I have a programming question

Asked 2 years ago, Updated 2 years ago, 16 views

Hello, I am a student who is learning Python at school recently.

In class Making a ball in a random position in a transparent box

box(size = vec(42,42,42), opacity = 0.4)
import random
for i in range(100) :
rate(10)
x = random.randint(-20,20)
y = random.randint(-20,20)
z = random.randint(-20,20)
r = random.randint(0,100) / 100
g = random.randint(0,100) / 100
b = random.randint(0,100) / 100
sphere(pos = vec(x,y,z), color = vec(r,g,b), opacity = r)

These examples and

Expressing free-falling motion

a = sphere(color = vec(1,0.4,0.6),make_trail = True, pos = vec(0,10,0))
a.v = vec(2,5,0)
box(size = vec(40,1,20), color = vec(0.2,0.9,0.4))
while True : # Repeat while the condition is True!
    rate(100) # Repeat 100 times per second
    a.pos = a.pos + 0.01 * a.v
    if a.pos.y <= 1.5 :
        a.v.y = -a.v.y
    else :
        a.v.y = a.v.y + 0.01 * -9.8

I learned an example like this, where you combine these two and several spheres move in a transparent box and bounce when they touch the wall I want to make something like that, but since I'm a beginner, I don't know how to make the code. I'm looking for someone who can help me. Also, I would appreciate it if you could explain it at eye level.

python

2022-09-20 15:52

1 Answers

import random
import time
Obj = []

class C():
    def __init__(self, name):
        self.number = random.randint(0,100)
        self.name = name
        self.act = 1

    def move(self):
        self.number = random.randint(0,100)
        print(f'{self.name} .. {self.number}')

def all_move(object):
    for i in object:
        i.move()
    print('\n')

def make_object(num):
    for i in num:
        t = C(i)
        Obj.append(t)

r = ['one','two','three','four']
make_object(r)

while(True):
    all_move(Obj)
    time.sleep(0.5)

Why don't we objectify the "concrete" in this way?


2022-09-20 15:52

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.