如何判断sprite是否在pygame中触及另一个sprite

时间:2017-01-03 04:11:04

标签: python pygame

我知道有人已经问过这个,但我的代码非常不同,他们的解决方案不适用于我的问题。

我正在尝试制作一款游戏,目标是“抓住”水果。然而,每当我的篮子接触水果时,水果就会越过篮子。这是我的代码:

import pygame
import time
import random
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
car_width = 64
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Avoid The Falling Objects')
clock = pygame.time.Clock()
carImg = pygame.image.load('basket.png')
appleImg = pygame.image.load('apple.png')
score = 0
def things(thingx, thingy):
    #pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])\
    gameDisplay.blit(appleImg, (thingx,thingy))
def car(x,y):
    gameDisplay.blit(carImg, (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',40)
    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(('You dropped a fruit! Your score was: {}').format(score))
def game_loop():
    score = 0
    x = (display_width * 0.45)
    y = (display_height * 0.8)
    x_change = 0
    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 7
    thing_width = 100
    thing_height = 100
    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
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
        x += x_change
        gameDisplay.fill(white)
        things(thing_startx, thing_starty)
        thing_starty += thing_speed
        car(x,y)
        if x > display_width - car_width or x < 0 - car_width:
            crash()
        if thing_starty > display_height:
            crash()
        if y < thing_starty+thing_height:
            print('y crossover')
            if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx  and x + car_width < thing_startx:
                score += 1
                thing_starty = -100
                thing_startx = random.randrange(0, display_width)
        pygame.display.update()
        clock.tick(60)
game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:2)

您可以使用pygame.Rect()colliderect()来检查碰撞。

import pygame
import random

# --- constants --- (UPPER_CASE names)

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED   = (255, 0, 0)

CAR_WITDH = 64

# --- functions --- (lower_case names)

def text_objects(text, font):
    text_image = font.render(text, True, BLACK)
    return text_image, text_image.get_rect()

def message_display(text):

    large_font = pygame.font.Font('freesansbold.ttf',40)
    text_image, text_rect = text_objects(text, large_font)
    text_rect.center = screen_rect.center

    screen.blit(text_image, text_rect)
    pygame.display.update()

    pygame.time.delay(2000)

def crash(score):
    message_display(('You dropped a fruit! Your score was: {}').format(score))

def reset_positions():

    car_rect.x = DISPLAY_WIDTH * 0.45
    car_rect.y = DISPLAY_HEIGHT * 0.8

    apple_rect.x = random.randrange(0, DISPLAY_WIDTH)
    apple_rect.y = -600

def game_loop():
    score = 0

    x_change = 0

    apple_speed = 7

    reset_positions()

    clock = pygame.time.Clock()

    while True:

        # --- events ---

        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
            if event.type == pygame.KEYUP:
                if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                    x_change = 0

        # --- updates (without draws) ---

        car_rect.x += x_change
        apple_rect.y += apple_speed

        if car_rect.right > DISPLAY_WIDTH or car_rect.left < 0:
            crash(score)
            reset_positions()
            score = 0

        if apple_rect.bottom > DISPLAY_HEIGHT:
            crash(score)
            reset_positions()
            score = 0

        if car_rect.colliderect(apple_rect):
            score += 1
            car_rect.y -= 10
            apple_rect.x = random.randrange(0, DISPLAY_WIDTH-apple_rect.width)
            apple_rect.y = -600

        # --- draws (without updates) ---

        screen.fill(WHITE)
        screen.blit(apple_image, apple_rect)
        screen.blit(car_image, car_rect)
        pygame.display.update()

        # --- FPS ---

        clock.tick(60)

# --- main --- (lower_case names)

# - init -

pygame.init()

screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
screen_rect = screen.get_rect()

pygame.display.set_caption('Avoid The Falling Objects')

# - objects -

car_image = pygame.image.load('basket.png')
car_rect = car_image.get_rect()

apple_image = pygame.image.load('apple.png')
apple_rect = apple_image.get_rect()

# - mainloop -

game_loop()