Forum Archive

Why isn't my script running correctly? [SOLVED]

Pythonistapro777

First I have this script, that works fine:

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.cells=Rect(self.x, self.y, 5, 5)
                cells=self.cells

        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(*self.cells)

class Intro(Scene):
        def setup(self):
                self.psize=13
                global plocx
                global plocy
                plocx=240
                plocy=160
                self.player = Rect(plocx, plocy, 20, 20)
                self.colour = Color(random(), random(), random())

                self.particles = []
                for p in range(100):
                        self.particles.append(Particle(self.size))

        def touch_began(self, touch):
                global x1
                global y1
                x1=touch.location.x
                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)/4
                        newplocx=plocx+addx

                if x < x1:
                        subx=(x-x1)/4
                        newplocx=plocx+subx

                if y > y1:
                        addy=(y-y1)/4
                        newplocy=plocy+addy

                if y < y1:
                        suby=(y-y1)/4
                        newplocy=plocy+suby

                xmin=215
                xmax=265
                ymin=140
                ymax=190

                while xmax > plocx and newplocx > plocx:
                        plocx = plocx + 1
                        self.player = Rect(plocx, plocy, 16, 16)

                while xmin < plocx and newplocx < plocx:
                        plocx = plocx - 1
                        self.player = Rect(plocx, plocy, 16, 16)

                while ymax > plocy and newplocy > plocy:
                        plocy = plocy + 1
                        self.player = Rect(plocx, plocy, 16, 16)

                while ymin < plocy and newplocy < plocy:
                        plocy = plocy - 1
                        self.player = Rect(plocx, plocy, 16, 16)

        def draw(self):
                background(0, 0.05, 0.2)
                self.player = Rect(plocx, plocy, self.psize, self.psize)
                for p in self.particles:
                        p.update()
                        p.draw()
                        cells = p.cells
                        if self.player.intersects(cells):
                                self.newpsize=self.psize+0.2
                                self.psize=self.newpsize
                                self.particles.remove(p)
                ellipse(*self.player)

run(Intro(), LANDSCAPE)

The main script for agar.io

Then the script for moving the cells:

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 = 0
                self.vy = 0
                self.colour = Color(random(), random(), random())

        def update(self):
                self.x += self.vx
                self.y += self.vy
                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)
                rect(self.x, self.y, 8, 8)

class Intro(Scene):
        def setup(self):
                self.particles = []
                for p in xrange(100):
                        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()

        def touch_began(self, touch):
                global x1
                global y1
                x1=touch.location.x
                y1=touch.location.y


        def touch_moved(self, touch):
                x=touch.location.x
                y=touch.location.y

                for p in self.particles:
                        if x1 > x:
                                p.vx = 1
                        if x1 < x:
                                p.vx = -1
                        if y1 > y:
                                p.vy = 1
                        if y1 < y:
                                p.vy = -1
                        if x1 == x:
                                p.vx = 0
                        if y1 == y:
                                p.vy = 0

        def touch_ended(self, touch):
                for p in self.particles:
                        p.vx = 0
                        p.vy = 0

run(Intro())
Pythonistapro777

Then I put those 2 together:

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 = 0
                self.vy = 0
                self.colour = Color(random(), random(), random())
                self.cells=Rect(self.x, self.y, 5, 5)
                cells=self.cells

        def update(self):
                self.x += self.vx
                self.y += self.vy
                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(*self.cells)

class Intro(Scene):
        def setup(self):
                self.psize=13
                global plocx
                global plocy
                plocx=240
                plocy=160
                self.player = Rect(plocx, plocy, 20, 20)
                self.colour = Color(random(), random(), random())

                self.particles = []
                for p in range(100):
                        self.particles.append(Particle(self.size))

        def touch_began(self, touch):
                global x1
                global y1
                x1=touch.location.x
                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

                for p in self.particles:
                        if x1 > x:
                                p.vx = 1
                                subx=(x-x1)/4
                                newplocx=plocx+subx
                        if x1 < x:
                                p.vx = -1
                                addx=(x-x1)/4
                                newplocx=plocx+addx
                        if y1 > y:
                                p.vy = 1
                                suby=(y-y1)/4
                                newplocy=plocy+suby
                        if y1 < y:
                                p.vy = -1
                                addy=(y-y1)/4
                                newplocy=plocy+addy
                        if x1 == x:
                                p.vx = 0
                        if y1 == y:
                                p.vy = 0

                xmin=215
                xmax=265
                ymin=140
                ymax=190

                while xmax > plocx and newplocx > plocx:
                        plocx = plocx + 1
                        self.player = Rect(plocx, plocy, 16, 16)

                while xmin < plocx and newplocx < plocx:
                        plocx = plocx - 1
                        self.player = Rect(plocx, plocy, 16, 16)

                while ymax > plocy and newplocy > plocy:
                        plocy = plocy + 1
                        self.player = Rect(plocx, plocy, 16, 16)

                while ymin < plocy and newplocy < plocy:
                        plocy = plocy - 1
                        self.player = Rect(plocx, plocy, 16, 16)

        def touch_ended(self, touch):
                for p in self.particles:
                        p.vx = 0
                        p.vy = 0

        def draw(self):
                background(0, 0.05, 0.2)
                self.player = Rect(plocx, plocy, self.psize, self.psize)
                for p in self.particles:
                        p.update()
                        p.draw()
                        cells = p.cells
                        if self.player.intersects(cells):
                                self.newpsize=self.psize+0.2
                                self.psize=self.newpsize
                                self.particles.remove(p)
                ellipse(*self.player)

run(Intro(), LANDSCAPE)

But it won't move the cells.
Could someone please help me with what I have done wrong, because I can't seem to figure it out.

Thanks in advance!

Also, it wouldn't let me post the whole thing because it was marked as "spam" by akismet.com. Sorry if it caused any inconvinience. Furthermore, sorry I've been asking loads of questions on the forum, but this and maybe one or two more, till I'm finnished with Agar.io clone. :D

Pythonistapro777

@ccc @Webmaster4o @dgelessus @Phuket2 @Cethric

wradcliffe

You need to understand that this type of post really IS spam of the forums. Your troubles have a lot to do with the fact that you are learning how to program in Python. These types of posts pollute the forum with code that is not useful for solving Pythonista problems and degrades the ability for other users to search for answers to problems in the future. It is akin to grafitti since other users in the future may stumble across this stuff and use it.

You have already benefitted greatly by getting advice from users like JonB to "refactor" your code and eliminate the globals. If you would just listen to that advice and do that, you will likely figure out what is wrong with your program along the way.

The key thing is that this has nothing to do with Pythonista!!!! Take it to stackoverflow or some other forum that is geared toward answering questions about programming in Python. PLEASE!

I am going to mark this posting for moderation but don't take it personally. I can see that your are just trying to learn and I really appreciate that.

ccc
def update(self):
                print(self.vx, self.vy)  # when this prints (0, 0), no movement should be expected
                self.x += self.vx
                self.y += self.vy

I would highly recommend that you find another Python developer in your area and engage in pair programming as a great way to accelerate your learning.

Cethric

Two things to look out for in your code.
1. 0 times a number is always equal to 0 (look and the initialisation of self.vx and self.vy)
2. The position of the self.cells layer is never changed (i.e. after initialisation the only time it is referenced is to redraw and to check for colision. Try setting self.cells.x and self.cells.y in the update method)

Also as @ccc mentioned consider creating a Github repository which we can all look at and provide feedback on. I also agree with @wardcliffe