python - My game keeps calling a method for the wrong sprite -


when try run game code tries run method wrong sprite. think line "player.handle_keys()" problem when run it, says can't find "handle_keys()" method "meteor" class. haven't got line run "meteor.handle_keys()" class should not have method.

here code:

import pygame import random  # define colors black = (  0,   0,   0) white = (255, 255, 255) red   = (255,   0,   0)   bg = pygame.image.load("bg1.png")     class space_ship(pygame.sprite.sprite):     def __init__(self, color, width, height):         super().__init__()     # create image of space_ship1, , fill color.     # image loaded disk.         self.image = pygame.surface([width, height])         self.image.fill(white)         self.image.set_colorkey(white)         self.rect = self.image.get_rect()     #draw image         self.image = pygame.image.load("player1.gif").convert()      # draw ellipse         #pygame.draw.ellipse(self.image, color, [0, 0, width, height])      # x , y coordinates         self.x = 500         self.y = 450        def handle_keys(self):             """ handles keys """             key = pygame.key.get_pressed()             dist = 5 # distance moved in 1 frame             if key[pygame.k_right]: # right key                 self.x += dist # move right             elif key[pygame.k_left]: # left key                 self.x -= dist # move left      def draw(self, surface):         """ draw on surface """         # blit @ current position         surface.blit(self.image, (self.x, self.y))     class asteroid(pygame.sprite.sprite):     def __init__(self, color, width, height):         super().__init__()     # create image of space_ship1, , fill color.     # image loaded disk.         self.image = pygame.surface([width, height])         self.image.fill(white)         self.image.set_colorkey(white)         self.rect = self.image.get_rect()     # draw ellipse         #pygame.draw.ellipse(self.image, color, [0, 0, width, height])          self.image = pygame.image.load("ast1.gif").convert()      # x , y coordinates         self.x = random.randint(50,950)         self.y = 10      def draw(self, surface):         """ draw on surface """         # blit @ current position         surface.blit(self.image, (self.x, self.y))      def fall(self):         dist = 5         self.y +=dist         if self.y > 600:             self.x = random.randint(50,950)             self.y = random.randint(-2000, -10)        def respawn(self):         self.y = -10       # initialize pygame pygame.init()  # set height , width of screen screen_width = 1000 screen_height = 600 screen = pygame.display.set_mode([screen_width, screen_height])  # list of 'sprites.' each sprite in program # added list. # list managed class called 'group.' asteroid_list = pygame.sprite.group()  # list of every sprite. # asteroids , player well. all_sprites_list = pygame.sprite.group()     player = space_ship(red, 20, 15) all_sprites_list.add(player) asteroid_1 = asteroid(black, 40, 40) asteroid_list.add(asteroid_1) all_sprites_list.add(asteroid_1) asteroid_2 = asteroid(black, 40, 40) asteroid_list.add(asteroid_2) all_sprites_list.add(asteroid_2) asteroid_3 = asteroid(black,40, 40) asteroid_list.add(asteroid_3) all_sprites_list.add(asteroid_3) asteroid_4 = asteroid(black,40, 40) asteroid_list.add(asteroid_4) all_sprites_list.add(asteroid_4) asteroid_5 = asteroid(black,40, 40) asteroid_list.add(asteroid_5) all_sprites_list.add(asteroid_5) asteroid_6 = asteroid(black,40, 40) asteroid_list.add(asteroid_6) all_sprites_list.add(asteroid_6) asteroid_7 = asteroid(black,40, 40) asteroid_list.add(asteroid_7) all_sprites_list.add(asteroid_7) asteroid_8 = asteroid(black,40, 40) asteroid_list.add(asteroid_8) all_sprites_list.add(asteroid_list)   # loop until user clicks close button. done = false  # used manage how fast screen updates clock = pygame.time.clock()  score = 0  # ----------------- main program loop -------------------- while not done:     event in pygame.event.get():         if event.type == pygame.quit:             done = true      #call upon function     player.handle_keys()      # clear screen     screen.fill(white)      #inside of game loop     screen.blit(bg, (0, 0))      # see if player space_ship1 has collided anything.     blocks_hit_list = pygame.sprite.spritecollide(player, asteroid_list, true)      # check list of collisions.     player in blocks_hit_list:         score +=1         print(score)      # draw spites     player.draw(screen)     asteroid_1.draw(screen)     asteroid_1.fall()     asteroid_2.draw(screen)     asteroid_2.fall()     asteroid_3.draw(screen)     asteroid_3.fall()     asteroid_4.draw(screen)     asteroid_4.fall()     asteroid_5.draw(screen)     asteroid_5.fall()     asteroid_6.draw(screen)     asteroid_6.fall()     asteroid_7.draw(screen)     asteroid_7.fall()     asteroid_8.draw(screen)     asteroid_8.fall()       #all_sprites_list.draw(screen)  # limit 60 frames per second     clock.tick(60)      # go ahead , update screen we've drawn.     pygame.display.flip()  pygame.quit() 

you overriding player in loop

# check list of collisions. player in blocks_hit_list:     score +=1     print(score) 

change else , good

# check list of collisions. something_else in blocks_hit_list:     score +=1     print(score) 

enjoy


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -