Pygame:处理导入图像的圆形碰撞

时间:2019-02-02 07:05:40

标签: python pygame collision circular-dependency

ltcImg我正在做一个简单的游戏,其中一辆汽车躲避了一些物体,这些物体我都是使用来自互联网的图片导入的。配置圆形匹配盒的最佳方法是什么,当它进入我的汽车的方盒时会很好地工作? CarImg 现在,我将它们放入长度和宽度参数中,因为这是我真正知道的全部方法。由于某些原因,我的点击框在“ ltc”圆圈的左侧结束了游戏的良好状态,但是当我越过右侧的圆圈边界时,即使我实际上并未“点击”圆圈,导致我崩溃并结束游戏。

我尝试调整ltcw和ltch来查看它是否使它更好,但是总体看来我只能使它变得更糟。在遇到方形点击框时使用导入的图片时,我找不到任何在线引用专门针对圆形点击框的内容。

import pygame
import time
import random

pygame.init()

display_width = 800
display_height = 600
car_width = 60
car_height = 113

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

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('TRX: FuD Racer')
clock = pygame.time.Clock()
carImg = pygame.image.load('RaceCar2.png')
ltcImg = pygame.image.load('LtcLogo.png')
xlmImg = pygame.image.load('Stell_Logo.png')


def car(x, y):
    gameDisplay.blit(carImg, (x, y))


def ltc(x, y):
    gameDisplay.blit(ltcImg, (x, y))


def stell(x, y):
    gameDisplay.blit(xlmImg, (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', 75)
    (textSurf, textRect) = text_objects(text, largeText)
    textRect.center = (display_width / 2, display_height / 2)
    gameDisplay.blit(textSurf, textRect)

    pygame.display.update()
    time.sleep(3)
    game_loop()


def crash():
    message_display('You Sold Your Tron!')


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

# I should adjust each coins speed in game in accordance with their transaction speeds

    ltc_startx = random.randrange(0, display_width)
    ltc_starty = -300
    ltc_speed = 2
    ltcw = 222
    ltch = 200

    stell_startx = random.randrange(0, display_width)
    stell_starty = -600
    stell_speed = 6
    stellw = 300
    stellh = 450

    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 = -6
                elif event.key == pygame.K_RIGHT:
                    x_change = 6

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -6
                elif event.key == pygame.K_DOWN:
                    y_change = 6

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key \
                    == pygame.K_DOWN:
                    y_change = 0

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key \
                    == pygame.K_RIGHT:
                    x_change = 0

        x += x_change
        y += y_change

        gameDisplay.fill(WHITE)

        ltc(ltc_startx, ltc_starty)
        ltc_starty += ltc_speed
        car(x, y)

        stell(stell_startx, stell_starty)
        stell_starty += stell_speed
        car(x, y)

        if x > display_width - car_width or x < -15:
            crash()

        if ltc_starty > display_height:
            ltc_starty = 0 - ltch
            ltc_startx = random.randrange(0, display_width - ltcw)

        if stell_starty > display_height:
            stell_starty = 0 - stellh
            stell_startx = random.randrange(0, display_width - stellw)

        if y < 0:
            crash()

        if y < ltc_starty + ltch:
            print 'y crossover'

            if x > ltc_startx and x < ltc_startx + ltcw or x \
                + car_width > ltc_startx and x + car_width < ltc_startx \
                + ltcw:
                print 'x crossover'
                crash()

        pygame.display.update()

        clock.tick(60)

game_loop()
pygame.quit()
quit()

0 个答案:

没有答案