如何与Pygame中的列表项冲突()?

时间:2017-05-26 18:03:32

标签: python python-3.x random pygame collision-detection

我偶然发现了一个小问题

*按"空格键"红场在显示器上随机生成产卵

问题 如何检测玩家方块何时与红色方块碰撞? 有可能吗?

“准则”可能会让您了解我所谈论的内容......

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 个答案:

答案 0 :(得分:1)

如果您使用红色方块创建一个组,则可以使用pygame.sprite.spritecollideany()

检查玩家方格和红色方块之间的碰撞

这包括为玩家方形和红色方块创建精灵类,以及为要添加的红色方块创建一个组。我的更新代码如下,当玩家方块与任何红色方块碰撞时,它会在屏幕上打印碰撞,您可以在checkCollision函数中处理碰撞。

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()

引用:

How to use sprite groups in pygame

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

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