Pygame中图像与矩形之间的碰撞

时间:2014-05-05 06:25:41

标签: python pygame

我在处理PNG和用户控制的矩形对象之间的冲突时遇到了一些问题。如果矩形与墙壁碰撞(另一个矩形)没有问题,但是当我尝试给PNG提供与墙壁相同的功能时,我收到错误。

sprite_image_filename = 'tails.PNG'
import pygame
from pygame.locals import *


speed = [7, 4]
e = 1.001
position=(50,50)
sprite = pygame.image.load(sprite_image_filename).convert_alpha()

rect = (sprite.get_rect())

wall1 = Block(sort,20, 400 )
wall1.set_position(0,200)


block_group.add(a_block, wall1)    

running=True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    if event.type == pygame.MOUSEMOTION :
        mouse_position = pygame.mouse.get_pos()
        a_block.set_position(mouse_position[0],mouse_position[1])
    clock.tick(frames_per_second)
    window.fill(a)
    #here
    rect = rect.move(speed)

    if rect.left < 0 or rect.right > 380:
        speed[0] = -speed[0] * e
        pygame.display.update()
    if rect.top < 0 or rect.bottom > 370:
        speed[1] = -speed[1] * e
        pygame.display.update()
    window.blit(sprite, rect)
    pygame.display.update()
    #here



    if pygame.sprite.collide_rect(a_block, sprite) or pygame.sprite.collide_rect(a_block,wall1):
        time_string = "Du overlevede {} sekunder".format(pygame.time.get_ticks()/1000)
        text = font.render(time_string, True, sort)

        pygame.display.update()
        pygame.time.wait(5000)
        running = False


    block_group.draw(window)
    pygame.display.update()#opdater skærmen



pygame.quit ()

错误的追溯是:

  

追踪(最近一次通话):         文件&#34; C:\ Python33 \ eskappa1.py&#34;,第105行,in           如果pygame.sprite.collide_rect(a_block,sprite)或pygame.sprite.collide_rect(a_block,wall1)

  File "C:\Python33\lib\site-packages\pygame\sprite.py", line 1300, in collide_rect
    return left.rect.colliderect(right.rect)
AttributeError: 'pygame.Surface' object has no attribute 'rect'
sprite_image_filename = 'tails.PNG'
sprite2_image_filename = 'block 2.PNG'
sprite3_image_filename = 'block 3.PNG'
sprite4_image_filename = 'block 4.PNG'

import pygame
from pygame.locals import *
r=(255,0,0)
b=(0,0,255)
sort =(0,0,0)
class Block(pygame.sprite.Sprite):
    def __init__(self, color=b, width= 40, height= 40):
        super(Block, self).__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.origin_x=self.rect.centerx
        self.origin_y=self.rect.centery

    def set_position(self, x, y):
        self.rect.x=x-self.origin_x
        self.rect.y=y-self.origin_y
pygame.init()

pygame.display.set_caption("EsKappa!")

window_size =window_width, window_height = 400, 400
window = pygame.display.set_mode(window_size)

a=(0,201,0)


clock = pygame.time.Clock()
frames_per_second = 30

block_group = pygame.sprite.Group()

a_block= Block(b)
a_block.set_position(window_width/2 -30, window_height/2)

speed = [7, 4]
e = 1.01
position=(50,50)
sprite = pygame.image.load(sprite_image_filename).convert_alpha()
sprite2= pygame.image.load(sprite2_image_filename).convert_alpha()
sprite3= pygame.image.load(sprite2_image_filename).convert_alpha()
sprite4= pygame.image.load(sprite2_image_filename).convert_alpha()
rect = (sprite.get_rect())
rect2 = sprite2.get_rect()
rect3 = sprite2.get_rect()
rect4 = sprite2.get_rect()



wall1 = Block(sort,20, 400 )
wall1.set_position(0,200)
wall2= Block(sort,400, 20 )
wall2.set_position(200,0)
wall3=Block(sort,20, 400 )
wall3.set_position(400,200)
wall4=Block(sort,400, 20 )
wall4.set_position(200,400)


block_group.add(a_block, wall1,wall2,wall3,wall4)


b2=(270, 70)
b3=(80,20)
b4=(70, 250)

h=pygame.time.get_ticks()/1000
font = pygame.font.SysFont("Times New Roman", 20)
font2 = pygame.font.SysFont("Times New Roman", 50)
text = font.render("Time", False, sort)
#lukke funktion
running=True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    if event.type == pygame.MOUSEMOTION :
        mouse_position = pygame.mouse.get_pos()
        a_block.set_position(mouse_position[0],mouse_position[1])
    clock.tick(frames_per_second)
    window.fill(a)
    #here
    rect = rect.move(speed)

    if rect.left < 0 or rect.right > 380:
        speed[0] = -speed[0] * e
        pygame.display.update()
    if rect.top < 0 or rect.bottom > 370:
        speed[1] = -speed[1] * e
        pygame.display.update()
    window.blit(sprite, rect)
    #window.blit(sprite2, rect)
    #window.blit(sprite3, rect)
    #window.blit(sprite4, rect)
    pygame.display.update()
    #here



    if  pygame.sprite.collide_rect(a_block, wall1) or pygame.sprite.collide_rect(a_block, wall2) or pygame.sprite.collide_rect(a_block, wall3) or pygame.sprite.collide_rect(a_block, wall4):
        time_string = "Du overlevede {} sekunder".format(round(pygame.time.get_ticks()/1000 -0.225))
        text = font.render(time_string, True, sort)
        window.blit(text, (window_width/2-100, window_height/2-100))
        if pygame.time.get_ticks()/1000>10:
            time_string1 = "Flot!"
            text = font2.render(time_string1, True, sort)
            window.blit(text, (20, 20))
        else:
            time_string2 = "Ikke så godt :c"
            text = font2.render(time_string2, True, sort)
            window.blit(text, window.blit(text, (20, 20)))
        pygame.display.update()
        pygame.time.wait(5000)
        running = False


    block_group.draw(window)
    pygame.display.update()#opdater skærmen



pygame.quit ()

1 个答案:

答案 0 :(得分:0)

错误消息很明确。

您致电left.rect.colliderect(right.rect),但rightpygame.Surface且没有属性rect

请注意您在rect课程中使用Block属性的方式:

class Block(pygame.sprite.Sprite):
    def __init__(self, color=b, width= 40, height= 40):
        super(Block, self).__init__()
        ...
        self.rect = self.image.get_rect()
        ...

pygame.Surface本身没有rect属性。

要么只将对象传递给包含rect属性的函数,要么使用多态并在get_rect()类中添加Block函数,并在函数中使用该函数碰撞检测,以便它适用于BlockSurface个对象。