python - 如何使用 Pygame 中的列表项进行 collideect() ?

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

我偶然遇到了一个小问题:

*当按“空格键”时,红色方 block 会在显示屏上随机生成

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

代码可能会让您了解我在说什么......

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

最佳答案

如果您用红色方 block 创建一个组,则可以使用pygame.sprite.spritecollideany()检查玩家方 block 和红色方 block 之间的碰撞

这涉及为玩家方格和红色方格创建 Sprite 类,以及要添加到的红色方格组。我更新的代码如下,当玩家方 block 与任何红色方 block 碰撞时,它会将碰撞打印到屏幕上,您可以在 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

关于python - 如何使用 Pygame 中的列表项进行 collideect() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207683/

相关文章:

java - 如何从java调用python脚本并返回长文本?

java - Python 的 Numpy np.random.choice 的 Java 等价物是什么?

python - pkg_resources.DistributionNotFound : The 'pipenv==2018.10.13' distribution was not found and is required by the application

python - 在 python 中,使用点表示法访问类属性的正确命名法是什么?

javascript - "Math.random() * arr.length | 0"如何返回数组的随机索引

c# - 生成随机位序列

python - 将 3 字节立体声 WAV 文件转换为 numpy 数组

python - 如何开始构建 Mac 应用程序

python-3.x - python 3 从父方法调用子属性

javascript - 按天分配人员