矩形之间的Pygame碰撞

时间:2018-07-31 13:24:41

标签: python pygame

因此,我想制作一个基本的2D游戏,在其中您可以控制矩形,而如果您碰到墙,就会死掉。我已经创建了一些墙图片

enter image description here

但是我现在被困住了。即使查看了文档,我也真的不知道代码的外观如何。第二件事是我不确定如何创建“ 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()

1 个答案:

答案 0 :(得分:1)

您不需要pygame精灵和精灵组来进行碰撞检测,只需使用pygame.Rects。我将所有墙壁放入列表中,并使其成为pygame.Rect对象,然后可以使用矩形的colliderect进行碰撞。您还需要播放器使用rect(只需将x, y, width, height变量替换为rect)。使用walls循环遍历for列表,以检查一个人是否与玩家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()