2016-01-07 39 views
1

到目前爲止,我幾乎完成了這個躲閃者/躲避者遊戲,我添加了在互聯網上的教程中找到的聲音,但是,當我嘗試運行遊戲時,當我按下按鈕玩,遊戲不響應,但當我按下鼠標按鈕,聲音播放,當我按下X按鈕,這是關閉程序,遊戲運行,我不知道如何使程序運行時,我按任意鍵,並在同一時間,當我按下鼠標按鈕使播放聲音,這裏是下面的教程,我用來做這下面是代碼(「http://programarcadegames.com/index.php?chapter=bitmapped_graphics_and_sound。」):Python遊戲沒有迴應

import pygame, random, sys 
from pygame.locals import * 


TEXTCOLOR = (0, 0, 0) 
FPS = 60 
BADDIEMINSIZE = 8 
BADDIEMAXSIZE = 70 
BADDIEMINSPEED = 1 
BADDIEMAXSPEED = 12 
ADDNEWBADDIERATE = 1 
PLAYERMOVERATE = 3 
WINDOWWIDTH = 1280 
WINDOWHEIGHT = 780 
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) 
background = pygame.image.load("background.png") 
backgroundRect = background.get_rect 
background_position = [0, 0] 

def terminate(): 
    pygame.quit() 
    sys.exit() 

def waitForPlayerToPressKey(): 
    while True: 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       terminate() 
      if event.type == KEYDOWN: 
       if event.key == K_ESCAPE: # pressing escape quits 
        terminate() 
       return 

def playerHasHitBaddie(playerRect, baddies): 
    for b in baddies: 
     if playerRect.colliderect(b['rect']): 
      return True 
    return False 

def drawText(text, font, surface, x, y): 
    textobj = font.render(text, 1, TEXTCOLOR) 
    textrect = textobj.get_rect() 
    textrect.topleft = (x, y) 
    surface.blit(textobj, textrect) 

# set up pygame, the window, and the mouse cursor 
pygame.init() 
mainClock = pygame.time.Clock() 
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) 
pygame.display.set_caption('Dodger') 
pygame.mouse.set_visible(False) 

# set up fonts 
font = pygame.font.SysFont(None, 48) 

# set up sounds 
gameOverSound = pygame.mixer.Sound('gameover.wav') 
click_sound = pygame.mixer.Sound("pistol.wav") 




# set up images 
playerImage = pygame.image.load('ship.png') 
playerRect = playerImage.get_rect() 
baddieImage = pygame.image.load('baddie.png') 
background_image = pygame.image.load("background.png") .convert() 

# Copy image to screen 
screen.blit(background_image, background_position) 

# Set positions of graphics 
background_position = [0, 0] 

# show the "Start" screen 
drawText('Dodger', font, windowSurface, (WINDOWWIDTH/3), (WINDOWHEIGHT/3)) 
drawText('Press a key to start.', font, windowSurface, (WINDOWWIDTH/3) - 30, (WINDOWHEIGHT/3) + 50) 
pygame.display.update() 
waitForPlayerToPressKey() 


topScore = 0 
while True: 
    # set up the start of the game 
    baddies = [] 
    score = 0 
    playerRect.topleft = (WINDOWWIDTH/2, WINDOWHEIGHT - 50) 
    moveLeft = moveRight = moveUp = moveDown = False 
    reverseCheat = slowCheat = False 
    baddieAddCounter = 0 

    done = False 

    while not done: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       done = True 
      elif event.type == pygame.MOUSEBUTTONDOWN: 
       click_sound.play() 


    while True: # the game loop runs while the game part is playing 
     score += 1 # increase score 



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



      if event.type == KEYDOWN: 
       if event.key == ord('z'): 
        reverseCheat = True 
       if event.key == ord('x'): 
        slowCheat = True 
       if event.key == K_LEFT or event.key == ord('a'): 
        moveRight = False 
        moveLeft = True 
       if event.key == K_RIGHT or event.key == ord('d'): 
        moveLeft = False 
        moveRight = True 
       if event.key == K_UP or event.key == ord('w'): 
        moveDown = False 
        moveUp = True 
       if event.key == K_DOWN or event.key == ord('s'): 
        moveUp = False 
        moveDown = True 

      if event.type == KEYUP: 
       if event.key == ord('z'): 
        reverseCheat = False 
        score = 0 
       if event.key == ord('x'): 
        slowCheat = False 
        score = 0 
       if event.key == K_ESCAPE: 
         terminate() 

       if event.key == K_LEFT or event.key == ord('a'): 
        moveLeft = False 
       if event.key == K_RIGHT or event.key == ord('d'): 
        moveRight = False 
       if event.key == K_UP or event.key == ord('w'): 
        moveUp = False 
       if event.key == K_DOWN or event.key == ord('s'): 
        moveDown = False  


      if event.type == MOUSEMOTION: 
       # If the mouse moves, move the player where the cursor is. 
       playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery) 

     # Add new baddies at the top of the screen, if needed. 
     if not reverseCheat and not slowCheat: 
      baddieAddCounter += 1 
     if baddieAddCounter == ADDNEWBADDIERATE: 
      baddieAddCounter = 0 
      baddieSize = random.randint(BADDIEMINSIZE, BADDIEMAXSIZE) 
      newBaddie = {'rect': pygame.Rect(random.randint(0, WINDOWWIDTH-baddieSize), 0 - baddieSize, baddieSize, baddieSize), 
         'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED), 
         'surface':pygame.transform.scale(baddieImage, (baddieSize, baddieSize)), 
         } 

      baddies.append(newBaddie) 

     # Move the player around. 
     if moveLeft and playerRect.left > 0: 
      playerRect.move_ip(-1 * PLAYERMOVERATE, 0) 
     if moveRight and playerRect.right < WINDOWWIDTH: 
      playerRect.move_ip(PLAYERMOVERATE, 0) 
     if moveUp and playerRect.top > 0: 
      playerRect.move_ip(0, -1 * PLAYERMOVERATE) 
     if moveDown and playerRect.bottom < WINDOWHEIGHT: 
      playerRect.move_ip(0, PLAYERMOVERATE) 

     # Move the mouse cursor to match the player. 
     pygame.mouse.set_pos(playerRect.centerx, playerRect.centery) 

     # Move the baddies down. 
     for b in baddies: 
      if not reverseCheat and not slowCheat: 
       b['rect'].move_ip(0, b['speed']) 
      elif reverseCheat: 
       b['rect'].move_ip(0, -5) 
      elif slowCheat: 
       b['rect'].move_ip(0, 1) 

     # Delete baddies that have fallen past the bottom. 
     for b in baddies[:]: 
      if b['rect'].top > WINDOWHEIGHT: 
       baddies.remove(b) 

     # Draw the game world on the window. 
     screen.blit(background_image, background_position) 

     # Draw the score and top score. 
     drawText('Score: %s' % (score), font, windowSurface, 10, 0) 
     drawText('Top Score: %s' % (topScore), font, windowSurface, 10, 40) 

     # Draw the player's rectangle 
     windowSurface.blit(playerImage, playerRect) 

     # Draw each baddie 
     for b in baddies: 
      windowSurface.blit(b['surface'], b['rect']) 

     pygame.display.update() 

     # Check if any of the baddies have hit the player. 
     if playerHasHitBaddie(playerRect, baddies): 
      if score > topScore: 
       topScore = score # set new top score 
      break 

     mainClock.tick(FPS) 

    # Stop the game and show the "Game Over" screen. 
    gameOverSound.play() 

    drawText('You lose', font, windowSurface, (WINDOWWIDTH/3), (WINDOWHEIGHT/3)) 
    drawText('Press a key to play again.', font, windowSurface, (WINDOWWIDTH/3) - 80, (WINDOWHEIGHT/3) + 50) 
    pygame.display.update() 
    waitForPlayerToPressKey() 

    gameOverSound.stop() 

我不知道我在做什麼錯,幫助我Pleease!支持這一點,將不勝感激。

+0

查看你所有的for事件循環,你應該明白你的問題。 – furas

回答

0

你有兩個while True循環 - 一個接一個。

第一個

done = False 

while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
     elif event.type == pygame.MOUSEBUTTONDOWN: 
      click_sound.play() 

運行,直到你點擊 「X」 按鈕(pygame.QUIT)。當你點擊鼠標時它只能播放聲音(pygame.MOUSEBUTTONDOWN

刪除此循環並在第二個while True循環中使用行if/else

+0

你告訴我,我應該完全刪除這些代碼:做=假 而沒有這樣做: 在pygame.event.get()事件: 如果event.type == pygame.QUIT: 完成=真 elif event.type == pygame.MOUSEBUTTONDOWN: click_sound.play()對不對? –

+0

是的,這段代碼會停止你的程序。在這個代碼下面你有另一個'while True'循環。你必須在另一個循環中使用'pygame.MOUSEBUTTONDOWN'和'click_sound.play()'。 – furas

+0

哦,我的上帝!有用!!非常感謝你furas !!!!! –