I wanted to learn pymunk, but for some reason, the gravity isn't working. I watched many tutorials and even followed them word for word, but my gravity never works. This is the code I'm using for my main game, and I've sat here for hours thinking about what could've gone wrong but I still can't find it.
factory_items=[]
space=pymunk.Space()
space.gravity=(0,100)
class factory_item():
def __init__(self):
self.image=apple
self.image=pygame.transform.scale(self.image,(100,100))
self.body=pymunk.Body(1,100,body_type=pymunk.Body.DYNAMIC)
self.body.pos=(random.randrange(0,401),0)
self.shape=pymunk.Circle(self.body,50)
space.add(self.body,self.shape)
def update(self):
win.blit(self.image,(self.body.pos[0]+scrollx,self.body.pos[1]))
The apples are being generated, but they aren't falling.
if timer>0.5:
timer=0
var=factory_item()
factory_items.append(var)
for i in factory_items:
i.update()
space.step(1/50)
It looks like you are using the wrong property to set/get positions of the bodies. The position property of a Pymunk Body is called position
. You can see the full API documentation of the position (and everything else in Pymunk) here: http://www.pymunk.org/en/latest/pymunk.html#pymunk.Body.position
So, try changing your code in the following way:
self.body.pos=(random.randrange(0,401),0)
# should be
self.body.position=(random.randrange(0,401),0)
# and
win.blit(self.image,(self.body.pos[0]+scrollx,self.body.pos[1]))
# should be
win.blit(self.image,(self.body.position[0]+scrollx,self.body.position[1]))
If it still doesnt work you can try to post your full code (preferably something that we can run, but at the same time not too long) in your question, like import random wrote in the comments.