Python Forum
index error when using lists to blit rotated images
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
index error when using lists to blit rotated images
#1
Hello, I am getting an index error when trying to use lists of rotated images. originally, to rotate images of 2 spaceships (think asteroids), I used a function to rotozoom each image for every degree change. It worked, but not surprisingly, it was lagging. I read that "cached images" might be a better choice, so I have preloaded two lists with 72 images each (5 degree increments through 360 degrees). However, after making one full rotation either cw or ccw, I get: in ship_draw
win.blit(self.image_list[self.rot_angle_index], (self.x_move - int(self.image_list[self.rot_angle_index].get_width() / 2), self.y_move - int(
IndexError: list index out of range

Even though I am ensuring the index value returns to 0, it won't display the image. Any help would be appreciated as to why this is happening and how to fix it. Thanks!

class spaceship(object):
    def __init__(self, x_move, y_move, width, height, rot, image):
        self.x_move = x_move
        self.y_move = y_move
        self.width = width
        self.height = height
        self.rot_angle = rot
        self.vel = 0
        self.hspeed = 0
        self.vspeed = 0
        self.thrust = False
        self.rotated = False
        self.fired_shots_delay = 10
        self.image = image
        self.image_list = []
        self.rot_angle_index = 0
        self.shield = 100
        self.visable = True
        self.score = 0

        self.image_list.append(self.image)
        for i in range(1, 71):
            holder = pygame.transform.rotozoom(self.image, i * 5, 1)
            self.image_list.append(holder)


    def ship_draw(self, win, list, angle):
        
        if self.rot_angle <= -360 or self.rot_angle >= 360:
            self.rot_angle = self.rot_angle - (360 * (ship_one.rot_angle // 360))

        self.rot_angle_index = self.rot_angle // 5

        if self.visable:

            win.blit(self.image_list[self.rot_angle_index], (self.x_move - int(self.image_list[self.rot_angle_index].get_width() / 2), 
                 self.y_move - int(self.image_list[self.rot_angle_index].get_height() / 2)))
        else:
            self.reset()
            win.blit(self.image_list[self.rot_angle_index],
                     (self.x_move - int(self.image_list[self.rot_angle_index].get_width() / 2), self.y_move - int(
                         self.image_list[self.rot_angle_index].get_height() / 2)))

        #  screen wrap
        if self.x_move > game_screen_x:
            self.x_move = 0
        elif self.x_move < 0:
            self.x_move = game_screen_x
        elif self.y_move > game_screen_y:
            self.y_move = 40
        elif self.y_move < 40:
            self.y_move = game_screen_y


# These are called right before main game loop

ship_one = spaceship(left_rnd_location_x, left_rnd_location_y, 32, 32, 0, ship_one_image)
ship_two = spaceship(right_rnd_location_x, right_rnd_location_y, 32, 32, 0, ship_two_image)

# Called at end of game loop before pygame.display.flip

ship_one.ship_draw(win, ship_one.image_list, ship_one.rot_angle)
ship_two.ship_draw(win, ship_two.image_list, ship_two.rot_angle)
Reply


Messages In This Thread
index error when using lists to blit rotated images - by codefun45 - Sep-01-2020, 06:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] pygame blit() method syafiq14 1 4,979 Oct-30-2020, 04:46 PM
Last Post: Russ_CW
  [PyGame] I found a way to generate sprites without .blit. Is it effecient? xBlackHeartx 19 8,638 Dec-07-2019, 01:06 PM
Last Post: metulburr
  moving image with blit rwahdan 2 3,066 Jul-10-2019, 06:24 PM
Last Post: nilamo
  error with survace.blit joemcsk1 3 4,504 Aug-06-2018, 12:23 AM
Last Post: metulburr
  Raspberry Pi pygameui - Fails with error index out of range erhixon 0 2,943 Sep-19-2017, 01:36 PM
Last Post: erhixon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020