Sep-01-2020, 06:25 AM
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!
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
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) |