omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    [SOLVED] Help with unlocal variable

    Pythonista
    2
    2
    1823
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Pythonistapro777
      Pythonistapro777 last edited by Pythonistapro777

      In my agar.io clone, I need an intersect for the player and cells, but they are in different classes.

      Here's the code:

      from scene import *
      from random import *
      class Particle(object):
      	def __init__(self, wh):
      		self.w = wh.w
      		self.h = wh.h
      		self.x = randint(0, self.w)
      		self.y = randint(0, self.h)
      		self.vx = randint(-10, 20)
      		self.vy = randint(-10, 20)
      		self.colour = Color(random(), random(), random())
      
      	def update(self):
      		self.x += self.vx
      		self.y += self.vy
      		self.vx *= 0
      		self.vy *= 0
      		if self.x > self.w:
      			self.x = self.w
      			self.vx *= -1
      		if self.x < 0:
      			self.x = 0
      			self.vx *= -1
      		if self.y > self.h:
      			self.y = self.h
      			self.vy *= -1
      		if self.y < 0:
      			self.y = 0
      			self.vy *= -1
      			
      	def draw(self):
      		self.cells=Rect(self.x, self.y, 5, 5)
      		fill(*self.colour)
      		ellipse(*self.cells)
      		if self.cells.intersects(self.cells):
      			pass
      
      class Intro(Scene):
      	def setup(self):
      		self.player = Rect(240,160, 10, 10)
      		self.colour = Color(random(), random(), random())
      		num=100
      		self.particles = []
      		for p in xrange(num):
      			self.particles.append(Particle(self.size))
      		if self.player.intersects(self.cells):
      			pass
      			
      	def draw(self):
      		background(0.00, 0.05, 0.20)
      		fill(*self.colour)
      		ellipse(*self.player)
      		for p in self.particles:
      			p.update()
      			p.draw()
      	
      run(Intro(), LANDSCAPE)
      

      When the code is run there is an error popping up. Please help me with this...

      Thanks in advance!

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        Attribute Error: 'Intro' object has no attribute 'cells'. means that the Intro object does not have an attribute called cells. You need to define one if you want your code to work.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Powered by NodeBB Forums | Contributors