Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Need help using timers for circles

Hi, I am having trouble trying to use the time_handler to basically spawn a ball every second. I have made a newBalls list where I will append each balls object into the list but the newBalls list is always empty and I have no idea why. Here is the code so for if anybody could explain to me how I would be able to spawn ball(circles) every second using the timer.

Code
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui

import random
from Vector import Vector

CANVAS_WIDTH = 600
CANVAS_HEIGHT = 300
totalBalls = 10
newBalls = []

class Ball:
   def __init__(self, pos, vel, radius, color):
       self.pos = pos
       self.vel = vel
       self.radius = radius
       self.color = color
       self.border = 1
       
   def draw(self, canvas):
    if(self.radius != 10):
        self.radius -= 1
    canvas.draw_circle(self.pos.get_p(), self.radius, self.border, self.color, self.color)
       
       
   def update(self):
       self.pos.add(self.vel)

class Interaction:
   def __init__(self, ball):
       self.ball = ball
       
   def update(self):
       for balls in self.ball:
           balls.update()
   
   def draw(self, canvas):
       self.update()
       for balls in self.ball:
           balls.draw(canvas)
       

       
def randCol():
   r = random.randrange (0, 256)
   g = random.randrange (0, 256)
   b = random.randrange (0, 256)
   return 'rgb('+str(r)+ ','+str(g)+ ','+str(b)+ ')'

def randRad():
   rad = random.randint(10, 51)
   return rad

def randVel():
   vel = random.randint(-5, 6)
   return vel

def randBall():
   randBall = Ball(Vector(CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2), Vector(randVel(),randVel()), randRad(), randCol())
   return randBall

#balls = []
balls = [ randBall() for i in range(totalBalls) ]

def time_handler():
   global newBalls, balls
   for x in range(totalBalls):
       newBalls.append(balls[x])

interaction = Interaction(balls)



frame = simplegui.create_frame("Test", CANVAS_WIDTH, CANVAS_HEIGHT)
timer = simplegui.create_timer(1000, time_handler)

frame.set_draw_handler(interaction.draw)
frame.start()
timer.start()
Try doing

Code
def time_handler():
  global newBalls, balls
  print(1)
  for x in range(totalBalls):
      newBalls.append(balls[x])

and see if 1 is being printed
i like python lua js
(Jan 26, 2020 09:36 AM)Riԁdle Wrote:  Try doing

Code
def time_handler():
  global newBalls, balls
 print(1)
  for x in range(totalBalls):
      newBalls.append(balls[x])

and see if 1 is being printed

I just tried this and 1 isn't being printed out either. I am clueless as to why...
(Jan 26, 2020 09:44 AM)Bora or Hyuna Wrote:  I just tried this and 1 isn't being printed out either. I am clueless as to why...

If it looks exactly like that your indentions are off and you’re probably running an older version of that script.


Keep debugging by adding print before the line where that function is called. Keep going until it finally prints.
(Jan 26, 2020 09:46 AM)Charger Wrote:  If it looks exactly like that your indentions are off and you’re probably running an older version of that script.


Keep debugging by adding print before the line where that function is called. Keep going until it finally prints.

Wow thanks so much! I hate these indentation issues I keep having with Python, I have finally managed to solve this!
Thank you guys for the help, much appreciated.
I see that I'm late, but if it helps, I recommend using pycharm or atom.io text editor as your IDE for python. Auto indentation with tabs is much easier, and a good habit to get into for readability with all languages, not just python.

On a side note, the indentation is the only complaint I ever hear from people who start learning python as their 2nd/3rd/+ language. It's a great language, but I guess that's one habit that's hard for programmers to form when coming from other languages.
Malware Dev & Grey Hat available for contract work. DM if you need me.

OSCP, OSCE, OSEE, OSWE, OSWP, CEH, CISSP

Yeah, I've been around a while
(Jan 26, 2020 10:15 PM)Nomad.Null Wrote:  I see that I'm late, but if it helps, I recommend using pycharm or atom.io text editor as your IDE for python. Auto indentation with tabs is much easier, and a good habit to get into for readability with all languages, not just python.

On a side note, the indentation is the only complaint I ever hear from people who start learning python as their 2nd/3rd/+ language. It's a great language, but I guess that's one habit that's hard for programmers to form when coming from other languages.

I love Pycharm, it is so helpful in making development quicker and debugging easier.
[Image: Iu0EfTL.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by Most3rs2Mar 23, 2017 11:07 PM 4 190 Mar 23, 2017 11:07 PM
Last Post: Most3rs2
[.NET] by selfieman231Dec 11, 2016 04:22 PM 5 158 Dec 11, 2016 04:22 PM
Last Post: selfieman231
[C#] by colja123Apr 3, 2016 01:47 AM 7 171 Apr 3, 2016 01:47 AM
Last Post: GuRuHaX