0
\$\begingroup\$

I'm trying to add collisions to my first game, and it has a bug.

My code is:

import pygame
from pygame.locals import *
import sys
import os
import time
width = 950
height = 500

screen = pygame.display.set_mode((width, height))
Game = False
pygame.init()
icon = pygame.image.load(os.path.join("Images", "icon.png")).convert_alpha()
pygame.display.set_icon(icon)
pygame.display.set_caption('Flappy Dog')

def game():
    Game = True
    background = pygame.image.load(os.path.join("Images", "Background_00.png")).convert()
    Dog1 = pygame.image.load(os.path.join("Images", "Dog1.png")).convert_alpha()
    SpikeUp0 = pygame.image.load(os.path.join("Images", "SpikeUp0.png")).convert_alpha()
    SpikeDown0 = pygame.image.load(os.path.join("Images", "SpikeDown0.png")).convert_alpha()
    Bone = pygame.image.load(os.path.join("Images", "Bone.png")).convert_alpha()

    boneSprite = pygame.sprite.Sprite()
    boneSprite.image = Bone
    boneSprite.rect = boneSprite.image.get_rect()
    spikeUpSprite = pygame.sprite.Sprite()
    spikeUpSprite.image = SpikeUp0
    spikeUpSprite.rect = spikeUpSprite.image.get_rect()
    spikeDownSprite = pygame.sprite.Sprite()
    spikeDownSprite.image = SpikeDown0
    spikeDownSprite.rect = spikeDownSprite.image.get_rect()
    dogSprite = pygame.sprite.Sprite()
    dogSprite.image = Dog1
    dogSprite.rect = dogSprite.image.get_rect()
    bones = pygame.sprite.Group()
    bones.add(boneSprite)
    dog = pygame.sprite.Group()
    dog.add(dogSprite)
    spikes = pygame.sprite.Group()
    spikes.add(spikeUpSprite)
    spikes.add(spikeDownSprite)
    pygame.display.update()

    Dog1_pos_y = 100
    Dog1_pos_x = 30

    while Game == True:
        pygame.event.pump()
        if pygame.key.get_pressed()[K_UP]: # Up
            Dog1_pos_y -= 1
            screen.blit(background, (0, 0))
            screen.blit(Dog1, (Dog1_pos_x, Dog1_pos_y))
            screen.blit(SpikeDown0, (0, 436))
            screen.blit(SpikeUp0, (0, 0))
            pygame.display.update()
            pygame.time.delay(1)
        else: # Down
            Dog1_pos_y = Dog1_pos_y +0.875
            screen.blit(background, (0, 0))
            screen.blit(Dog1, (Dog1_pos_x, Dog1_pos_y))
            screen.blit(SpikeDown0, (0, 436))
            screen.blit(SpikeUp0, (0, 0))
            pygame.display.update()
            pygame.time.delay(1)
        if pygame.key.get_pressed() [K_LEFT]:
            Dog1_pos_x -= 1.5
            screen.blit(background, (0, 0))
            screen.blit(Dog1, (Dog1_pos_x, Dog1_pos_y))
            screen.blit(SpikeDown0, (0, 436))
            screen.blit(SpikeUp0, (0, 0))
            pygame.display.update()
            pygame.time.delay(1)
        if pygame.key.get_pressed() [K_RIGHT]:
            Dog1_pos_x += 1.5
            screen.blit(background, (0, 0))
            screen.blit(Dog1, (Dog1_pos_x, Dog1_pos_y))
            screen.blit(SpikeDown0, (0, 436))
            screen.blit(SpikeUp0, (0, 0))
            pygame.display.update()
            pygame.time.delay(1)

        if pygame.sprite.spritecollideany(dogSprite, spikes):
            print ("GAME OVER")
        for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 pygame.quit()
                 sys.exit()
                 pygame.display.update()


def gameOver():
    background = pygame.image.load(os.path.join("Images", "Background_00.png")).convert()
    GameOver = pygame.image.load(os.path.join("Images", "Game-Over.png")).convert_alpha()
    replay = pygame.image.load(os.path.join("Images", "Replay.png")).convert_alpha()
    Dog0 = pygame.image.load(os.path.join("Images", "Dog0.png")).convert_alpha()
    Bone = pygame.image.load(os.path.join("Images", "Bone.png")).convert_alpha()


    screen.blit(background, (0, 0))
    screen.blit(GameOver, (0, 0))
    screen.blit(replay, (600, 375))
    screen.blit(Dog0, (10, 250))
    pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                pygame.display.update()
            if event.type == pygame.MOUSEBUTTONDOWN:
                x_mouse, y_mouse = pygame.mouse.get_pos()
                if x_mouse >= 600 and y_mouse >= 375:
                    game()


def mainScreen():
    background = pygame.image.load(os.path.join("Images", "Background_00.png")).convert()
    FlappyDog = pygame.image.load(os.path.join("Images", "Flappy.png")).convert_alpha()
    Play = pygame.image.load(os.path.join("Images", "Play.png")).convert_alpha()
    Dog = pygame.image.load(os.path.join("Images", "Dog0.png")).convert_alpha()
    SpikeUp0 = pygame.image.load(os.path.join("Images", "SpikeUp0.png")).convert_alpha()
    SpikeUp1 = pygame.image.load(os.path.join("Images", "SpikeUp1.png")).convert_alpha()
    SpikeDown0 = pygame.image.load(os.path.join("Images", "SpikeDown0.png")).convert_alpha()
    SpikeDown1 = pygame.image.load(os.path.join("Images", "SpikeDown1.png")).convert_alpha()
    Bone = pygame.image.load(os.path.join("Images", "Bone.png")).convert_alpha()

    screen.blit(background, (0, 0))
    screen.blit(SpikeUp0, (0, 0))
    screen.blit(SpikeDown0, (0, 450))
    screen.blit(FlappyDog, (0, 0))
    screen.blit(Dog, (10, 250))
    screen.blit(Bone, (150, -300))
    screen.blit(Bone, (150, 400))
    screen.blit(Play, (650, 370))
    pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                pygame.display.update()
            if event.type == MOUSEBUTTONDOWN:
                x_mouse, y_mouse = pygame.mouse.get_pos()
                if x_mouse >= 650 and y_mouse >= 370:
                    game()

mainScreen()

I don't know why, when I press the play button I go to the game screen for a second (I'm not touching the spikes) and I go to the game over screen.

There is no error message

Thanks in advantage.

\$\endgroup\$
1
\$\begingroup\$

pygame.sprite.groupcollide returns a dictionary that always includes the sprites in the first group. Therefore, the if test you have written will always return true.

You should re-write the function to check whether or not there are sprites in the list for every sprite in the dog group.

\$\endgroup\$
  • \$\begingroup\$ Ok, I understand. But how can I do a def or a if that returns me True or False? \$\endgroup\$ – ArnauBG Apr 17 at 16:20
  • \$\begingroup\$ @ArnauBG You should write a function that checks every list in the dictionary returned by groupcollide. If any of those lists has anything it it, return true. Otherwise, return false. \$\endgroup\$ – Joe Apr 17 at 16:46
  • \$\begingroup\$ For the moment I have This. It returns {<Sprite sprite(in 1 groups)>: [<Sprite sprite(in 1 groups)>, <Sprite sprite(in 1 groups)>]} @Joe What does it mean? Sorry for inconvenience. \$\endgroup\$ – ArnauBG Apr 20 at 17:42
  • \$\begingroup\$ @ArnauBG that's a dictionary with the results from groupcollide, with one entry. The first key is a Sprite, probably from dog. Its value is a list of two sprites, which are probably from spikes. I interpret that to mean, your dog has collided with at least two spikes. \$\endgroup\$ – Joe Apr 23 at 14:49
  • \$\begingroup\$ No, in the game I press play, for a second I am on the game screen and passed a second I am at Game Over but returns me {<Sprite sprite(in 1 groups)>: [<Sprite sprite(in 1 groups)>, <Sprite sprite(in 1 groups)>]} everytime I press play. \$\endgroup\$ – ArnauBG Apr 23 at 18:52
0
\$\begingroup\$

Ok, I found the solution.

Change the way to make images sprites, like this:

Dog = pygame.sprite.Sprite()
Dog.rect = pygame.Rect(0, 100, 270, 256)
Bone = pygame.sprite.Sprite()
Bone.rect = pygame.Rect(850, 0, 87, 498)
Dog.image = pygame.image.load("Dog1.png").convert_alpha()
Bone.image = pygame.image.load("Bone.png").convert_alpha()

And don't use pygame.sprite.groupcollide, use pygame.sprite.collide_rect(Dog, Bone)

When blit the image replace the x and y pos. and use Dog.rect.topleft and Bone.rect.topleft. This will work.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.