We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello Forum,
I got a question: First off: I'm using Python Mode. I'm trying to instantiate an object 10 times, but whatever i do ends up in the ellipses getting a tremendous speed boost. The idea is: Create 10 ellipses all spawning on different locations and with different velocity. Is this possible using lists? Here is the code, creating 1 instance of the object:
from BallTab import Ball
def setup():
global myFirstBall
size(800, 800)
myFirstBall = Ball(random(50, 750), random(50, 750), 2, 3, 100)
def draw():
global myFirstBall
background(69, 69, 69)
myFirstBall.display()
myFirstBall.move()
if myFirstBall.y > 750:
myFirstBall.ys = -2
if myFirstBall.x > 750:
myFirstBall.xs = -3
if myFirstBall.y < 50:
myFirstBall.ys = 2
if myFirstBall.x < 50:
myFirstBall.xs = 3
BallTab:
class Ball(object):
def __init__(self, xPos, yPos, xSpeed, ySpeed, radius):
self.x = xPos
self.y = yPos
self.xs = xSpeed
self.ys = ySpeed
self.r = radius
def display(self):
ellipse(self.x, self.y, self.r, self.r)
def move(self):
self.x += self.xs
self.y += self.ys
Answers
@juicy -- Yes, adding things to lists is trivial in python. Just use loop over the range/xrange and use
mylistname.append(myobject)
. Or use a list comprehension.From Creating a list of objects in Python:
From other Creating a list of objects in Python:
It works now, thank you very much! If anyone wants to see the finished code, leave a reply!
Glad it worked out, @juicy!
Yes, I would love to see the finished code. I also think sharing your Python example with the forum would help new Processing.py learners.
Hi Juicy
I am newbie here at Phyton, so I'd like to watch the fiinished code if you don't mind
Thanks
Hi @jeremydouglass & @laimperiestro!
Given that @juicy didn't posted his final code yet, I remembered I had a very similar sketch from the previous old forum:
https://forum.Processing.org/one/topic/nullpointerexeption-with-an-arraylist
Its old online link too: :bz
http://studio.ProcessingTogether.com/sp/pad/export/ro.9oyKfI9kOIa77/latest
Made some refactoring in order to modernize it a bit more. 1st the Java Mode version:
And finally, the corresponding Python Mode, so you all can compare them: :-bd
Sorry for the late response. Been kind of busy! Here is my completed code (NOTE: Python):
Main:
Tab: BallTab:
Thxs @juicy and @GoToLoop
Since I've already coded versions for Java & Python modes, why not for p5.js as well? :-bd
http://p5js.SketchPad.cc/sp/pad/view/ro.CwHWpJ$SP1EN8i/latest
Thanks to you both. Having a side-by-side examples of multiple objects in Java mode, Python mode, and p5.js seems really useful -- a kind of "Rosetta Stone" for the learning community.