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] xrange

    Pythonista
    4
    8
    5077
    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

      Here is my agar.io clone without movin the player.

      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())
      		self.psize=10
      		global plocx
      		plocx=240
      		global plocy
      		plocy=160
      
      	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)
      		self.player = Rect(plocx, plocy, self.psize, self.psize)
      		fill(*self.colour)
      		ellipse(*self.cells)
      		if not self.player.intersects(self.cells):
      			ellipse(*self.player)
      		if self.player.intersects(self.cells):
      			self.cells=Rect(-2, -2, 5, 5)
      			self.newpsize=self.psize
      			self.newpsize=self.newpsize+1
      			self.psize=self.newpsize
      			ellipse(*self.player)
      			
      class Intro(Scene):
      	def setup(self):
      		num=100
      		self.particles = []
      		for p in xrange(num):
      			self.particles.append(Particle(self.size))
      			
      	def draw(self):
      		background(0.00, 0.05, 0.20)
      		for p in self.particles:
      			p.update()
      			p.draw()
      		
      	
      run(Intro(), LANDSCAPE)
      

      This is it after I added touch and moving the player.

      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())
      		global cells
      		cells=Rect(self.x, self.y, 5, 5)
      
      	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):
      		fill(*self.colour)
      		ellipse(*cells)
      			
      class Intro(Scene):
      	def setup(self):
      		self.psize=10
      		global plocx
      		plocx=240
      		global plocy
      		plocy=160
      		global newplocx
      		newplocx = 0
      		global newplocy
      		newplocy = 0
      		self.player = Rect(plocx, plocy, 16, 16)
      		self.colour = Color(random(), random(), random())
      		self.particles = []
      		for p in xrange(100):
      			self.particles.append(Particle(self.size))
      			
      	def touch_began(self, touch):
      		global x1
      		x1=touch.location.x
      		global y1
      		y1=touch.location.y
      		
      
      	def touch_moved(self, touch):
      		global plocx
      		global plocy
      		global newplocx
      		global newplocy
      		x=touch.location.x
      		y=touch.location.y
      		if x > x1:
      			addx=x-x1
      			newplocx=plocx+addx
      		if x < x1:
      			subx=x-x1
      			newplocx=plocx+subx
      		if y > y1:
      			addy=y-y1
      			newplocy=plocy+addy
      		if y < y1:
      			suby=y-y1
      			newplocy=plocy+suby
      			
      		while newplocx != plocx and newplocy > plocx:
      			plocx = plocx + 1
      			self.player = Rect(plocx, plocx, 16, 16)
      		if plocx == newplocx:
      			self.player = Rect(plocx, plocy, 16, 16)
      				
      		while newplocx != plocx and newplocx < plocx:
      			plocx = plocx - 1
      			self.player = Rect(plocx, plocy, 16, 16)
      		if plocx == newplocx:
      			self.player = Rect(plocx, plocy, 16, 16)
      				
      		while newplocy != plocy and newplocy > plocy:
      			plocy = plocy + 1
      			self.player = Rect(plocx, plocy, 16, 16)
      		if plocy == newplocy:
      			self.player = Rect(plocx, plocy, 16, 16)
      	
      		while newplocy != plocy and newplocy < plocy:
      			plocy = plocy - 1
      			self.player = Rect(plocx, plocy, 16, 16)
      		if plocy == newplocy:
      			self.player = Rect(plocx, plocy, 16, 16)
      			
      	def draw(self):
      		global cells
      		background(1, 1, 1)
      		self.player = Rect(plocx, plocy, self.psize, self.psize)
      		if not self.player.intersects(cells):
      			ellipse(*self.player)
      		if self.player.intersects(cells):
      			self.cells=Rect(-2, -2, 5, 5)
      			self.newpsize=self.psize
      			self.newpsize=self.newpsize+1
      			self.psize=self.newpsize
      			ellipse(*self.player)
      		for p in self.particles:
      			p.update()
      			p.draw()
      	
      run(Intro(), LANDSCAPE)
      

      When you run the code you will notice that the first syntax has 100 cells, as it should-but the second one has only 1 instead of 100. I need help with fixing this...

      Thanks in advance!

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

        Try using range instead of xrange. Xrange returns a generator which can only be iterated through once. Don't have time to read your code, so I don't know for sure this is the case.

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

          That wasn't it, but thanks for contribution..

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

            Ok. I'm in math class, which is why I couldn't read

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

              xrange isn't an iterator, it's an iterable. The important difference is that iterators have a "position" and can generally only be traversed once. Iterables are not iterators, but an iterator can be created for them, which happens automatically for example in a for loop. For one iterable there can be multiple iterators, with each one having its own "position".

              In most cases there's no problem with using xrange instead of range, in fact in Python 3 range acts very much like xrange in Python 2. xrange is more memory-efficient, because unlike range it doesn't create an actual list containing all numbers in the given range. Instead it creates an object that behaves like a list, but without storing all of the number objects.

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

                Right, that's what I was getting at, but is that the solution to the problem?

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

                  Still doesn't work... :(

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

                    global cells
                    is your problem. I would suggest you avoid using globals, especially in classes that you plan on instantiating many copies of!

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