0
\$\begingroup\$

I'm trying to make the gravity a bit slower, like

Dog.rect.y += 0.5
print (Dog.rect.y)
screen.blit(background, (0, 0))
screen.blit(spikeUp0.image, (0, 0))
screen.blit(spikeDown0.image, (0, 436))
screen.blit(Dog.image, (Dog.rect.topleft))
pygame.display.update()

But the dog floats. The minimum I get set to y 1 with rect methods. How can I make it slower?

Here is the complete code in GitHub Gist

\$\endgroup\$
1
\$\begingroup\$

I'm not sure if it would work well, as I'm not familiar with pygame, but you could stick the value that you want in your Dog object as a float, and do calculations on this value, then use this value converted to int to update the rect.y variable.

Dog.computation_y += 0.5
Dog.rect.y = math.floor(Dog.computation_y)

You'll be able to control a bit more the rate at which the y will change. This also means that the object will not move every frame.

\$\endgroup\$
  • \$\begingroup\$ Sorry but I can't understand. This is my first game I coded in Pygame. \$\endgroup\$ – ArnauBG May 3 '19 at 14:26
  • \$\begingroup\$ What part could be clearer? \$\endgroup\$ – Vaillancourt May 3 '19 at 14:28
  • \$\begingroup\$ Is this part: "but you could stick the value that you want in your Dog object as a float, and do calculations on this value, then use this value converted to int to update the rect.y variable. Dog.computation_y += 0.5 Dog.rect.y = math.floor(Dog.computation_y)" \$\endgroup\$ – ArnauBG May 3 '19 at 15:23
  • \$\begingroup\$ You can add a variable to you Dog object that is called computation_y. That variable is to be used to do the calculations: whenever you want to move the dog, you use that variable. This will allow to modify the position of the object the way you want. When you're ready to draw your Dog, use that value to set the position of the sprite (through the rect). This allows you to de-couple the logic from the rendering, which is something done pretty often. \$\endgroup\$ – Vaillancourt May 3 '19 at 15:33
0
\$\begingroup\$

Ok, I did not remember that it was so easy only I had to increase the delay and done.
Sorry for inconvenience.

\$\endgroup\$
  • \$\begingroup\$ It's good that you found a fix! Just be aware that you won't be able to use that fix when you'll have multiple objects moving at different speeds. \$\endgroup\$ – Vaillancourt May 3 '19 at 18:52
  • \$\begingroup\$ Ok, thanks for all \$\endgroup\$ – ArnauBG May 3 '19 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.