S
S
Span4ev2022-02-14 09:17:14
Python
Span4ev, 2022-02-14 09:17:14

How to use real numbers less than 1 in pygame?

I will not give the whole code, only the essence. Small code is easier to understand.
If anything, all imports work. The object moves when I pass it an integer or float greater than 1 (and of course it moves at a speed of 1 at 1.9).
And of course it doesn't move when it's a real value less than 1, like 0.9

I know that rect can't store a real number in itself, but I don't understand how it works to pass real numbers to it

class Settings:
  # класс со всеми настройками, который я передаю в другой класс

  def __init__(self):

    # Если здесь  будет целочисленное значение, то объект двигается.
    # self.speed = 1  # Всё ОК!
    # self.speed = 1.9  # Всё ОК!

    # Объект не двигается с вещественными числами меньше 1
    self.speed = 0.3

class NOMATTER:
  # класс, описывающий поведение объектов...

  # вызываемый метод
  def move(self):
    if self.rect.y > 0:
      self.rect.y += float(self.settings.speed)


Where and how to specify float() so that I can work with them? I don't quite understand the principle. It seems like I should assign a float to the property that I will be working with, for example,
self.nevazhno_kakoe_nazvanie = float(self.rect.centerx) # Чтобы работать с перемещением по оси "икс"
# и затем:
self.nevazhno_kakoe_nazvanie += self.settings.speed
# и затем использовать:
self.rect.centerx = self.nevazhno_kakoe_nazvanie


Then, as I understand it, it will work.
But it turns out that everything needs to be "translated" into a float, with which I will work:
x, y, etc.
I find it cumbersome and inconvenient, and that I don't understand the concept. Maybe there is a simpler way so that I can immediately pass a real number 0.3 and work with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay, 2022-02-18
@Span4ev

The rect coordinates are stored as an integer value. In order to implement movement less than a pixel, it is necessary to store the value of the coordinates in separate real variables and substitute them (discarding the fractional part) into the current coordinates of the object. It also costs its real numbers to increase, and not the current coordinates directly.
p.s. My first reply. Do not judge strictly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question