小组没有属性矩形,我不知道该怎么办
我正在尝试编程流量模拟,并需要Pygame碰撞检测。但是,一旦我运行该程序(尚未完成),它告诉我我在car_up上有一个属性错误,因为它没有属性rect。有人可以告诉我为什么这是这样,我该怎么做才能改善它?是的,我一直在尝试自己弄清楚,但没有成功。
import pygame
import random
import threading
from pygame.locals import (QUIT, KEYDOWN, K_ESCAPE)
# Define constants for the screen width and height
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 400
grey=[90 , 90 , 90]
green =[125, 255, 50]
spawnrate_up=1500
spawnrate_down=800
dimentions=[75,35]
#CLASSES
#create car
class Car_up (pygame.sprite.Sprite):
def __init__(self, green, dimentions):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface(dimentions)
self.image.fill(green)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.x = random.randint(-100, -20)
self.rect.y = 150
self.speedx= 10
self.speedy= 0
# move car and delete once out of screen
def update(self):
merge=random.randint(750,1000)
if self.rect.left > SCREEN_WIDTH:
self.kill()
if self.rect.colliderect(car_down.rect):
car_up.speed.x=car_up.speed.y=0
elif self.rect.centery<=266 and self.rect.centerx>merge:
self.speedx=10
self.speedy=3
else:
self.speedx=10
self.speedy=0
self.rect.move_ip(self.speedx, self.speedy)
class Car_down(pygame.sprite.Sprite):
def __init__(self, green, dimentions):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface(dimentions)
self.image.fill(green)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.x = random.randint(-100,-20)
self.rect.y = 250
self.speedx= 10
self.speedy= 0
# move car and delete once out of screen
def update(self):
self.speedx=10
self.speedy=0
self.rect.move_ip(self.speedx, self.speedy)
if self.rect.left > SCREEN_WIDTH:
self.kill()
pygame.init()
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock=pygame.time.Clock()
#create custom event
ADDCAR_down= pygame.USEREVENT + 1
ADDCAR_up= pygame.USEREVENT + 2
pygame.time.set_timer(ADDCAR_up, spawnrate_up )
pygame.time.set_timer(ADDCAR_down, spawnrate_down )
#create car
car_up= pygame.sprite.Group()
car_down= pygame.sprite.Group()
car_down.add(Car_down(green,dimentions))
car_up.add(Car_up(green,dimentions))
#infinite loop
run = True
#MAIN
while run:
# Look at every event in the queue
for event in pygame.event.get():
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
run = False
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
run = False
elif event.type == ADDCAR_down:
# Create the new CAR and add it to sprite groups
new_car_down = Car_down(green,dimentions)
car_down.add(new_car_down)
elif event.type == ADDCAR_up:
new_car_up = Car_up(green,dimentions)
car_up.add(new_car_up)
#grey screen
screen.fill(grey)
# Draw the car on the screen
car_up.draw(screen)
car_up.update()
car_down.draw(screen)
car_down.update()
pygame.display.flip()
clock.tick(25)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
car_down
是 。但是 组中的对象具有rect
属性。您需要使用代码> 检测A sprite 的碰撞以及 group 中的对象另请参见
car_down
is apygame.sprite.Group
so obviously it doesn't have arect
attribute. However thepygams.sprites.Sprite
object in the Group have arect
attribute. You need to usepygame.sprite.spritecollide
to detect collision of a Sprite and the objects in a Group:if self.rect.colliderect(car_down.rect):
See also How do I detect collision in pygame?