微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

另一个帮助,现在我想知道如何通过任何其他矩形删除碰撞矩形?

如何解决另一个帮助,现在我想知道如何通过任何其他矩形删除碰撞矩形?

**我想通过另一个不与任何人碰撞的矩形删除碰撞矩形我可以在下面给出的代码中改进什么,基本上我想更改列表中碰撞矩形的坐标**>

import pygame
from random import randint
pygame.init()

screen = pygame.display.set_mode((350,675))
pygame.display.set_caption("First Game")

x_poss = randint(0,350)
y_poss = randint(0,675)
width = 20
height = 20
vel = 8

bases = []

red = (255,0)

image_1 = pygame.image.load('bird.png').convert_alpha()
image_2 = pygame.image.load('base.png').convert_alpha()
image_3 = pygame.image.load('base2.png').convert_alpha()
image_4 = pygame.image.load('enemy.png').convert_alpha()

isJump = False
jumpCount = 10
run = True

def draw_rect(color,x,y,width,height):
    pygame.draw.rect(screen,color,(x,height))

for i in range(5):
    bases.append(pygame.draw.rect(screen,(255,255,255),(randint(0,330),randint(0,675),height)))
    bases.append(pygame.draw.rect(screen,height)))


while run:
    pygame.time.delay(50)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    key = pygame.key.get_pressed()

    if key[pygame.K_LEFT] and x_poss > vel - width:
        x_poss -= vel

    if key[pygame.K_RIGHT] and x_poss < 350 - vel - width:
        x_poss += vel

    if not(isJump):
        if key[pygame.K_UP] and y_poss > vel:
            y_poss -= vel

        if key[pygame.K_DOWN] and y_poss < 670 - height - vel:
            y_poss += vel

        if key[pygame.K_SPACE]:
            isJump = True
    else:
        if jumpCount >= -10:
            y_poss -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        else:
            jumpCount = 10
            isJump = False

    screen.fill((255,255))


    for base_1 in bases[:5]:
        screen.blit(image_2,base_1)
    for base_2 in bases[5:]:
        screen.blit(image_3,base_2)

    for i,base in enumerate(bases):
        if base.collidelist(bases[i+1:]) >= 0:
            print('yes')



    draw_rect((0,0),300,440,10,10)
    draw_rect((255,x_poss,y_poss,height)

    screen.blit(image_4,pygame.draw.rect(screen,(0,(300,10)))
    screen.blit(image_1,(x_poss,height)))
    pygame.display.update()

pygame.quit()

** 我可以在下面给出的代码中改进什么,基本上我想更改列表中碰撞矩形的坐标**

for i,base in enumerate(bases):
        if base.collidelist(bases[i+1:]) >= 0:
            print('yes')

解决方法

只接受不与创建矩形列表时已经生成的矩形发生碰撞的矩形:

# DELETE
# for i in range(5):
#    bases.append(pygame.draw.rect(screen,(255,255,255),(randint(0,330),randint(0,675),width,height)))
#    bases.append(pygame.draw.rect(screen,height)))

bases = []
while len(bases) < 10:
    rect = pygame.Rect(randint(0,height)
    if rect.collidelist(bases) < 0:
        bases.append(rect) 

注意,矩形可以用 pygame.Rect 对象表示。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。