0
\$\begingroup\$

I'm trying to make my game's collisions with masks. I have 6 masks but only 2 works.

My code is:

import sys
from os import path
from random import randint
import pygame
from pygame.locals import *

# Pygame Config
width = 950
height = 500

screen = pygame.display.set_mode((width, height))
pygame.init()
pygame.display.set_caption('Collision Mask')

clock = pygame.time.Clock()
FPS = 120

# Variables
Bone1_pos = 0
Bone2_pos = 0

# load images
background = pygame.image.load(path.join("data", "Sprites", "Background_00.png")).convert()


# Sprites

spikeUp0 = pygame.sprite.Sprite()
spikeUp0.image = pygame.image.load(path.join("data", "Sprites", "SpikeUp0.png")).convert_alpha()
spikeUp0.rect = pygame.Rect(0, 0, 1000, 64)
spikeUp0.mask = pygame.mask.from_surface(spikeUp0.image)

spikeDown0 = pygame.sprite.Sprite()
spikeDown0.image = pygame.image.load(path.join("data", "Sprites", "SpikeDown0.png")).convert_alpha()
spikeDown0.rect = pygame.Rect(0, 450, 1000, 64)
spikeDown0.mask = pygame.mask.from_surface(spikeDown0.image)

Dog = pygame.sprite.Sprite()
Dog.image = pygame.image.load(path.join("data", "Sprites", "Dog1.png")).convert_alpha()
Dog.rect = pygame.Rect(100, 200, 127, 110)
Dog.mask = pygame.mask.from_surface(Dog.image)

Bone1_x = 500

# Bone "y" Range: From -50 to 40
Bone1_y = randint(-75, 40)

BoneUp1 = pygame.sprite.Sprite()
BoneUp1.image = pygame.image.load(path.join("data", "Sprites", "BoneUp.png")).convert_alpha()
BoneUp1.rect = pygame.Rect(Bone1_x, Bone1_y, 75, 173)
BoneUp1.mask =pygame.mask.from_surface(BoneUp1.image)

BoneDown1 = pygame.sprite.Sprite()
BoneDown1.image = pygame.image.load(path.join("data", "Sprites", "BoneDown.png")).convert_alpha()
BoneDown1.rect = pygame.Rect(Bone1_x, Bone1_y + 450, 75, 180)
BoneDown1.mask = pygame.mask.from_surface(BoneDown1.image)

Bone2_x = 1000

# Bone "y" Range: From -50 to 40
Bone2_y = randint(-75, 40)

BoneUp2 = pygame.sprite.Sprite()
BoneUp2.image = pygame.image.load(path.join("data", "Sprites", "BoneUp.png")).convert_alpha()
BoneUp2.rect = pygame.Rect(Bone2_x, Bone2_y, 75, 173)
BoneUp2.mask = pygame.mask.from_surface(BoneUp2.image)

BoneDown2 = pygame.sprite.Sprite()
BoneDown2.image = pygame.image.load(path.join("data", "Sprites", "BoneDown.png")).convert_alpha()
BoneDown2.rect = pygame.Rect(Bone2_x, Bone2_y + 450, 75, 180)
BoneDown2.mask = pygame.mask.from_surface(BoneDown2.image)

obstacles = pygame.sprite.Group()
for o in [spikeDown0, spikeUp0, BoneUp1, BoneDown1, BoneUp2, BoneDown2]:
    obstacles.add(o)

def game():
    while True:
        #FPS
        clock.tick(FPS)

        #Global Variables
        global Bone1_pos
        global Bone2_pos

        global Bone1_x
        global Bone1_y

        global Bone2_x
        global Bone2_y

        global obstacles

        #Bone Positions

            #Pos 1
        if Bone1_pos == 1:
            Bone1_y = -85
        if Bone2_pos == 1:
            Bone2_y = -85

            #Pos 2
        if Bone1_pos == 2:
            Bone1_y = -30
        if Bone2_pos == 2:
            Bone2_y = -30

            #Pos 3
        if Bone1_pos == 3:
            Bone1_y = 5
        if Bone2_pos == 3:
            Bone2_y = 5

        def DogMove():
            if pygame.key.get_pressed()[K_UP]: # Up
                Dog.rect.y -= 1
                pygame.time.delay(2)
            else: # Down
                Dog.rect.y += 1
                pygame.time.delay(2)
            if pygame.key.get_pressed() [K_LEFT]: # Left
                Dog.rect.x -= 1
                pygame.time.delay(2)
            if pygame.key.get_pressed() [K_RIGHT]: # Right
                Dog.rect.x += 1
                pygame.time.delay(2)   

        #Sprite blit
        screen.blit(background, (0, 0))
        screen.blit(BoneDown1.image, (Bone1_x, Bone1_y + 400))
        screen.blit(BoneUp1.image, (Bone1_x, Bone1_y))
        screen.blit(BoneDown2.image, (Bone2_x, Bone2_y + 400))
        screen.blit(BoneUp2.image, (Bone2_x, Bone2_y))
        screen.blit(spikeUp0.image, (0, 0))
        screen.blit(spikeDown0.image, (0, 436))
        screen.blit(Dog.image, (Dog.rect.topleft))

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        # Movement
            #Dog
        DogMove()
            # Bones
        Bone1_x -= 1
        Bone2_x -= 1

        # Barrier
        if Dog.rect.x < 0:
            Dog.rect.x = 0
        elif Dog.rect.x > 822:
            Dog.rect.x = 822

        # Collison
        if pygame.sprite.spritecollide(Dog, obstacles, False, pygame.sprite.collide_mask):
            pygame.display.set_caption("Flappy Dog (Collision)")
        else:
            pygame.display.set_caption("Flappy Dog (No Collision)")

        #Bone Replacement
        if Bone1_x <= -70 :
            Bone1_pos = randint(1,3)
            Bone1_x = 1100

        if Bone2_x <= -70:
            Bone2_pos = randint(1,3)
            Bone2_x = 1100 
game()
\$\endgroup\$
  • \$\begingroup\$ Might want to try and find a step by step debugger and use it on your code, or add debug output lines to your game or to the engine. \$\endgroup\$ – Vaillancourt Aug 8 at 13:07
  • 1
    \$\begingroup\$ Declaring globals inside a loop doesn't seem to be a good practice. \$\endgroup\$ – Juanpa Aug 29 at 16:43

Your Answer

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

Browse other questions tagged or ask your own question.