0

I have spent three days changing my code into OOP. Im completely new to python and pygame or coding in general. Many hours of trial and error have gone into this little bit I've got. Now i've hit a point where ive created a rectangle that is changing properly, but the Surface isn't rotating with the rect. I'll insert my code here and can someone tell me what i'm doing wrong. The general idea is, for my OOP/Sprites, I want to have ONE set of animation images/Sprite map, and simply ROTATE that to face the direction pressed. I can get the RECT to work, the surface always looks up. I am FULLY aware, I could make a batch of images/animations for [up/down/left/right] and have it cycle per key press and I did this once successfully but it seems EXCEPTIONALLY redundant. Can anyone work with me on this?

# imports
import pygame
import sys


class Ant(pygame.sprite.Sprite):

    def __init__(self, pos_x, pos_y):
        super().__init__()
        self.image = pygame.image.load('../Invasive Ants/Images/Ants/Space Ants/Space Ant/SALU.png').convert_alpha()
        rotated_image = self.image
        self.rect = rotated_image.get_rect()
        self.angle = 0
        self.rect.center = [pos_x, pos_y]
        self.is_animating = False
        self.angle = 0

    # def animate(self):
        # self.is_animating = True

    def move(self):
        self.image = pygame.image.load('../Invasive Ants/Images/Ants/Space Ants/Space Ant/SALU.png').convert_alpha()
        rotated_image = self.image
        self.rect = rotated_image.get_rect()
        self.angle = 0
        ant_speed = 5

        if event.key == pygame.K_w:
            pygame.transform.rotate(rotated_image, 45)
            self.rect.centery -= ant_speed
            print('the ant moved up')


        if event.key == pygame.K_s:
            pygame.transform.rotate(rotated_image, 45)
            self.rect.centery += ant_speed
            print('the ant moved down')


        if event.key == pygame.K_a:
            pygame.transform.rotate(rotated_image, 45)
            self.rect.centerx -= ant_speed
            print('the ant moved left')


        if event.key == pygame.K_d:
            pygame.transform.rotate(rotated_image, 45)
            self.rect.centerx += ant_speed
            print('the ant moved right')
        return rotated_image



# game init
pygame.init()
clock = pygame.time.Clock()

# set up your game window
pygame.display.set_mode()
pygame.display.set_caption('Invasive Ants')
x, y = pygame.display.get_window_size()
game_window = pygame.display.set_mode((x, y))

# make groups
moving_ant = pygame.sprite.GroupSingle()

# make instance of object class -ex. ant = Ant()
player_ant = Ant(x/2, y/2)
moving_ant.add(moving_ant, player_ant)
# add instances to groups -ex. moving_ant.add(moving_ant,player)


# while true game loop
while True:

    # Check for game close
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # Check for key press
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()

            if event.key == pygame.K_w:
                player_ant.move()
                pygame.key.set_repeat(1, 10)

            if event.key == pygame.K_s:
                player_ant.move()
                pygame.key.set_repeat(1, 10)

            if event.key == pygame.K_a:
                player_ant.move()
                pygame.key.set_repeat(1, 10)

            if event.key == pygame.K_d:
                player_ant.move()
                pygame.key.set_repeat(1, 10)

    # make objects seen
    game_window.fill((65, 65, 65))
    moving_ant.draw(game_window)
    moving_ant.update()

    pygame.display.update()
    clock.tick(60)

I have spent three days changing my code into OOP. Im completely new to python and pygame or coding in general. Many hours of trial and error have gone into this little bit I've got. Now i've hit a point where ive created a rectangle that is changing properly, but the Surface isn't rotating with the rect. I'll insert my code here and can someone tell me what i'm doing wrong. The general idea is, for my OOP/Sprites, I want to have ONE set of animation images/Sprite map, and simply ROTATE that to face the direction pressed. I can get the RECT to work, the surface always looks up. I am FULLY aware, I could make a batch of images/animations for [up/down/left/right] and have it cycle per key press and I did this once successfully but it seems EXCEPTIONALLY redundant. Can anyone work with me on this?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • `pygame.transform.rotate(rotated_image, 45)` does nothing at all. [`pygame.transform.rotate`](https://www.pygame.org/docs/ref/transform.html#pygame.transform.rotate) does not rotate the surface itself but it returns a new and rotated surface. You have to use it like this `rotated_image = pygame.transform.rotate(image, angle)`. Read [How do I rotate an image around its center using PyGame?](https://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame/54714144#54714144). Everything you need to know about rotating images in Pygame is explained there. – Rabbid76 Jan 02 '23 at 22:28

0 Answers0