【问题标题】:Pygame collision between rectangles矩形之间的 Pygame 碰撞
【发布时间】:2018-07-31 23:08:18
【问题描述】:

所以我想制作一个基本的 2d 游戏,你可以控制一个矩形,如果你碰到墙就会死。我已经创建了一些墙图片

但我现在卡住了。即使我查看了文档,我也真的不知道代码应该是什么样子。第二件事是我不确定如何创建“Sprite”。如何将我的矩形设为“Sprite”,否则没关系,它可以保持只是一个移动的普通矩形?

import pygame
pygame.init()

win = pygame.display.set_mode((1200, 600))

pygame.display.set_caption("My Game")

x = 40
y = 45
width = 30
height = 30
vel = 4
black = (0,0,0)

run = True
while run:
pygame.time.delay(15)

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

win.fill((255,255,255))

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and x > 0:
    x -= vel
if keys[pygame.K_RIGHT] and x < 1200 - width:
    x += vel
if keys[pygame.K_UP] and y > 0:
    y -= vel
if keys[pygame.K_DOWN] and y < 600 - height:
    y += vel

pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

# Boundaries
pygame.draw.rect(win, (black), (0, 0, 1200, 20))
pygame.draw.rect(win, (black), (0, 0, 20, 600))
pygame.draw.rect(win, (black), (0, 580, 1200, 20))
pygame.draw.rect(win, (black), (1180, 0, 20, 600))

# Obstacle walls
pygame.draw.rect(win, (black), (300, 0, 20, 530))
pygame.draw.rect(win, (black), (20, 100, 230, 20))
pygame.draw.rect(win, (black), (70, 200, 230, 20))
pygame.draw.rect(win, (black), (20, 300, 230, 20))
pygame.draw.rect(win, (black), (70, 400, 230, 20))

# Middle Wall
pygame.draw.rect(win, (black), (600, 100, 20, 500))


pygame.display.update()

pygame.quit()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    碰撞检测不需要 pygame 精灵和精灵组,你可以使用pygame.Rects。我会将所有墙壁放入一个列表中,并将它们设为pygame.Rect 对象,然后可以使用矩形的colliderect 进行碰撞。您还需要播放器的矩形(只需将x, y, width, height 变量替换为矩形)。使用 for 循环遍历 walls 列表,以检查是否与玩家 rect 发生碰撞并绘制它们。

    import pygame
    
    
    pygame.init()
    win = pygame.display.set_mode((1200, 600))
    clock = pygame.time.Clock()  # A clock to limit the frame rate.
    BLACK = (0, 0, 0)
    WHITE = (255,255,255)
    RED = (255, 0, 0)
    
    # The player variables have been replaced by a pygame.Rect.
    player = pygame.Rect(40, 45, 30, 30)
    vel = 4
    # The walls are now pygame.Rects as well. Just put them into a list.
    walls = [
        pygame.Rect(0, 0, 1200, 20), pygame.Rect(0, 0, 20, 600),
        pygame.Rect(0, 580, 1200, 20), pygame.Rect(1180, 0, 20, 600),
        pygame.Rect(300, 0, 20, 530), pygame.Rect(20, 100, 230, 20),
        pygame.Rect(70, 200, 230, 20), pygame.Rect(20, 300, 230, 20),
        pygame.Rect(70, 400, 230, 20), pygame.Rect(600, 100, 20, 500),
        ]
    
    run = True
    while run:
        # Handle the events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        keys = pygame.key.get_pressed()
    
        # Update the player coordinates.
        if keys[pygame.K_LEFT] and player.x > 0:
            player.x -= vel
        if keys[pygame.K_RIGHT] and player.x < 1200 - player.width:
            player.x += vel
        if keys[pygame.K_UP] and player.y > 0:
            player.y -= vel
        if keys[pygame.K_DOWN] and player.y < 600 - player.height:
            player.y += vel
    
        # Game logic.
        for wall in walls:
            # Check if the player rect collides with a wall rect.
            if player.colliderect(wall):
                print('Game over')
                # Then quit or restart.
    
        # Draw everything.
        win.fill(WHITE)
        pygame.draw.rect(win, RED, player)
        # Use a for loop to draw the wall rects.
        for wall in walls:
            pygame.draw.rect(win, BLACK, wall)
    
        pygame.display.update()
        clock.tick(60)  # Limit the frame rate to 60 FPS.
    
    pygame.quit()
    

    【讨论】:

    • 非常感谢,尤其是“墙”列表。我永远不会想到这一点。
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多