Pygame:线路无法与雷坦格碰撞

2024-10-03 21:26:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个程序,看起来像雷达地图。我画了一条旋转360度的线,雷达上有一个“可怕”的图像。我试图找出一条直线和一个矩形碰撞,但是我得到了这个错误。属性错误:'pygame.Rect游戏'object没有'get-rect'属性

基本上,我的问题是如何让一条绘制的线与一个blited图像发生碰撞?在

谢谢。在

import pygame
import math
from colors import *
from pygame.locals import *
pygame.init()

bkrd = pygame.image.load("radar//radar2.jpg")
bkrdrect = bkrd.get_rect()
bogey = pygame.image.load("radar//bogey2.png")
bogeyrect = bogey.get_rect()


window_size = window_width, window_height = 1101,822
window = pygame.display.set_mode( window_size, pygame.RESIZABLE )
pygame.display.set_caption( "Radar" )


clock = pygame.time.Clock()
FPS = 60
pygame.time.set_timer(USEREVENT + 1, 100)

pygame.key.set_repeat(0,0)
window.blit(bkrd,bkrdrect)          
pygame.display.update()


angle = 0
eta = 0
bogey_one_x = 532
bogey_one_y = 210



def mainloop():
    global angle,eta,bogey_one_x,bogey_one_y
    while True:

        angle +=1
        eta +=1

        bogey_one_x += .05
        bogey_one_y +=.02

        window.blit(bkrd,bkrdrect)
        window.blit(bogey,(bogey_one_x,bogey_one_y))

        radar = (685,413)
        radar_len = 253

        x = radar[0] + math.cos(math.radians(angle)) * radar_len
        y = radar[1] + math.sin(math.radians(angle)) * radar_len

        line = pygame.draw.line(window, (green), radar, (x,y), 1)
        linerect = line.get_rect()

        if linerect.colliderect(bogeyrect):
            print "collide"

        pygame.display.update()

        for event in pygame.event.get():
            if ( event.type == pygame.QUIT ) or \
            ( event.type == pygame.KEYDOWN and \
            ( event.key == pygame.K_ESCAPE) ):
                running = False
                pygame.quit()

mainloop()

Tags: rectimporteventgetdisplaymathwindowone