P
P
pelmen_x_pelmen2021-09-28 15:19:06
Python
pelmen_x_pelmen, 2021-09-28 15:19:06

Why doesn't the pygame rect object add small numbers to the x and y attributes?

import pygame
import math


# блок цветов
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN =(0,255,0)
BLUE = (0,0,255)


# блок основных переменных
FPS = 60
WINDOW_SIZE = (720,640)
LVL_SIZE = (720,640)
lvl_surface = pygame.Surface(LVL_SIZE)


# блок основных объектов
window = pygame.display.set_mode(WINDOW_SIZE)
time = pygame.time.Clock()


# класс персонажа
class Hero(pygame.sprite.Sprite):


    def __init__(self,x,y):

        pygame.sprite.Sprite.__init__(self)

        # атрибуты
        self.image_list = []
        for i in range(3):
            self.image_list.append(pygame.image.load('hero.png').convert_alpha())
        self.image = self.image_list[0]
        self.rect = self.image.get_rect(center = (x,y))
        self.a = 90
        # список кнопок управления героем
        # индекс кнопок: a-0 d-1 space-2
        self.key_down = [False, False, False]
        self.speed = 1


    def move(self):

        self.rect.x += self.speed * math.cos(math.radians(self.a))
        self.rect.y -= self.speed * math.sin(math.radians(self.a))
        self.rect.y += 0.1

    def rotation(self):

        self.image = pygame.transform.rotate(self.image_list[0],self.a - 90)
        self.rect = self.image.get_rect(center = self.rect.center)


    def update(self):

        if self.key_down[0]:
            self.a += 1

        if self.key_down[1]:
            self.a -= 1

        if self.key_down[2]:
            self.move()
        self.rotation()

# игровые объекты
hero = Hero(200,200)


# функция основного цикла
def game():

    # основной цикл
    while True:

        # цикл проверки событий
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                return
            
            # проверка нажатий клавиш
            if event.type == pygame.KEYDOWN:
                
                if event.key == pygame.K_a:
                    hero.key_down[0] = True

                if event.key == pygame.K_d:
                    hero.key_down[1] = True
                    
                if event.key == pygame.K_SPACE:
                    hero.key_down[2] = True

            if event.type == pygame.KEYUP:

                if event.key == pygame.K_a:
                    hero.key_down[0] = False

                if event.key == pygame.K_d:
                    hero.key_down[1] = False
                    
                if event.key == pygame.K_SPACE:
                    hero.key_down[2] = False

        # отображение объектов и поверхностей
        lvl_surface.fill(BLACK)
        lvl_surface.blit(hero.image,hero.rect)
        window.blit(lvl_surface,(0,0))
        pygame.draw.line(window,RED,hero.rect.center,(hero.rect.centerx + 200 * math.cos(math.radians(hero.a)), hero.rect.centery - 200 * math.sin(math.radians(hero.a))))

        # обновление объектов и поверхностей
        pygame.display.update()
        hero.update()

        # задержка(ФПС)
        time.tick(FPS)


# вызов функции основного цикла
if __name__ == '__main__':
    game()


pygame.quit()


as planned, the character should move in a circle if the rotation angle is constantly changed (hero.a) , but in the end it describes a spiral, most likely because hero.rect.x/y does not change if fractional numbers are added,
tell me how to fix it.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question