PyGame Y轴碰撞检测

时间:2018-11-05 17:06:55

标签: python pygame collision-detection

前段时间,我使用pygame创建了这个非常基本的游戏。游戏的目的是躲避掉落在您身上的矩形。首先,我想增加上下移动的能力,而不仅仅是左右并进。因此,我添加了按钮按下,y更改等。这一切都很好,但是有一个问题。如果我越过下面的障碍物,即使我离它不远,它仍然算是碰撞(或如我在代码中命名的崩溃)。我认为问题出在game_loop函数内部,因为这是发生冲突逻辑的地方。

这是代码(没有错误代码出现):

import pygame
import time
import random
import base64
import io
import webbrowser

pygame.init()

display_width = 800
display_height = 600

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
cyan = (0, 80, 200)
green = (0, 200, 0)
grey = (200, 200, 200)

pepe_width = 70

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('The Best Game')
clock = pygame.time.Clock()

photo = 'R0lGODlhRgBGAPZUAAAAAAAAMwAAzAArAAArMwArzAAr/wBVmQBVzABV/zMAADMAMzMrADMrMzMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOA/zOqM2YAM2YrAGYrM2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA/2aqAGaqM2aqZmaqmWaqzJkrAJkrM5lVAJlVM5lVZplVmZmAAJmAM5mAZpmAmZmAzJmqZpmqmZmqzJmq/5nVmZnVzMxVAMxVM8xVZsyAAMyAM8yAZsyqZsyqmcyqzMyq/8zVmczVzMzV/8z/zMz////VzP/V////zP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUAAFQALAAAAABGAEYAAAf/gFOCg4SFhoeIiYZMOScoKI45SUxJk4qXmJmJSSghKCI4OSInOT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nmO+WJyRPibBKnxLZdqS07aySOBM6oe4grYN4aQEQi9oCUMAF20vCbNIEskuWiKQi9WtJgcokQ1lzB0yQXyGcoAMLEBe1BqhAD/jyAyJtwHcDCiMot+otInEKL9xZhAUKgFoEQDSmFngaXwDaH9Xsxz9/MY+nPvVFnZInjoaQgg8egNoGQxQCPrAY/3hgRiRbFuyW12wSf+jCA3t+xQdsBW8C7JuAFLS0tJHDJXIYGDKo2tJF4nHlIJ7QhBGEeMT3fbAOgPCVHlQgBLUEVRiHXGH0aKqj5OGBdvkyAhITN8dNVQJfdACBCVjxoIMiF6D4palP8jNAv5TAE7vpY+Iy9b70hWgFKmjaBBRAohsmWEGpmbC6DOwvdMMABVLQg5MloQIh/i5iC5BfQlRguDGtwEzVi9BGA8qHnuolqGgqAftaYM1SKK2b6wtlNFfgxIdWLQIOoKS5HmeujU71eltnAAO82QcheeiTU0tf1Yb3BVGsgACIS+gCHMDKOtPQeDTEXLxM4AAIqHWrE/tw6bY2WLXgRYAFqDWFlwpa1CN+8wEcgAAKdLvbB6AABcId7nFz+wCHJrGD171pKMfUACnwShTm22kRfzfN9M23fYfrZBH729nuRuyZE/CA8XZT1s++9rvX/V12uzvN9m6wst1sgWR5t60QhXi/ax3xWWdcxBB3MIn7jdgH5JebU+jBxd+N2GUPt+VpbjnLF+7yZbe65iyX+c1H0APgCkLlBrAAs5ktbWYHXdpBN7oFin5aoTcdARAYutCnHtEERJ3qQo960xPA81MdwgcqsEDYVaABEpD97CnQgArITgK1l33sYy+72+NudrGfXe1inzveVVABFSQBYIgIBAA7'


output = io.BytesIO(base64.b64decode(photo))

pepeImg = pygame.image.load(output)


def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: " + str(count), True, black)
    gameDisplay.blit(text, (10, 10))


def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])


def pepe(x, y):
    gameDisplay.blit(pepeImg, (x, y))


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()


def message_display(text):
    LargeText = pygame.font.Font('freesansbold.ttf', 70)
    TextSurf, TextRect = text_objects(text, LargeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()


def crash():
    message_display('lel pal u ded')


def button(msg, x, y, w, h, a, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, a, (x, y, w, h))

        if click[0] == 1 and action != None:
            if action == "play":
                game_loop()

    else:
        pygame.draw.rect(gameDisplay, a, (350, 450, 100, 50))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, TextRect = text_objects(msg, smallText)
    TextRect.center = ((x + (w / 2)), y + (h / 2))
    gameDisplay.blit(textSurf, TextRect)


def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(cyan)
        LargeText = pygame.font.Font('freesansbold.ttf', 70)
        TextSurf, TextRect = text_objects("The Best Game", LargeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Play", 350, 450, 100, 50, green, "play")

        pygame.display.update()
        clock.tick(15)


def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
    y_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 3.5
    thing_width = 100
    thing_height = 100

    dodged = 0

    gameExit = False

    while not gameExit:

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                elif event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    x_change = 0
                    y_change = 0

        x += x_change
        y += y_change
        gameDisplay.fill(grey)

        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed
        pepe(x, y)
        things_dodged(dodged)

        if x > display_width - pepe_width or x < 0:
            crash()
        if y > display_height - pepe_width or y < 0:
            ctash()


        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0, display_width)
            dodged += 1
            thing_speed += 0.1


        if y < thing_starty + thing_height:

            if x > thing_startx and x < thing_startx + thing_width or x + pepe_width > thing_startx and x + pepe_width < thing_startx + thing_width:
                #webbrowser.open('https://youtu.be/dQw4w9WgXcQ')
                crash()



        pygame.display.update()
        clock.tick(120)


game_intro()
game_loop()
pygame.quit()
quit()

编辑:这是最终的工作代码:

import pygame
import time
import random
import base64
import io
import webbrowser

pygame.init()

display_width = 800
display_height = 600

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
cyan = (0, 80, 200)
green = (0, 200, 0)
grey = (200, 200, 200)

pepe_width = 70
pepe_height = 70

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('The Best Game')
clock = pygame.time.Clock()

photo = 'R0lGODlhRgBGAPZUAAAAAAAAMwAAzAArAAArMwArzAAr/wBVmQBVzABV/zMAADMAMzMrADMrMzMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOA/zOqM2YAM2YrAGYrM2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA/2aqAGaqM2aqZmaqmWaqzJkrAJkrM5lVAJlVM5lVZplVmZmAAJmAM5mAZpmAmZmAzJmqZpmqmZmqzJmq/5nVmZnVzMxVAMxVM8xVZsyAAMyAM8yAZsyqZsyqmcyqzMyq/8zVmczVzMzV/8z/zMz////VzP/V////zP///wAAAAAAAAAAAAAA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ABFSQBYIgIBAA7'


output = io.BytesIO(base64.b64decode(photo))

pepeImg = pygame.image.load(output)


def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: " + str(count), True, black)
    gameDisplay.blit(text, (10, 10))


def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])


def pepe(x, y):
    gameDisplay.blit(pepeImg, (x, y))


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()


def message_display(text):
    LargeText = pygame.font.Font('freesansbold.ttf', 70)
    TextSurf, TextRect = text_objects(text, LargeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()


def crash():
    message_display('lel pal u ded')


def button(msg, x, y, w, h, a, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, a, (x, y, w, h))

        if click[0] == 1 and action != None:
            if action == "play":
                game_loop()

    else:
        pygame.draw.rect(gameDisplay, a, (350, 450, 100, 50))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, TextRect = text_objects(msg, smallText)
    TextRect.center = ((x + (w / 2)), y + (h / 2))
    gameDisplay.blit(textSurf, TextRect)


def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(cyan)
        LargeText = pygame.font.Font('freesansbold.ttf', 70)
        TextSurf, TextRect = text_objects("The Best Game", LargeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Play", 350, 450, 100, 50, green, "play")

        pygame.display.update()
        clock.tick(15)


def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
    y_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 3.5
    thing_width = 100
    thing_height = 100

    dodged = 0

    gameExit = False

    while not gameExit:

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                elif event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    x_change = 0
                    y_change = 0

        x += x_change
        y += y_change
        gameDisplay.fill(grey)

        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed
        pepe(x, y)
        things_dodged(dodged)

        if x > display_width - pepe_width or x < 0:
            crash()
        if y > display_height - pepe_width or y < 0:
            crash()


        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0, display_width)
            dodged += 1
            thing_speed += 0.1

        if (x < thing_startx + thing_width and x + pepe_width > thing_startx and
            y < thing_starty + thing_height and y + pepe_height > thing_starty):
            crash()
            webbrowser.open('https://youtu.be/dQw4w9WgXcQ')




        pygame.display.update()
        clock.tick(120)


game_intro()
game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:1)

您的碰撞检测已损坏。这应该起作用:

if (x < thing_startx + thing_width and x + pepe_width > thing_startx and
    y < thing_starty + thing_height and y + pepe_height > thing_starty):

我强烈建议为您的对象创建pygame.Rects,因为它们具有可靠且快速的碰撞检测方法。

player = pygame.Rect(100, 200, 70, 70)
enemy = pygame.Rect(150, 200, 100, 100)
# In the main loop.
if player.colliderect(enemy):
    # do something.