我不能让我的碰撞在pygame中工作

时间:2017-10-14 00:01:20

标签: python pygame

import pygame, time, random

pygame.init()

display_width = 800
display_height = 600

white = (255,255,255)
green = (0,255,0)
black = (0,0,0)

gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

boy = pygame.image.load("pig.png")
fence = pygame.image.load("fence.png")

def message_display(text):
    largeText = pygame.font.Font("freesansbold.ttf",115)
    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 text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()
def crash():
    message_display("Game Over")
def fence1(fencex, fencey, fencew, fenceh, color):
    gameDisplay.blit(fence, (fencex, fencey, fencew, fenceh))
def collision():
    pig_image = pygame.image.load("pig.png")
    pig_rect = pig_image.get_rect()
    fence_image = pygame.image.load("fence.png")
    fence_rect = fence_image.get_rect()
    if pig_rect.colliderect(fence_rect):
        crash()
def game_loop():
    fence_startx = 900
    fence_starty = gameDisplay.get_height() * 0.8
    fence_speed = 15
    fence_width = 50
    fence_height = 50

    x = gameDisplay.get_width() * 0.1
    y = gameDisplay.get_height() * 0.8
    y_change = 0
    on_ground = True
    gravity = .9

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    if on_ground:
                        y_change = -20
                        on_ground = False

        y_change += gravity
        y += y_change
        if y >= 500:
            y = 500
            y_change = 0
            on_ground = True

        gameDisplay.fill(green)
        gameDisplay.blit(boy, (x, y))
        fence1(fence_startx, fence_starty, fence_width, fence_height, white)
        fence_startx -= fence_speed
        randomx = random.randint(1,3)

        if fence_startx <= -50:
            if randomx == 1:
                fence_startx = 1000
            if randomx == 2:
                fence_startx = 800
            if randomx == 3:
                fence_startx = 1500

        collision()

        pygame.display.flip()

        clock.tick(60)

game_loop()
pygame.quit()
quit()

由于某种原因,当我运行此代码时,它会让我感到厌烦,并且#34; Game Over&#34;。有人可以帮我解决这个问题。我在这段代码中使用了碰撞功能。使用pygame.rect代码是在pygame中进行碰撞的最佳方法吗?一切都有效,除了我在游戏中的碰撞。我非常感谢一些帮助。感谢。

1 个答案:

答案 0 :(得分:2)

collision函数中,您创建两个rects,但将其坐标保留在默认位置(0, 0),以便rects重叠。您应该在主game_loop函数中为播放器和围栏添加rects,将其xy(或topleft)属性设置为所需的坐标,然后更新它们是while循环的每次迭代。要检查rects是否发生碰撞,请将它们传递给碰撞函数并执行以下操作:

def collision(boy_rect, fence_rect):
    if boy_rect.colliderect(fence_rect):
        # I'm just printing it to shorten the code example.
        print('crash')

这是一个简单,完整的例子:

import random
import pygame


pygame.init()

green = (0,255,0)

gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

boy = pygame.Surface((40, 60))
boy.fill((0, 40, 100))
fence = pygame.Surface((50, 50))
fence.fill((100, 60, 30))


def collision(boy_rect, fence_rect):
    if boy_rect.colliderect(fence_rect):
        # I'm just printing it to shorten the code example.
        print('crash')


def game_loop():
    fence_speed = 15
    # Create a fence and a boy rect and set their x,y or topleft coordinates.
    fence_rect = fence.get_rect()
    fence_rect.x = 900
    fence_rect.y = gameDisplay.get_height() * 0.8

    x = gameDisplay.get_width() * 0.1
    y = gameDisplay.get_height() * 0.8
    boy_rect = boy.get_rect(topleft=(x, y))

    y_change = 0
    on_ground = True
    gravity = .9

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    if on_ground:
                        y_change = -20
                        on_ground = False

        y_change += gravity
        y += y_change
        if y >= 500:
            y = 500
            y_change = 0
            on_ground = True
        boy_rect.y = y  # Update the rect position.

        gameDisplay.fill(green)
        gameDisplay.blit(boy, boy_rect)
        gameDisplay.blit(fence, fence_rect)
        # Update the position of the fence rect.
        fence_rect.x -= fence_speed

        if fence_rect.x <= -50:
            # You can use random.choice to pick one of these values.
            fence_rect.x = random.choice((800, 1000, 1500))
        # Pass the two rects and check if they collide.
        collision(boy_rect, fence_rect)

        pygame.display.flip()
        clock.tick(60)

game_loop()
pygame.quit()