我需要帮助来检查我的精灵,邪恶的饼干,是否与边框矩形碰撞。我已经这样做了,但它导致了一个错误



准确的错误信息如下,包括相关的代码。

大家好。我正在尝试制作一些检查是否有邪恶的饼干与边框矩形碰撞的东西,这样我就可以使用它来使邪恶的饼干从上到下弹跳。我不知道如何,通常以这个错误结束:

File "C:UsersUserDownloadsCookie CafeCookie Cafecookie_cafe_4.py", line 215, in <module>
collisioncookie(evil)
File "C:UsersUserDownloadsCookie CafeCookie Cafecookie_cafe_4.py", line 78, in collisioncookie
if pygame.sprite.spritecollide(item,border_bottom, False):
File "C:UsersUserAppDataRoamingPythonPython310site-packagespygamesprite.py", line 1706, in spritecollide
return [
File "C:UsersUserAppDataRoamingPythonPython310site-packagespygamesprite.py", line 1709, in <listcomp>
if default_sprite_collide_func(group_sprite.rect)
AttributeError: 'int' object has no attribute 'rect'
# the_cookie_cafe.py
# Part four  - collisions
import pygame,sys
import random
cookie_collided = False
class Player(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__()
self.sprites = []
self.is_animating = False
self.sprites.append(pygame.image.load("ghostanim1.png").convert_alpha())
self.sprites.append(pygame.image.load("ghostanim2.png").convert_alpha())

self.csprite = 0
self.image = self.sprites[self.csprite]
self.rect = self.image.get_rect()
self.rect.center = [pos_x,pos_y]

def update(self):
if self.is_animating == True:
self.csprite += 0.05
if int(self.csprite) >= len(self.sprites):
self.csprite = 0
self.is_animating = False
# Bug fixing - list cannot go above 0 so image won't lead to error
self.image = self.sprites[int(self.csprite)]
def animate(self):
self.is_animating = True
def move(self,pos_x,pos_y):
self.rect.topleft = (self.rect.x-player_speed_x, self.rect.y-player_speed_y)
self.rect.move_ip(-player_speed_x, -player_speed_y)
class EvilCookie(pygame.sprite.Sprite):
def __init__(self,picture_path,pos_x,pos_y):
global rect
super().__init__()
self.image = pygame.image.load(picture_path)
self.rect = self.image.get_rect()
pygame.draw.rect(screen,(255,255,255),self)
self.rect.center = [pos_x,pos_y]
def update(self):
if cookie_collided == False:     
self.rect.y += 1
else:
pass
def topcollide(self):
if cookie_collided == True:  
self.rect.y -= 2
else:
pass

def bottomcollide(self):
if cookie_collided == True:  
self.rect.y += 2
else:
pass

class EdibleCookie(pygame.sprite.Sprite):
def __init__(self,picture_path,pos_x,pos_y):
super().__init__()
self.image = pygame.image.load(picture_path)
self.rect = self.image.get_rect(center = (screen_width/2,screen_height/2))
self.rect.center = [pos_x,pos_y]
def checkcollision(item):
item.rect.clamp_ip(pygame.display.get_surface().get_rect())
def collisioncookie(item):
if pygame.sprite.spritecollide(item,border_bottom, False):
print("collide bottom")

pygame.init()
clock = pygame.time.Clock()

# Main window - defines window definitons and the screen itself, the "surface"
screen_width = 1280
screen_height = 960
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('Cookie Cafe') # Sets window title
# Images - loads images with pixel accuracy (convert_alpha)
background = pygame.image.load("background.png").convert_alpha()
bigenemy = pygame.image.load("enemybig.png").convert_alpha()
smallenemy = pygame.image.load("enemysmall.png").convert_alpha()
goodenemy = pygame.image.load("goodcookie.png").convert_alpha()
# Rectangles - Creates rectangles which defines where images/characters can be 
cookerrect = pygame.Rect(1000, 550, 200, 200)
backgroundrect = background.get_rect()
backgroundrect.center = (screen_width/2, screen_height/2)
# Sprite group: MOVING SPRITES
moving_sprites = pygame.sprite.Group()
player = Player(300,700)
moving_sprites.add(player)
# Sprite group: Edible Cookies
goodcookie = EdibleCookie("goodcookie.png",random.randrange(600,800),random.randrange(0,800))
goodcookie_group = pygame.sprite.Group()
goodcookie_group.add(goodcookie)
# Sprite group: Evil Cookies
evil = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil2 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil3 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil4 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil5 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil6 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil7 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil8 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil9 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil10 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil11 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil12 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil13 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil14 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil15 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil16 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil17 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil18 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil19 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil20 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil21 = EvilCookie("enemysmall.png",random.randrange(0, screen_width),random.randrange(0,screen_height/2))
evil_cookies = pygame.sprite.Group()
evil_cookies.add(evil,evil2,evil3,evil4,evil5,evil6,evil7,evil8,evil9,evil10,evil11,evil12,evil13,evil14,evil15,evil16,evil17,evil18,evil19,evil20,evil21)
# Speed and movement control
player_speed_x = 0
player_speed_y = 0
screen.blit(background,backgroundrect) # Prints images 
origin_x = 300
origin_y = 700
# Border
border_top = pygame.Rect(0,0,1500,10)
border_bottom = (0,955,1500,10)
# Music
pygame.mixer.init()
pygame.mixer.music.load("music.wav")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play()
# Loop
run = True
while run:
for event in pygame.event.get():
player.move(int(origin_x),int(origin_y))
if event.type == pygame.QUIT: # Checking inputs, here, QUIT is checked (leave game)
pygame.quit() 
sys.exit()
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player_speed_y -= 1
origin_y -= 1
player.animate()
if event.key == pygame.K_DOWN:
player_speed_y += 1
origin_y += 1
player.animate()
if event.key == pygame.K_LEFT:
player_speed_x -= 1
origin_x -= 1
player.animate()
if event.key == pygame.K_RIGHT:
player_speed_x += 1
origin_x += 1
player.animate()
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
player_speed_y += 1
if event.key == pygame.K_DOWN:
player_speed_y -= 1

if event.key == pygame.K_LEFT:
player_speed_x += 1

if event.key == pygame.K_RIGHT:
player_speed_x -= 1

player.move(origin_x,origin_y)
screen.blit(background,backgroundrect)
moving_sprites.draw(screen)
moving_sprites.update()
evil_cookies.draw(screen)
evil_cookies.update()
goodcookie_group.draw(screen)
goodcookie_group.update()
checkcollision(player)
pygame.draw.rect(screen,(255,255,255),border_top)
pygame.draw.rect(screen,(255,255,255),border_bottom)

badhit = pygame.sprite.spritecollide(player,evil_cookies, False)
if badhit:
print("Hit Evil Cookie")

goodhit = pygame.sprite.spritecollide(player,goodcookie_group, False)
if goodhit:
print("Hit GoodCookie")

collisioncookie(evil)

pygame.display.flip()
clock.tick(60) # Keeps FPS at 60 to prevent lag or un-playable game

参见如何在pygame中检测碰撞?pygame.sprite.spritecollide的第二个参数需要是pygame.sprite.Group。然而,border_bottom是一个矩形。用pygame.Rect.colliderect代替pygame.sprite.spritecollide:

def collisioncookie(item):
if item.rect.colliderect(border_bottom):
print("collide bottom")

相关内容

  • 没有找到相关文章

最新更新