如何更改其中一个精灵的点击框?

时间:2019-06-21 06:15:16

标签: python python-3.x pygame

因此,当游戏开始时,会有供用户躲避的盒子,但是当前,对象的命中框有些偏离。例如,当您将飞船移过盒子时,它会注册为命中,以游戏结束。

这是针对软件设计和开发HSC评估任务的 我不太确定如何解决此问题

这是代码!

#This program was created by Tadiwa Mooyo
#more of the car game has been worked on, now there are boxes for the user to "dodge" and it will display the crash message when the boxes are hit
#This program was started on the 22/02/2019
import pygame
import time
import random

pygame.init()

display_width = 1200
display_height = 700
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

car_width = 100

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Great Space Escape!')
clock = pygame.time.Clock()
carImg = pygame.image.load('Ships_directory\\Green_black_ship.png')

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Score: "+str(count), True, white)
    gameDisplay.blit(text,(0,0))


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

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

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

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 crash():
    message_display("You Crashed")

def game_intro():

    intro = True

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

        gameDisplay.fill(black)
        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("A bit Racey", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)
        pygame.display.update()
        clock.tick(15)




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

    x_change = 0

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

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

            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(black)


        #things(thingx, thingy, thingw, thingh, color)
        things(thing_startx, thing_starty, thing_width, thing_height, white)
        thing_starty += thing_speed
        car(x,y)
        things_dodged(dodged)

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

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

        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+thing_width:
                print('x crossover')
                crash()


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

game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:1)

使用pygame.Rect简化代码,并通过.colliderect()实施碰撞测试:

例如

things_rect = pygame.Rect(thing_startx, thing_starty, thing_width, thing_height)
car_rect    = pygame.Rect(x, y, *carImg.get_size())

if car_rect.colliderect(things_rect):
    crash()

如果要实施“自己的”测试(该测试检查2个矩形是否相交),则必须检查两个维度上的矩形是否“重叠”。

如果x1,则两个范围[x1+w1x2]和[x2+w2x1 < x2+w2 and x2 < x1+w1]重叠。

因此可以按如下方式对矩形进行交集测试:

car_w, car_h = carImg.get_size()
if (thing_startx < x+car_w and x < thing_startx+thing_width and 
    thing_starty < y+car_h and y < thing_starty+thing_height):
    crash()