python - Pygame 矩形之间的碰撞

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

最佳答案

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

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

关于python - Pygame 矩形之间的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51614481/

相关文章:

python - 用于列出 python 模块中所有方法的 Emacs 插件

python - Elastic Beanstalk 无法识别文件的绝对路径,返回 FileNotFoundError

compiler-errors - pygame炮弹碰撞检测

python - 为什么这个 Sprite 没有显示在屏幕上?

python - 物体碰撞时播放声音

python - django 测试数据库不是用 utf8 创建的

python - 将 np.zeros 与 OpenCV 结合使用时,图像看起来曝光过度(几乎全白) imshow

python - 通过比较 Pandas 中前 n 行来获取列的最小值

python - 类型错误 : 'bool' object is not callable?

python - 如何创建 "Solid" block ? pygame