【问题标题】:Pygame spritecollide not removing spritesPygame spritecollide 不删除精灵
【发布时间】:2018-06-20 21:06:54
【问题描述】:

请帮忙。

我是 pygame 的新手。我无法弄清楚为什么我的碰撞检测不起作用。我正在使用 spritecollide,只是看不到为什么精灵没有从 sprites_list 中删除。我正在使用 pygame 绘制的图像和圆圈,这可能与它有关吗?

游戏应该是一个非常简单的收集游戏,玩家精灵可以使用箭头键四处移动并导致红色圆圈消失。非常感谢您在此提供任何帮助。

import pygame, sys
from pygame.locals import *
import random


class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("king_black.png")
        self.rect = self.image.get_rect()

class CircleSprite(pygame.sprite.Sprite):
    def __init__(self, colour, width, height):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width, height])
        self.rect = self.image.get_rect()
        self.image.set_colorkey((0,0,0))
        pygame.draw.ellipse(self.image, colour, [0, 0, width, height])


#initialise pygame
pygame.init()

#Setup game clock
FPS = 30 #Frames per second
clock = pygame.time.Clock()

#Setup display
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Hello world!")

#Setup colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

rect_x = 200
rect_y = 150
rect_width = 100
rect_height = 50

player = Player()

#Create a sprite group
sprites_list = pygame.sprite.Group()

#Circle list
circle_list = pygame.sprite.Group()

for i in range(10):
    circle = CircleSprite(RED, 40, 40)

    # Set a random location for the circle
    circle.rect.x = random.randrange(400)
    circle.rect.y = random.randrange(300)

    # Add the circle to the list of objects
    sprites_list.add(circle)

sprites_list.add(player)

#Main game loop
while True:

  for event in pygame.event.get():
    if event.type == QUIT:
      pygame.quit()
      sys.exit()

  #Looks for pressed keys
  keys_pressed = pygame.key.get_pressed()

  #Responds to most recent key pressed
  if keys_pressed[K_LEFT]:
    rect_x -= 5
  if keys_pressed[K_RIGHT]:
    rect_x += 5
  if keys_pressed[K_UP]:
    rect_y -= 5
  if keys_pressed[K_DOWN]:
    rect_y += 5

  #Collision detection
  sprite_hits = pygame.sprite.spritecollide(player, sprites_list, True)

  #Draw screen background
  DISPLAYSURF.fill((WHITE))

  #Draw circles to display
  sprites_list.draw(DISPLAYSURF)

  #Blit image to display and flip it.
  DISPLAYSURF.blit(player.image, (rect_x, rect_y))
  pygame.display.flip()

  clock.tick(FPS)

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    如果您打印玩家的矩形或在矩形 (DISPLAYSURF.blit(player.image, player.rect)) 上对图像进行 blit,您会看到它永远不会移动,只会停留在默认坐标 (0, 0) 上。由于rect 用于碰撞检测,玩家无法与圆形精灵发生碰撞。

    您必须每帧更新矩形的位置或直接增加player.rect.xplayer.rect.y

    if keys_pressed[K_LEFT]:
        rect_x -= 5
    if keys_pressed[K_RIGHT]:
        rect_x += 5
    if keys_pressed[K_UP]:
        rect_y -= 5
    if keys_pressed[K_DOWN]:
        rect_y += 5
    player.rect.topleft = (rect_x, rect_y)
    

    【讨论】:

    • 辉煌。现在完全有道理。感谢您为我解释并修复它。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多