我遇到群组碰撞问题。我正在用枪和游戏制作游戏
老鼠是十字准线。当我触摸enemy(group)
时,直到我触摸唯一时才检测到碰撞
corner(0,0)
中的敌人。我希望能够与屏幕上移动的所有敌人发生碰撞。一世
我使用pygame.sprite.spritecollideany()
作为碰撞代码,我使用sys.exit()
作为输出。
import pygame
import random
import sys
import os
from pygame.locals import *
#Initialize pygame
pygame.init()
#Window properties
window_width = 1366
window_height = 768
window = pygame.display.set_mode((window_width,window_height),pygame.FULLSCREEN)
pygame.display.set_caption("Sniper")
#Clock
clock = pygame.time.Clock()
#Movement
moveX = 1
moveY = 0
#Colors
black = (0,0,0)
white = (255,255,255)
blue = (0,0,64)
#Sprite groups
enemies = pygame.sprite.Group()
cursors = pygame.sprite.Group()
#Enemy
class Enemy(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = 25
self.height = 100
self.image = pygame.image.load('graphic\\sprite\\spr_enemy.png').convert()
self.rect = self.image.get_rect()
def update(self):
window.blit(self.image,(self.x,self.y))
#Player
class Player(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = 100
self.height = 25
self.image = pygame.image.load('graphic\\sprite\\spr_player.png').convert()
self.rect = self.image.get_rect()
def update(self):
window.blit(self.image,(self.x,self.y))
#Cursor
class Cursor(pygame.sprite.Sprite):
def __init__(self):
pygame.mouse.set_visible(False)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('graphic\\cursor\\crosshair.png').convert()
self.rect = self.image.get_rect()
def update(self,enemies):
self.rect.center = pygame.mouse.get_pos()
if (pygame.sprite.spritecollideany(self,enemies)):
sys.exit()
#Define sprites
player = Player(500,100)
cursor = Cursor()
cursors.add(cursor)
enemy = Enemy(0,0)
#Gameloop
gameLoop = True
while gameLoop:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
gameLoop = False
#Keydown
if (event.type == pygame.KEYDOWN):
#Space key
if (event.key == pygame.K_SPACE):
x = random.randint(0,0)
y = random.randint(0,668)
enemies.add(Enemy(x,y))
#Escape key
if (event.key == pygame.K_ESCAPE):
sys.exit()
#Window color
window.fill(blue)
#Updating player
player.update()
#Updating enemy
for enemy in enemies:
enemy.x += moveX
enemy.update()
enemies.update()
enemies.draw(window)
#Updating cursor
cursors.update(enemies)
cursors.draw(window)
#FPS
clock.tick(60)
pygame.display.flip()
pygame.quit()
答案 0 :(得分:0)
您需要将pygame.sprite.spritecollideany()
命令的位置从更新功能更改为while循环,删除命令周围的parantheses,并将命令更改为pygame.sprite.spriteCollide()
,使其如下所示:
import pygame
import random
import sys
import os
from pygame.locals import *
#Initialize pygame
pygame.init()
#Window properties
window_width = 1366
window_height = 768
window = pygame.display.set_mode((window_width,window_height),pygame.FULLSCREEN)
pygame.display.set_caption("Sniper")
#Clock
clock = pygame.time.Clock()
#Movement
moveX = 1
moveY = 0
#Colors
black = (0,0,0)
white = (255,255,255)
blue = (0,0,64)
#Sprite groups
enemies = pygame.sprite.Group()
cursors = pygame.sprite.Group()
#Enemy
class Enemy(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = 25
self.height = 100
self.image = pygame.image.load('graphic\\sprite\\spr_enemy.png').convert()
self.rect = self.image.get_rect()
def update(self):
window.blit(self.image,(self.x,self.y))
#Player
class Player(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = 100
self.height = 25
self.image = pygame.image.load('graphic\\sprite\\spr_player.png').convert()
self.rect = self.image.get_rect()
def update(self):
window.blit(self.image,(self.x,self.y))
#Cursor
class Cursor(pygame.sprite.Sprite):
def __init__(self):
pygame.mouse.set_visible(False)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('graphic\\cursor\\crosshair.png').convert()
self.rect = self.image.get_rect()
def update(self):
self.rect.center = pygame.mouse.get_pos()
#Define sprites
player = Player(500,100)
cursor = Cursor()
cursors.add(cursor)
enemy = Enemy(0,0)
#Gameloop
gameLoop = True
while 1:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
gameLoop = False
#Keydown
if (event.type == pygame.KEYDOWN):
#Space key
if (event.key == pygame.K_SPACE):
x = random.randint(0,0)
y = random.randint(0,668)
enemies.add(Enemy(x,y))
#Escape key
if (event.key == pygame.K_ESCAPE):
sys.exit()
if pygame.sprite.spritecollide(cursor, enemies, True):
sys.exit()
#Window color
window.fill(blue)
#Updating player
player.update()
#Updating enemy
for enemy in enemies:
enemy.x += moveX
enemy.update()
enemies.update()
enemies.draw(window)
#Updating cursor
cursors.update(enemies)
cursors.draw(window)
#FPS
clock.tick(60)
pygame.display.flip()
pygame.quit()
对这些更改的解释是,通过将pygame.sprite.spritecollide()
命令移动到while循环中,您可以在任何位置与任何敌人进行联系,并仍然激活您的sys.exit() 。 pygame.sprite.spritecollide()
在其parantheses中需要三件事:你将函数放入的变量(cursor = Cursor()
),与之碰撞的精灵组(enemies = pygame.sprite.Group()
),以及真实/假的第三(因为你的陈述是游戏仍在运行gameloop = True
,所以True将被放在第三部分。)这应该解决你的问题,如果没有,那么我希望你很快找到答案!另外,我建议您使用while 1:
或while True:
!