我在pygame
中遇到了一些问题。我试图这样做,以便当plum
点击地面时(代码中称为SCREENRECT
),LIVES
将减1。
碰撞检测适用于SCORE
,但我无法弄清楚如何做LIVES
。欢迎任何建议,谢谢。代码:
import random, os.path
import pygame
from pygame.locals import *
if not pygame.image.get_extended():
raise SystemExit("Sorry, extended image module required")
MAX_PLUMS = 30 #most dropping plums at once
PLUM_ODDS = 250 #chance a new plum will drop
PLUM_RESPAWN = 10 #frames between new plums
SCREENRECT = Rect(0, 0, 640, 480) #screen size
SCORE = 0
LIVES = 3
main_dir = os.path.split(os.path.abspath(__file__))[0]
def load_image(file):
"loads an image, prepares it for play"
file = os.path.join(main_dir, 'data', file)
try:
surface = pygame.image.load(file)
except pygame.error:
raise SystemExit('Could not load image "%s" %s'%(file, pygame.get_error()))
return surface.convert()
def load_images(*files):
imgs = []
for file in files:
imgs.append(load_image(file))
return imgs
class dummysound:
def play(self): pass
def load_sound(file):
if not pygame.mixer: return dummysound()
file = os.path.join(main_dir, 'data', file)
try:
sound = pygame.mixer.Sound(file)
return sound
except pygame.error:
print ('Warning, unable to load, %s' % file)
return dummysound()
class Player(pygame.sprite.Sprite):
speed = 15
bounce = 24
images = []
def __init__(self):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect(midbottom=SCREENRECT.midbottom)
self.origtop = self.rect.top
self.facing = -1
def move(self, direction):
if direction: self.facing = direction
self.rect.move_ip(direction*self.speed, 0)
self.rect = self.rect.clamp(SCREENRECT)
if direction < 0:
self.image = self.images[0]
elif direction > 0:
self.image = self.images[1]
self.rect.top = self.origtop - (self.rect.left//self.bounce%2)
class dropper(pygame.sprite.Sprite):
speed = 9
animcycle = 12
images = []
def __init__(self):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.facing = random.choice((-1,1)) * dropper.speed
self.frame = 0
if self.facing < 0:
self.rect.right = SCREENRECT.right
def update(self):
self.rect.move_ip(self.facing, 0)
if not SCREENRECT.contains(self.rect):
self.facing = -self.facing;
self.rect = self.rect.clamp(SCREENRECT)
class Explosion(pygame.sprite.Sprite):
defaultlife = 12
animcycle = 3
images = []
def __init__(self, actor):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect(center=actor.rect.center)
self.life = self.defaultlife
def update(self):
self.life = self.life - 1
self.image = self.images[self.life//self.animcycle%2]
if self.life <= 0: self.kill()
class Plum(pygame.sprite.Sprite):
speed = 9
images = []
def __init__(self, dropper):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect(midbottom=dropper.rect.move(0,5).midbottom)
def update(self):
self.rect.move_ip(0, self.speed)
if self.rect.bottom >= 470:
Explosion(self)
self.kill()
class Score(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.Font(None, 25)
self.font.set_italic(1)
self.color = Color('white')
self.lastscore = -1
self.update()
self.rect = self.image.get_rect().move(10, 450)
def update(self):
if SCORE != self.lastscore:
self.lastscore = SCORE
msg = "Score: %d" % SCORE
self.image = self.font.render(msg, 0, self.color)
class Lives(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.Font(None, 25)
self.font.set_italic(1)
self.color = Color('white')
self.lastlife = -1
self.update()
self.rect = self.image.get_rect().move(565, 450)
def update(self):
if LIVES != self.lastlife:
self.lastlife = LIVES
msg = "Lives: %d" % LIVES
self.image = self.font.render(msg, 0, self.color)
def main(winstyle = 0):
pygame.init()
if pygame.mixer and not pygame.mixer.get_init():
print ('Warning, no sound')
pygame.mixer = None
winstyle = 0
bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32)
screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth)
img = load_image('player1.gif')
Player.images = [img, pygame.transform.flip(img, 1, 0)]
img = load_image('explosion1.gif')
Explosion.images = [img, pygame.transform.flip(img, 1, 1)]
dropper.images = load_images('dropper.gif')
Plum.images = [load_image('plum.gif')]
icon = pygame.transform.scale(Plum.images[0], (32, 32))
pygame.display.set_icon(icon)
pygame.display.set_caption('Plum Picker')
pygame.mouse.set_visible(0)
bgdtile = load_image('background.gif')
background = pygame.Surface(SCREENRECT.size)
for x in range(0, SCREENRECT.width, bgdtile.get_width()):
background.blit(bgdtile, (x, 0))
screen.blit(background, (0,0))
pygame.display.flip()
boom_sound = load_sound('boom.wav')
shoot_sound = load_sound('car_door.wav')
if pygame.mixer:
music = os.path.join(main_dir, 'data', 'house_lo.wav')
pygame.mixer.music.load(music)
pygame.mixer.music.play(-1)
droppers = pygame.sprite.Group()
plums = pygame.sprite.Group()
all = pygame.sprite.RenderUpdates()
lastdropper = pygame.sprite.GroupSingle()
Player.containers = all
dropper.containers = droppers, all, lastdropper
Plum.containers ums, all
Explosion.containers = all
Score.containers = all
Lives.containers = all
global score
plumrespawn = PLUM_RESPAWN
kills = 0
clock = pygame.time.Clock()
global SCORE
global LIVES
player = Player()
dropper()
if pygame.font:
all.add(Score())
all.add(Lives())
while player.alive():
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == KEYDOWN and event.key == K_ESCAPE):
return
keystate = pygame.key.get_pressed()
all.clear(screen, background)
all.update()
direction = keystate[K_RIGHT] - keystate[K_LEFT]
player.move(direction)
# Create new dropper
if plumrespawn:
plumrespawn = plumrespawn - 1
elif not int(random.random() * PLUM_ODDS):
dropper()
plumrespawn = PLUM_RESPAWN
# Drop plums
if lastdropper and not int(random.random() * PLUM_ODDS):
Plum(lastdropper.sprite)
for plum in pygame.sprite.spritecollide(player, plums, 1):
boom_sound.play()
SCORE = SCORE + 1
#LIVES NEEDS TO BE HERE. I THINK IT NEEDS TO BE SOMETHING LIKE:
#for plum in pygame.sprite.spritecollide(SCREENRECT, plums, 1):
#boom_sound.play()
#LIVES = LIVES - 1
if LIVES == 0:
player.kill()
#draw the scene
dirty = all.draw(screen)
pygame.display.update(dirty)
clock.tick(40)
if pygame.mixer:
pygame.mixer.music.fadeout(1000)
pygame.time.wait(1000)
pygame.quit()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
如果您正在检查李子和Rect(0,0,640,480)之间的碰撞,您会注意到李子一直包含在矩形内。
尝试:
for plum in pygame.sprite.spritecollide(Rect(0,480,640,50), plums, 1):
boom_sound.play()
LIVES = LIVES - 1
我很久没有使用过pygame的精灵,所以情况可能并非如此,但它值得一试:)