- 此问题在此处已有答案**:
pygame.sprite.groupcollide() does not work when trying to implement collision in pygame [duplicate](1个答案)
Permanently delete sprite from memory Pygame(1个答案)
Pygame - getting a sprite to cycle through images in a class, and assign a hitbox(1个答案)
2天前关闭。
我如何检查bullet_grou
和enemy_group
之间的碰撞,如果他们碰撞,我希望他们消失。我已经看了一些模拟的问题,但没有一个帮助我。thenks所有的帮助。这个游戏应该是一个模拟的游戏小行星。#我不知道有什么更多的详细说明在这里BT它不让我张贴这个问题,如果我不写一些超过她对不起
import pygame , sys
from random import randint
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50,50))
self.image.fill((255,255,255))
self.rect = self.image.get_rect(center = (screen_width/2,screen_height/2))
def update(self):
self.rect.center = pygame.mouse.get_pos()
def create_bullet(self):
return Bullet(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1])
def create_enemy(self):
return Enemy(randint(50,screen_width-50),-200)
class Bullet(pygame.sprite.Sprite):
def __init__(self,pos_x,pos_y):
super().__init__()
self.image = pygame.Surface((5,25))
self.image.fill((255,0,0))
self.rect = self.image.get_rect(center = (pos_x,pos_y))
def update(self):
self.rect.y -= 5
if self.rect.y <= -200:
self.kill()
class Enemy(pygame.sprite.Sprite):
def __init__(self,pos_x,pos_y):
super().__init__()
self.image = pygame.Surface((50,50))
self.image.fill((255,140,0))
self.rect = self.image.get_rect(center = (pos_x,pos_y))
## def __init__(self):
## super().__init__()
## self.image = pygame.Surface((50,50))
## self.image.fill((255,128,128))
## self.rect = self.image.get_rect(center = (randint(0,screen_width),-200))
##
def update(self):
self.rect.y += 5
if self.rect.y >= screen_height + 200:
self.kill()
pygame.init()
clock = pygame.time.Clock()
screen_width, screen_height = 800, 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.mouse.set_visible(False)
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer,150)
player = Player()
player_group = pygame.sprite.Group()
player_group.add(player)
bullet_group = pygame.sprite.Group()
enemy_group = pygame.sprite.Group()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
# enemy_group.add(player.create_enemy())
bullet_group.add(player.create_bullet())
if event.type == obstacle_timer:
if randint(0,2):
enemy_group.add(player.create_enemy())
## if Enemy.rect.coliderect(Bullet.rect):
## print('colision')
screen.fill((30,30,30))
bullet_group.draw(screen)
bullet_group.update()
# collide_enemy_bullet = pygame.sprite.spritecollide(bullet_group, Enemy , False) #HERE IS THE PROBLEM
collide_player_enemy = pygame.sprite.spritecollide(player, enemy_group, False)
for s in collide_player_enemy:
pygame.draw.rect(screen, (255, 255, 255), s.rect, 5, 1)
print('collide_player_enemy')
## for x in collide_enemy_bullet:
## pygame.draw.rect(screen, (255, 255, 255), x.rect, 5, 1)#HERE ALSO
## print('collide_enemy_bullet')
player_group.draw(screen)
player_group.update()
enemy_group.draw(screen)
enemy_group.update()
pygame.display.flip()
clock.tick(240)
1条答案
按热度按时间jgwigjjp1#
pygame.sprite.groupcollide()应该可以做到这一点。你传入第一组和第二组,以及你是否希望每个组在碰撞时消失。
Pygame文档:https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.groupcollide