Hack Forums
[Python] Need help using timers for circles - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Need help using timers for circles (/showthread.php?tid=6044201)



Need help using timers for circles - Bora or Hyuna - Jan 26, 2020

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()



RE: Need help using timers for circles - Riԁdle - Jan 26, 2020

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


RE: Need help using timers for circles - Bora or Hyuna - Jan 26, 2020

(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...


RE: Need help using timers for circles - Charger - Jan 26, 2020

(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.


RE: Need help using timers for circles - Bora or Hyuna - Jan 26, 2020

(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.


RE: Need help using timers for circles - Nomad.Null - Jan 26, 2020

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.


RE: Need help using timers for circles - Lindros - Jan 27, 2020

(Jan 26, 2020 10:15 PM)True Demon 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.