Pygame - Python3.x文本和声音仅在某个部分有效地工作

时间:2017-07-17 02:28:17

标签: python pygame

这就是一切

from pygame import *
import random as rand
from colors import *
import os
import time
pygame.mixer.pre_init(44100, 16, 2, 4096)
pygame.init()
fps = 60
window_size = window_width, window_height = 800, 600
font = pygame.font.SysFont( "Power Green, Times New Roman, Arial", 25  )
score = 0

pygame.mixer.music.load('music.wav')
pygame.mixer.music.play(-1)
explode = pygame.mixer.Sound("explode.wav")


class Player(pygame.sprite.Sprite):
    #sprite for player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("plane.png")
        self.rect = self.image.get_rect()
        self.setprop()
        self.xspeed = 0




    def setprop( self ):

        self.rect = self.image.get_rect()

        self.orgin_x = self.rect.centerx
        self.orgin_y = self.rect.centery

    def set_position(self, x, y):
        self.rect.x = x - self.orgin_x
        self.rect.y = y - self.orgin_y

    def update(self):
        self.xspeed = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.xspeed = -9
        if keystate[pygame.K_RIGHT]:
            self.xspeed = 9
        self.rect.x += self.xspeed

        if self.rect.right > window_width:
            self.rect.right = window_width

        if self.rect.left < 0:
            self.rect.left = 0

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        sprites.add(bullet)
        bullets.add(bullet)

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image  = pygame.image.load("boulder.png")
        self.rect   = self.image.get_rect()
        self.rect.x = rand.randrange(window_width - self.rect.width)
        self.rect.y = rand.randrange( -100, -40 )
        self.speedy  = rand.randrange(6, 8)
        self.speedx = rand.randrange( -3, 3 )

    def play_sound(self):
        self.sound.play()

    def update(self):
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        if self.rect.top > window_height + 10 or self.rect.left < -20 or self.rect.right > window_width + 20:
            self.rect.x = rand.randrange(window_width - self.rect.width)
            self.rect.y = rand.randrange( -100, -40 )
            self.speedy  = rand.randrange(6, 10 )



class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10, 20))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedy = -10

    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom > 0:
            self.kill


def health_bars(player_health):
    if player_health > 75:
        player_health_color = green
    elif player_health > 50: 
        player_health_color = yellow
    else:
        player_health_color = red

    pygame.draw.rect(window, player_health_color, (20,20, player_health, 25))

def set_message(text):
    global message, previous_message
    message = font.render( 'HEALTH:' + str(player_health), True, white )
    previous_message = message

def set_score(text):
    global scoremsg
    scoremsg = font.render( 'SCORE:' + str(score), True, white )







sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
sprites.add(player)
for i in range(12):
    m = Mob()
    sprites.add(m)
    mobs.add(m)




player.set_position( window_width/2, window_height/2 +215 )       

pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( window_size, pygame.RESIZABLE )
pygame.display.set_caption('Dodge the boulders or shoot them (shooting gets you score)')
clock = pygame.time.Clock()
zap = pygame.mixer.Sound("zap.wav")
GAMEOVER = pygame.mixer.Sound("gameover.wav")

player_health = 100

running = True

while ( running ):

    clock.tick( fps )
    set_message( player_health)
    set_score(set_score)

    if (player_health == 0):

        pygame.mixer.music.stop()
        #window.blit(gameovertext, (window_width/2, window_height/2))
        pygame.display.update()
        pygame.mixer.Sound.play(GAMEOVER)



        for event in pygame.event.get():
            if( event.type == pygame.QUIT ):
                running = False


    for event in pygame.event.get():
        if( event.type == pygame.QUIT ):
            running = False
        elif event.type == pygame.KEYDOWN:
            if( event.key == pygame.K_SPACE ):
                player.shoot()








    window.fill(black)
    health_bars(player_health)
    sprites.update()
    #collide group with group
    hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
    if hits:
        pygame.mixer.Sound.play(zap)
        score += 1
    for hit in hits:
        m = Mob()
        sprites.add(m)
        mobs.add(m)


    #collide sprite with group
    hits = pygame.sprite.spritecollide(player, mobs, False)
    if hits:    
        player_health = player_health - 1

    sprites.draw(window)
    window.blit( message, (20, 50) )
    window.blit( scoremsg, (window_width/2 + 320, window_height - 35) )

    if (player_health == 0):

        pygame.mixer.music.stop()
        gameoverfont = pygame.font.SysFont("Power Green", 100)

        gameovertext = gameoverfont.render("GAME OVER", True, white )
        window.blit(gameovertext, (window_width / 2 - 160, window_height / 2 -75))
        pygame.mixer.Sound.play(GAMEOVER)
    pygame.display.update()


pygame.display.quit()
pygame.quit()

这是我遇到问题的部分:

if(player_health == 0):

pygame.mixer.music.stop()
gameoverfont = pygame.font.SysFont("Power Green", 100)

gameovertext = gameoverfont.render("GAME OVER", True, white )
window.blit(gameovertext, (window_width / 2 - 160, window_height / 2 -75))
pygame.mixer.Sound.play(GAMEOVER)
pygame.display.update()

奇怪的是,文本在某些时候出现,并且它会在不同的时间内发生。我在录制此链接时出现了GAMEOVER文字的较长时间之一:https://www.youtube.com/watch?v=Xf8aKx4SOTc

如果有人能告诉我这里我做错了什么我会很感激,我对pygame本身很新。 编辑:好吧youtube似乎已关闭。基本上它是什么,是GAME OVER在消失之前显示大约1.5秒,然后游戏语音音频将播放两次。 (不知道为什么两次)

1 个答案:

答案 0 :(得分:0)

首先你有两个if (player_health == 0):,试着加入所需的行只有一个,这应该解决声音播放两次的问题。

此外,您使用相同的名称hits来检测与子弹怪物的碰撞,使用不同的名称会更容易阅读和更容易,这样做可以避免混淆得分。

当它的GAME OVER时,循环仍在运行,并且没有任何东西阻止分数增加或减少。 对于小怪的if hits:,您应该在测试中添加另一个条件,例如:

if hits and player_health > 0: 
    player_health = player_health - 1

否则您应该将运行状况测试更改为

if (player_health <= 0):

我希望这可以解决文本显示的问题。