1

i stumbled into a little problem:

*When pressing "SPACE Bar" Red Squares keep Spawning randomly on Display

Question How to detect when the player square collides with the red squares? is it possible?

The Code might give you an idea of what I'm talking about...

import pygame, sys
from random import randint
from pygame.locals import*

"List the Stores the Squares"
red_square_list = []

gameDisplay_width = 800
gameDisplay_height = 600

pygame.init()
gameDisplay = pygame.display.set_mode((gameDisplay_width, gameDisplay_height))
pygame.display.set_caption("Square-it")
clock = pygame.time.Clock()
red_color = pygame.Color("red")

"White Square"
white_x = 400
white_y = 300
white_width = 10
white_height = 10
white_color = pygame.Color("white")
white = pygame.Rect(white_x, white_y, white_width, white_height)

"Red Squares"
def create_red_square(x, y):
    red_width = 10
    red_height = 10
    red = pygame.Rect(x, y, red_width, red_height)
    return red

while True:

    clock.tick(60)
    gameDisplay.fill((0, 20, 5))
    gameDisplay.fill(white_color, white)

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

    for each in red_square_list:
        gameDisplay.fill(red_color, each)

    pygame.display.update()

    '''White Square Movement'''
    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        white.left = white.left - 4

    if keys[K_RIGHT]:
        white.right = white.right + 4

    if keys[K_UP]:
        white.top = white.top - 4

    if keys[K_DOWN]:
        white.bottom = white.bottom + 4

    "when Space key Pressed, Spawns red Squares"
    if keys[K_SPACE]:
        x = randint(0, gameDisplay_width)
        y = randint(0, gameDisplay_height)
        red_square_list.append(create_red_square(x, y))

1 Answers1

1

If you create a group out of the red squares, you can check for collision between the player square and the red squares using pygame.sprite.spritecollideany()

This involves creating sprite classes for both the player square as well as the red squares, and a group for the red squares to be added to. My updated code is below, it will print collision to the screen when the player square is colliding with any of the red squares, you can handle the collision in the checkCollision function.

import pygame, sys
from random import randint
from pygame.locals import*

"List the Stores the Squares"
red_square_list = []

gameDisplay_width = 800
gameDisplay_height = 600

pygame.init()
gameDisplay = pygame.display.set_mode((gameDisplay_width, gameDisplay_height))
pygame.display.set_caption("Square-it")
clock = pygame.time.Clock()
red_color = pygame.Color("red")

"White Square"
white_x = 400
white_y = 300
white_width = 10
white_height = 10
white_color = pygame.Color("white")

redSquares = pygame.sprite.Group()

class playerSquare(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width,height])
        self.image.fill(white_color)

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        pygame.draw.rect(self.image, white_color, self.rect)

white = playerSquare(white_x, white_y, white_width, white_height)

class RedSquare(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width,height])
        self.image.fill(red_color)

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        pygame.draw.rect(self.image, red_color, self.rect)

"Red Squares"
def create_red_square(x, y):
    red_width = 10
    red_height = 10
    red = RedSquare(x, y, 10, 10)
    redSquares.add(red)
    return red

def checkCollision():
    if pygame.sprite.spritecollideany(white, redSquares) != None:
        print('collision')
        return True
    return False

while True:
    clock.tick(60)
    gameDisplay.fill((0, 20, 5))
    gameDisplay.fill(white_color, white)

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

    for each in red_square_list:
        gameDisplay.fill(red_color, each)

    pygame.display.update()

    '''White Square Movement'''
    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        white.rect.x = white.rect.x - 4

    if keys[K_RIGHT]:
        white.rect.x = white.rect.x + 4

    if keys[K_UP]:
        white.rect.y = white.rect.y - 4

    if keys[K_DOWN]:
        white.rect.y = white.rect.y + 4

    "when Space key Pressed, Spawns red Squares"
    if keys[K_SPACE]:
        x = randint(0, gameDisplay_width)
        y = randint(0, gameDisplay_height)
        red_square_list.append(create_red_square(x, y))

    checkCollision()

referenced:

How to use sprite groups in pygame

http://www.101computing.net/creating-sprites-using-pygame/

https://www.pygame.org/docs/ref/sprite.html

njoosse
  • 549
  • 3
  • 8