1

在这个游戏中,我正在创建一个图像由玩家使用箭头(赛车)和两个图像(香蕉)控制,它们以定义的方向和速度移动(现在放慢了速度,所以我可以更好地看到正在发生的事情)但是他们的起点是随机的。目标是让汽车避开这些障碍物并到达终点线,只有在向前行驶时,即使在明显没有接触的情况下,也无法在没有碰撞路段的情况下通过这些香蕉。这很棘手,因为事情在 X 轴上移动。请帮我。这是两个图像之间碰撞的代码段:

if x + car_width >= banX:
            print('ycollision')
            if y + car_width >= banX and y + car_width <= banX + ban_height or ybottom + car_width >= banX and ybottom + car_width <= banX + ban_height:
                print('xcollision')
                crash()

这是整个代码:

import pygame
import random
import time
pygame.init()

#Colors
black = (0,0,0)
white = (255,255,255)
grey = (96,96,96)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

#Setting and variables
display_width = 1570
display_height = 450
car_width = 98
car_height = 66
clock = pygame.time.Clock()
wn = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('My own game')



#racecar
carImg = pygame.image.load('myOwnRGame.png')
Xchange = 0
Ychange = 0
y = 192
x = 10
def car(x,y):
    wn.blit(carImg, (x,y))

#finish line
finish_line = pygame.image.load('myOwnFinishreal.png')
Xfin = 1480
Yfin = 0
def finish():
    wn.blit(finish_line, (Xfin, Yfin))
#Crashing and wining
def textObjects(text, font):
    textSurface = font.render(text, True, red)
    return textSurface, textSurface.get_rect()

def displayMessage(text):
    textFont1 = pygame.font.Font('freesansbold.ttf', 32)
    textSurf, textRect = textObjects(text, textFont1)
    textRect.center = ((display_width/2),(display_height/2))
    while True:
        wn.blit(textSurf, textRect)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.display.quit()
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_x:
                    pygame.display.quit()
                    pygame.quit()
                if event.key == pygame.K_SPACE:
                    gameLoop()
        pygame.display.update()

def crash():
    displayMessage('You crashed!Press X to quit, _SPACE_ to restart!')
def win():
    displayMessage('Bravo! You are the best car runner! Press X to quit and _SPACE_ to restart.')

#Banana widht48 height26
banImg = pygame.image.load('myOwnBanana.png')
ban_width = 48
ban_height = 26
def banana(banX, banY):
    wn.blit(banImg, (banX, banY))
def banana2(banX, banY2):
    wn.blit(banImg, (banX, banY2))

#Game loop
def gameLoop():
    y = 192
    x = 10
    Xchange = 0
    Ychange = 0

    banX = 1600

    banY = random.randrange(-5, (display_height - 21))
    banY2 = random.randrange(-5, (display_height - 21))

    ban_change = -3
    alive = True
    losing = True
    while alive and losing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.display.quit()
                pygame.quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_x:
                    pygame.display.quit()
                    pygame.quit()
                if event.key == pygame.K_UP:
                    Xchange = 0
                    Ychange = 0
                    Xchange = 2.5

                elif event.key == pygame.K_LEFT:
                    Xchange = 0
                    Ychange = 0
                    Ychange = -3

                elif event.key == pygame.K_RIGHT:
                    Xchange = 0
                    Ychange = 0
                    Ychange = 3
                elif event.key == pygame.K_DOWN:
                    Xchange = 0
                    Ychange = 0
                    Xchange = -3
        #Borders          
        if y <= -15 or y >= display_height - 15:
            Xchange = 0
            Ychange = 0
            ban_change = 2.5
            crash()
        if x >= display_width:
            Xchange = 0
            Ychange = 0
            ban_change = 2.5
            win()
        if x <= 0:
            x = 10
        #Banana spon
        if banX <= 0:
            banY = random.randrange(-5, (display_height - 21))
            banY2 = random.randrange(-5, (display_height - 21))
            banX = 1540

        x += Xchange
        y += Ychange
        ybottom = y + car_height
        #Banana collision   
        if x + car_width >= banX:
            print('ycollision')
            if y + car_width >= banX and y + car_width <= banX + ban_height or ybottom + car_width >= banX and ybottom + car_width <= banX + ban_height:
                print('xcollision')
                crash()
        wn.fill(grey)
        finish()
        car(x, y)
        banana(banX, banY)
        banana2(banX, banY2)
        banX += ban_change
        pygame.display.update()
        clock.tick(60)

    pygame.display.quit()   
    pygame.quit()
gameLoop()

问题摘要:我怎样才能让香蕉和汽车发生碰撞,但仍然能够通过移动汽车来躲避碰撞?

4

1 回答 1

1

我建议在调用之前绘制对象crash,以显示碰撞时对象的实际位置。请注意,对象已被移动,但在崩溃前未重绘

wn.fill(grey)
finish()
car(x, y)
banana(banX, banY)
banana2(banX, banY2)   

if # collision test
    crash()

要评估矩形(x1, y1, w1, h1)和 _(x2, y2, w2, h2) 是否相交,您必须评估矩形是否在两个维度上重叠:

collide = x1 < x2+w2 and x2 < x1+w1 and y1 < y2+h2 and y2 < y1+h1

对于您的代码,ma 如下所示:

if x < banX + ban_width and banX < x + car_width:
    if y < banY + ban_height and banY < y + car_height:  
        crash()

无论如何,我建议pygame.Rectcolliderect()进行碰撞测试。pygame.Surface从对象中获取矩形carImg并按. 例如:banImgget_rect()

car_rect = carImg.get_rect(topleft = (x, y))
ban_rect = banImg.get_rect(topleft = (banX, banY))
ban2_rect = banImg.get_rect(topleft = (banX, banY2))

if car_rect.colliderect(ban_rect) or car_rect.colliderect(ban2_rect):
    crash()
于 2020-05-24T06:47:50.403 回答