Python 如何更改我的精灵之一的点击框?

Python 如何更改我的精灵之一的点击框?,python,python-3.x,pygame,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

因此,当游戏开始时,将有框供用户躲避,但目前,该对象的命中框有点不对劲。。例如,当你将船移过一个盒子时,它会被记录为命中,并在游戏结束时结束

这是一项软件设计和开发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()
#此程序由Tadiwa Mooyo创建
#更多的汽车游戏已经完成,现在有了用户可以“闪避”的盒子,当盒子被击中时,它会显示碰撞信息
#该计划于2019年2月22日启动
导入pygame
导入时间
随机输入
pygame.init()
显示宽度=1200
显示高度=700
黑色=(0,0,0)
白色=(255255)
红色=(255,0,0)
轿厢宽度=100
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题('TheGreatSpace Escape!')
clock=pygame.time.clock()
carImg=pygame.image.load('Ships\u directory\\Green\u black\u ship.png')
def道奇事件(计数):
font=pygame.font.SysFont(无,25)
text=font.render(“分数:+str(计数),真,白色)
blit(文本,(0,0))
定义事物(thingx、thingy、thingw、thingh、颜色):
pygame.draw.rect(游戏显示,颜色,[thingx,thingy,thingw,thingh])
def汽车(x,y):
游戏显示.blit(carImg,(x,y))
def text_对象(文本、字体):
textSurface=font.render(文本、真、白)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text\u对象(text,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(2)
博弈(循环)
def crash():
信息显示(“您崩溃了”)
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
游戏显示。填充(黑色)
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text_对象(“有点疯狂”,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.display.update()
时钟滴答(15)
def game_loop():
x=(显示宽度*0.45)
y=(显示高度*0.5)
x_变化=0
thing\u startx=random.randrange(0,显示宽度)
事情开始=-600
速度=4
物体宽度=200
物体高度=200
减淡=0
gameExit=False
不退出游戏时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-10
如果event.key==pygame.K_RIGHT:
x_变化=10
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
x_变化=0
x+=x_变化
游戏显示。填充(黑色)
#东西(东西,东西,东西,东西,东西,颜色)
事物(事物开始、事物开始、事物宽度、事物高度、白色)
物体开始速度+=物体速度
汽车(x,y)
闪避的事物(闪避)
如果x>显示宽度-轿厢宽度或x<0:
崩溃()
如果事物开始>显示高度:
物体起点=0-物体高度
thing\u startx=random.randrange(0,显示宽度)
减淡+=1
速度+=0.5
物体宽度+=(减淡*1.4)
如果y<物体起点+物体高度:
打印('y交叉')
如果x>thing\u startx和xthing\u startx和x+car\u width
使用以下方法简化代码并实施碰撞测试:

e、 g

things\u rect=pygame.rect(things\u startx,things\u starty,things\u width,things\u height)
car\u rect=pygame.rect(x,y,*carImg.get\u size())
如果汽车发生碰撞(物体发生碰撞):
崩溃()

如果您想实现“自己的”测试,即检查两个矩形是否相交,那么您必须检查矩形在两个维度中是否“重叠”

如果
x1
,则两个范围[
x1
x1+w2
]重叠

因此,矩形的相交测试可以按如下方式执行:

car\u w,car\u h=carImg.get\u size()
如果(东西开始Tx