0

with my code right now, whenever my sprite collides with a Pokemon, the pokemon blits at random locations 3 - 5 times then just doesn't let me collide with it anymore. Sometimes the pokemon that blits is random sometimes its not. Here is my pokemon code and my collision code:

class Pokemon:
    def __init__(self, parent_screen):
        self.pokemon = [
                        pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/pokemon/bulbasaur.png').convert(),
                        pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/pokemon/caterpie.png').convert(),
                        pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/pokemon/charmander.png').convert(),
                        pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/pokemon/pidgey.png').convert(),
                        pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/pokemon/squirtle.png').convert()
                        ]
     
        self.parent_screen = parent_screen
        self.x = SIZE
        self.y = SIZE
        self.random_pokemon = random.choice(self.pokemon)
        self.set_colorkey()
        
    def set_colorkey(self):
        for i in range(len(self.pokemon)):
            image = self.pokemon[i]
            image.set_colorkey((0, 0, 0))
            self.pokemon[i] = pygame.transform.scale(image, (150, 150))



        

    def move(self):
        self.parent_screen.blit(random.choice(self.pokemon), (self.x, self.y))
        self.x = random.randint(1,25)*SIZE
        self.y = random.randint(1,25)*SIZE

    def draw_pokemon(self):

         self.parent_screen.blit(self.random_pokemon,  (self.x, self.y))


#MY COLLISION CODE: 

score = 0
class Game:
    def __init__(self):
        pygame.init()
        pygame.display.set_caption('Pokemon Catcher')
        pygame.mixer.init()
        self.surface = pygame.display.set_mode((700, 677))
        self.background_music()
        self.snake = Snake(self.surface)
        self.snake.draw()
        self.random_pokemon = Pokemon(self.surface)
        self.pokemon = Pokemon(self.surface)
        self.pokemon.draw_pokemon()

    def collide(self,x1,y1,x2,y2):
        distance = math.sqrt((math.pow(x2 - x1,2)) + (math.pow(y2-y1,2)))
        if distance < 40:
            return True
        else:
            return False


    def play(self):
        global score
        self.render_background()
        self.snake.walk()
        self.pokemon.draw_pokemon()
        self.display_score()
        pygame.display.flip()

        
        if self.collide(self.snake.x, self.snake.y, self.random_pokemon.x, self.random_pokemon.y):
            score += 1
            self.pokemon.move()
            sound = pygame.mixer.Sound('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/music/cartoon eat.wav')
            pygame.mixer.Sound.play(sound)



            pygame.display.flip()
        # collide with boundary
        if self.snake.x <= -40 or self.snake.y <= -40 or self.snake.x >= 650 or self.snake.y >= 600:

            sound = pygame.mixer.Sound('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/music/bump.wav')
            pygame.mixer.Sound.play(sound)
            raise 'Hit the boundry error'
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
gershinho
  • 33
  • 4
  • Can I ask why you draw a pokemon in your move function and why you are getting a random pokemon ASIDE from the already randomly chosen one? Im talking about first line of your `move()` function – eisa.exe Aug 05 '22 at 21:06
  • @eisa.exe I draw a pokemon in my move function because I call my move function every time I collide with a pokemon. So I want to draw a new random pokemon every time I collide so I just put it in the move function. I am getting a new random pokemon after I collide because the already chosen one is the initial pokemon when you load up the game. So when I collide with that initial pokemon I want to choose a different random pokemon. – gershinho Aug 05 '22 at 21:26
  • Oh, well it would be more efficient and more object oriented to create a new instance of a pokemon after a collision – eisa.exe Aug 05 '22 at 21:28
  • @eisa.exe how would I do that? – gershinho Aug 05 '22 at 21:38
  • 1
    separate functionality. Write a procedure that draws the image where you want it. Write a function that return random co-ordinates. Test them both, then write a procedure that uses them to draw an image at a random co-ordinate. – ctrl-alt-delor Aug 05 '22 at 21:40
  • @ctrl-alt-delor I pretty much have that though. My draw function draws my image where I want it. My move function draws at random coordinate. These functions just won't fit in my collision function – gershinho Aug 05 '22 at 21:56
  • So which bit is broken? – ctrl-alt-delor Aug 05 '22 at 22:09
  • What do you mean by “not random”? – ctrl-alt-delor Aug 05 '22 at 22:10
  • @ctrl-alt-delor The problem is my move function, when I put it in my collision function it doesn't give me the outcome I want. is there a way I could just send you a video of what occurs with my code? – gershinho Aug 05 '22 at 23:09
  • I think you should move the pokemon once it collides but draw it all the time. – Jerry Aug 06 '22 at 05:11

0 Answers0