Array of sprites?

22 views
Skip to first unread message

Paul

unread,
Sep 24, 2011, 4:33:04 PM9/24/11
to pyglet-users
please help, im trying to get an image to draw itself multiple times
(so it looks like there are more then one images) but all with
different y coordinates.
(like a classic space invaders game kinda)
so i tried to make an array for the 'monsters'...
when i do this none of the monsters showup on the screen..
please help :)

[tt]

import pyglet
import random

monster = pyglet.image.load('monster.png')
window = pyglet.window.Window(500, 500)
map = pyglet.image.load('map.png')

monsters = []

@window.event
def on_draw():
window.clear()
if (len(monsters)) < 10:
for M in monsters:
monsters.append(monster(x= 200,
y=random.randint(20, 480)))
monsters.M.draw()
map.blit(0, 0, 0)

pyglet.app.run()
[/tt]

thanks! and sry , i dont know how to display this code better on here..

Paul

unread,
Sep 24, 2011, 4:40:10 PM9/24/11
to pyglet-users
oops! its supposted to be
code:

monsters[M].draw()

:code

i was forgetting my knowledge on arrays haha, but that still didn't
fix the problem..
any help? thnx!

Richard Jones

unread,
Sep 24, 2011, 7:58:19 PM9/24/11
to pyglet...@googlegroups.com
On 25 September 2011 06:33, Paul <thepa...@gmail.com> wrote:
> monsters = []
>
> @window.event
> def on_draw():
>        window.clear()
>        if (len(monsters)) < 10:
>                for M in monsters:
>                        monsters.append(monster(x= 200,
> y=random.randint(20, 480)))
>                        monsters.M.draw()
>        map.blit(0, 0, 0)

The monsters code in there is effectively a no-op (looping over the
empty monsters list). If there was a monster in the list then you'd
(hopefully) get an error from Python because you were mutating the
monsters list while iterating over it.

I suspect what you want to do is:

monsters = []
for i in range(10):
     monsters.append(monster(x=200, y=random.randint(20, 480)))

@window.event
def on_draw():
     window.clear()
     for M in monsters:
           M.draw()
   map.blit(0, 0, 0)


Richard

Paul

unread,
Sep 24, 2011, 9:27:17 PM9/24/11
to pyglet-users
thanks for the help,
but i tried what u suggested and changed it to this:


import pyglet
import random

monster = pyglet.image.load('monster.png')
monster = pyglet.sprite.Sprite(monster)
window = pyglet.window.Window(500, 500)
map = pyglet.image.load('map.png')

monsters = []
for i in range(10):
monsters.append(monster(x = 480, y = random.randint(20, 480)))
#LINE 11

@window.event
def on_draw():
window.clear()
map.blit(0, 0, 0)
for M in monsters:
M.draw()
pyglet.app.run()


but it says this error: TypeError: 'Sprite' object is not callable
(line 11)
Message has been deleted
Message has been deleted

Thomas A R Woelz

unread,
Sep 25, 2011, 1:35:15 AM9/25/11
to pyglet...@googlegroups.com
for i in xrange(10):
    monster = pyglet.sprite.Sprite(pyglet.image.load('monster.png'), x = 480, y = random.randint(20, 480))
    monsters.append(monster)

Greg Ewing

unread,
Sep 25, 2011, 3:42:06 AM9/25/11
to pyglet...@googlegroups.com
Richard Jones wrote:
> If there was a monster in the list then you'd
> (hopefully) get an error from Python because you were mutating the
> monsters list while iterating over it.

Nope, that only happens for dicts. With lists, it will
happily go into an infinite loop in that situation:

>>> L = [1]
>>> for i in L:
... L.append(i+1)
... print L
...
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

... and so on until you kill it.

--
Greg

Paul

unread,
Sep 25, 2011, 5:48:32 PM9/25/11
to pyglet-users
thank you so much!
and to everyone on this thread :)
Reply all
Reply to author
Forward
0 new messages