Pygame spritecollide错误

时间:2013-02-23 17:14:55

标签: python pygame

我有几个关于pygame的问题。我对python / pygame是全新的,如果对于一个人好奇,我正在做这个,或者如果我写这个草率。

对于我的另一个问题,当我使用spritecollide时,即使图像消失后,对象似乎仍然存在。让我分享一下代码

import pygame, time, random, sys, player, creep, weapon
from pygame.locals import *

pygame.init()

#Variables for the game
width  = 700
height = 500
clock = pygame.time.Clock()

screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('Creep')

#Create Characters of the game
player1 = player.Player()
player1.rect.x = 0
player1.rect.y = 0

comp = creep.Creep()
comp.rect.x = random.randrange(width)
comp.rect.y = random.randrange(height)


bullet = weapon.Weapon()
bullet.rect.x = -1
bullet.rect.y = -1

#Make Character Groups
good = pygame.sprite.Group(player1)
bad = pygame.sprite.Group(comp)
weap = pygame.sprite.Group(bullet)


while True:
clock.tick(60)
screen.fill((0,0,0))

#set up for game to get input
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        sys.exit()
    if event.type == KEYDOWN and event.key == K_ESCAPE:
        sys.exit()
    if event.type == KEYDOWN and event.key == K_c:
        bullet.rect.x = player1.rect.x + 25
        bullet.rect.y = player1.rect.y

#main controls
key = pygame.key.get_pressed()
if key[K_RIGHT]:
    player1.rect.x = player1.moveRight(player1.rect.x)
if key[K_LEFT]:
    player1.rect.x = player1.moveLeft(player1.rect.x)
if key[K_DOWN]:
    player1.rect.y = player1.moveDown(player1.rect.y)
if key[K_UP]: 
    player1.rect.y = player1.moveUp(player1.rect.y)

if bullet.rect.x > -1:
    weap.draw(screen)
    bullet.rect.x = bullet.rect.x +5


pygame.sprite.spritecollide(bullet, bad, True)
pygame.sprite.spritecollide(comp, good, True)

#game functions
good.draw(screen)
bad.draw(screen)

pygame.display.flip()

所以我有一张枪的图像(player1,'good'组),计算机的图像(comp,'bad'组),以及当LCTRL被击中时“子弹”的图像(子弹,'武器组。当子弹击中坏组中的图像时,它会消失,这就是我想要的。但是当我向那个方向移动player1图像时,它会消失,好像“坏组”仍在那里。我希望这是有道理的。

我调用的类的示例代码如下所示:

import pygame

class Creep(pygame.sprite.Sprite):

def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load('creep.jpg')
    self.rect = self.image.get_rect()

有什么想法吗?如果有更好的方法,请告诉我,我一周前才开始学习,不知道我是否朝着正确的方向前进。谢谢!

1 个答案:

答案 0 :(得分:1)

我不确定你的变数是什么。注意:如果你将精灵放在多个组中,然后杀死它,会自动在所有组中杀死它

我开始清理代码。

#import time, sys, # not longer required 

import player, creep, weapon
import random
import pygame
from pygame.locals import *

pygame.init()

#Variables for the game
width  = 700
height = 500
clock = pygame.time.Clock()

screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('Creep')

#Create Characters of the game
player1 = player.Player()
player1.rect.x = 0
player1.rect.y = 0

comp = creep.Creep()
comp.rect.topleft = random.randrange(width), random.randrange(height)

bullet = pygame.sprite.Sprite()# weapon.Weapon()
bullet.rect.topleft = (-1, -1)

#Make Character Groups
good = pygame.sprite.Group(player1)
bad = pygame.sprite.Group(comp)
weap = pygame.sprite.Group(bullet)

done = False

while not done:
    clock.tick(60)

    #set up for game to get input
    for event in pygame.event.get():
        if event.type == pygame.QUIT: done = True
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE: done = True

            elif event.key == K_c:
                bullet.rect.x = player1.rect.x + 25
                bullet.rect.y = player1.rect.y

    #main controls
    key = pygame.key.get_pressed()
    if key[K_RIGHT]:
        player.rect.x += 10
    if key[K_LEFT]:
        player1.rect.x -= 10
    if key[K_DOWN]:
        player.rect.y += 10
    if key[K_UP]: 
        player.rect.y -= 10    

    # movement, collisions
    pygame.sprite.spritecollide(bullet, bad, True)
    pygame.sprite.spritecollide(comp, good, True)

    # not sure what this was for? If you meant 'onscreen' or?
    # You can kill it if it goes offscreen. Otherwise draw works if offscreen.
    if bullet.rect.x > -1:
        bullet.rect.x += 5

    screen.fill(Color("black"))
    weap.draw(screen)

    #game functions
    good.draw(screen)
    bad.draw(screen)

    pygame.display.flip()