2013-04-12 20 views
0

隨機PACMAN 任務是動畫5個對象(pac man和4個幽靈隨機移動,如果pac男子與他反彈的幽靈相撞,幽靈通過彼此並且在被pac man擊中時不受影響當鼠標被點擊時,模式改變並且pac man的鬼魂反彈,如果它們碰撞並且pac man不受影響,如果再次點擊鼠標,它將恢復到原始模式函數ghost_collision和mode_swich處理這個,我有一個if語句在最後爲鼠標點擊。BUG1I不能解決爲什麼當精靈碰撞有時他們變得不動(兩種模式)。也是我可以取代一些重複的if語句有一個列表和一個for循環?如果是這樣的話?我試着列出所有的ghost_boundary變量,然後使用使用pygame模式改變彈跳pacman更改buging

For sprite in sprite_list():   
    sprite.left < 0 or sprite.right > WIDTH: 
    sprite_velocity[1] = -1 * sprite_velocity[1] 

並且失敗了。

import pygame 
from random import * 



pygame.init() 

def ghost_collision(): 
    # global pacman_up, pacman_right, pacman_velocity why? 
    SCREAM.play() 
    pacman_velocity[0] = randint(-1,1) 
    pacman_velocity[1] = randint(-1,1) 
    if pacman_velocity[0] < 0: 
     pacman = pygame.image.load(PACMAN_LEFT).convert() 
    elif pacman_velocity[0] > 0: 
     pacman = pygame.image.load(PACMAN_RIGHT).convert() 
    return pacman_velocity 

def mode_switch(): 
    global mode 
    blue_right, orange_right, pink_right,\ 
      red_right, blue, orange, pink, red 
    if mode == False: 
     mode = True 
     blue = pygame.image.load(GHOST_SCARED).convert() 
     orange = pygame.image.load(GHOST_SCARED).convert() 
     pink = pygame.image.load(GHOST_SCARED).convert() 
     red = pygame.image.load(GHOST_SCARED).convert() 
    else: 
     mode = False 
     if blue_right == True: 
      blue = pygame.image.load(BLUE_LEFT).convert() 
      blue_right = False 
    else: 
     blue = pygame.image.load(BLUE_RIGHT).convert() 
     blue_right = True 
    if orange_right == True: 
     orange = pygame.image.load(ORANGE_LEFT).convert() 
     orange_right = False 
    else: 
     orange = pygame.image.load(ORANGE_RIGHT).convert() 
     orange_right = True 
    if pink_right == True: 
     pink = pygame.image.load(PINK_LEFT).convert() 
     pink_right = False 
    else: 
     pink = pygame.image.load(PINK_RIGHT).convert() 
     pink_right = True 
    if red_right == True: 
     red = pygame.image.load(RED_LEFT).convert() 
     red_right = False 
    else: 
     red = pygame.image.load(RED_RIGHT).convert() 
     red_right = True 


PACMAN_LEFT = 'pacman-left.png'#import all necessary images and sounds 
PACMAN_RIGHT = 'pacman-right.png' 
BLUE_LEFT = 'blue-left.png' 
BLUE_RIGHT = 'blue-right.png' 
ORANGE_LEFT = 'orange-left.png' 
ORANGE_RIGHT = 'orange-right.png' 
PINK_LEFT = 'pink-left.png' 
PINK_RIGHT = 'pink-right.png' 
RED_LEFT = 'red-left.png' 
RED_RIGHT = 'red-right.png' 
GHOST_SCARED = 'vulnerable.png' 
BOUNCE_SOUND = 'Thump.wav' 
SCREAM_SOUND = 'pacman_death.wav' 
GHOST_WAIL = 'pacman_eatfruit.wav' 
SOUND = pygame.mixer.Sound(BOUNCE_SOUND) 
SCREAM = pygame.mixer.Sound(SCREAM_SOUND) 
WAIL = pygame.mixer.Sound(GHOST_WAIL) 
WIDTH = 800 
HEIGHT = 600 
BACKGROUND_COLOUR = 0, 0, 0 
CAPTION = 'Random Pacman' 
pacman_up = True#define image direction controlling variables 
pacman_right = True 
blue_right = False 
orange_right = True 
pink_right = False 
red_right = True 
mode = False 

frame = pygame.display.set_mode((WIDTH, HEIGHT)) 
pygame.display.set_caption(CAPTION) 
pacman = pygame.image.load(PACMAN_RIGHT).convert()#create sprites and define boundaries 
pacman_boundary = pacman.get_rect(center = (300,300)) 
pacman_velocity = [1, 1] 
blue = pygame.image.load(BLUE_LEFT).convert() 
blue_boundary = pacman.get_rect(center = (300,300)) 
blue_velocity = [-1, -1] 
orange = pygame.image.load(ORANGE_RIGHT).convert() 
orange_boundary = pacman.get_rect(center = (300,300)) 
orange_velocity = [1, -1] 
pink = pygame.image.load(PINK_LEFT).convert() 
pink_boundary = pacman.get_rect(center = (300,300)) 
pink_velocity = [-1, 1] 
red = pygame.image.load(RED_RIGHT).convert() 
red_boundary = pacman.get_rect(center = (300,300)) 
red_velocity = [1, 1] 

finished = False 
while not finished: 
    pygame.event.pump() 
     for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      finished = True 
     if event.type == pygame.MOUSEBUTTONUP: 
      mode_switch() 

    ##################################################### 

if pacman_boundary.left < 0 or pacman_boundary.right > WIDTH:#pacman bounce code 
    SOUND.play() 
    pacman_velocity[0] = -1 * pacman_velocity[0] 
    if pacman_velocity[0] < 0: 
     pacman = pygame.image.load(PACMAN_LEFT).convert() 
    elif pacman_velocity[0] > 0: 
     pacman = pygame.image.load(PACMAN_RIGHT).convert() 
    pacman_velocity[1] = randint(-1,1) 

if pacman_boundary.top < 0 or pacman_boundary.bottom > HEIGHT: 
    SOUND.play() 
    pacman_velocity[1] = -1 * pacman_velocity[1] 

if mode == False:   
    if pacman_boundary.colliderect(blue_boundary)\ 
     or pacman_boundary.colliderect(orange_boundary)\ 
     or pacman_boundary.colliderect(pink_boundary)\ 
     or pacman_boundary.colliderect(red_boundary): 
     ghost_collision() 

    ##################################################### 

if blue_boundary.left < 0 or blue_boundary.right > WIDTH:#blue bounce code 
    blue_velocity[0] = -1 * blue_velocity[0] 
    if mode == False: 
     if blue_right == True: 
      blue = pygame.image.load(BLUE_LEFT).convert() 
      blue_right = False 
     else: 
      blue = pygame.image.load(BLUE_RIGHT).convert() 
      blue_right = True 

if blue_boundary.top < 0 or blue_boundary.bottom > HEIGHT: 
    SOUND.play() 
    blue_velocity[1] = -1 * blue_velocity[1] 

if mode == True: 
    if pacman_boundary.colliderect(blue_boundary): 

     blue_velocity[0] = randint(-1,1) 
     blue_velocity[1] = randint(-1,1) 
     WAIL.play() 


##################################################### 

if orange_boundary.left < 0 or orange_boundary.right > WIDTH:#orange bounce code 
    orange_velocity[0] = -1 * orange_velocity[0] 
    if mode == False: 
     if orange_right == True: 
      orange = pygame.image.load(ORANGE_LEFT).convert() 
      orange_right = False 
     else: 
      orange = pygame.image.load(ORANGE_RIGHT).convert() 
      orange_right = True 

if orange_boundary.top < 0 or orange_boundary.bottom > HEIGHT: 
    SOUND.play() 
    orange_velocity[1] = -1 * orange_velocity[1] 

if mode == True: 
    if pacman_boundary.colliderect(orange_boundary): 
     orange_velocity[0] = randint(-1,1) 
     orange_velocity[1] = randint(-1,1) 
     WAIL.play() 

##################################################### 

if pink_boundary.left < 0 or pink_boundary.right > WIDTH:#pink bounce code 
    pink_velocity[0] = -1 * pink_velocity[0] 
    if mode == False: 
     if pink_right == True: 
      pink = pygame.image.load(PINK_LEFT).convert() 
      pink_right = False 
     else: 
      pink = pygame.image.load(PINK_RIGHT).convert() 
      pink_right = True 

if pink_boundary.top < 0 or pink_boundary.bottom > HEIGHT: 
    SOUND.play() 
    pink_velocity[1] = -1 * pink_velocity[1] 

if mode == True: 
    if pacman_boundary.colliderect(pink_boundary): 
     pink_velocity[0] = randint(-1,1) 
     pink_velocity[1] = randint(-1,1) 
     WAIL.play() 

##################################################### 

if red_boundary.left < 0 or red_boundary.right > WIDTH:#red bounce code 
    red_velocity[0] = -1 * red_velocity[0] 
    if mode == False: 
     if red_right == True: 
      red = pygame.image.load(RED_LEFT).convert() 
      red_right = False 
     else: 
      red = pygame.image.load(RED_RIGHT).convert() 
      red_right = True 

if red_boundary.top < 0 or red_boundary.bottom > HEIGHT: 
    SOUND.play() 
    red_velocity[1] = -1 * red_velocity[1] 

if mode == True: 
    if pacman_boundary.colliderect(red_boundary): 
     red_velocity[0] = randint(-1,1) 
     red_velocity[1] = randint(-1,1) 
     WAIL.play() 

##################################################### 



pacman_boundary = pacman_boundary.move(pacman_velocity) 
blue_boundary = blue_boundary.move(blue_velocity) 
orange_boundary = orange_boundary.move(orange_velocity) 
pink_boundary = pink_boundary.move(pink_velocity) 
red_boundary = red_boundary.move(red_velocity) 
frame.fill(BACKGROUND_COLOUR) 
frame.blit(pacman, pacman_boundary) 
frame.blit(blue, blue_boundary) 
frame.blit(orange, orange_boundary) 
frame.blit(pink, pink_boundary) 
frame.blit(red, red_boundary) 
pygame.display.flip() 

pygame.quit() 

回答

0

原因相撞後的精靈有時變得不動就是因爲這個代碼在這裏:

pacman_velocity[0] = randint(-1,1) 
pacman_velocity[1] = randint(-1,1) 

randint該範圍意味着你得到一個整數值,要麼是-1,0,或1.
從文檔:(http://docs.python.org/2/library/random.html

random.randint(a, b) 
Return a random integer N such that a <= N <= b 

這意味着,(理論上)每次有一個碰撞,速度的x分量爲0的概率爲1/3,速度的y分量爲零的概率爲1/3。如果你只需要選擇1或-1,嘗試random.choice()函數:

pacman_velocity[0] = random.choice((-1, 1)) 
pacman_velocity[1] = random.choice((-1, 1)) 

如果你想包括0的可能性,但要減少其頻率,你可以完成與是這樣的:

pacman_velocity[0] = random.choice((-1, -1, -1, 0, 1, 1, 1)) 
pacman_velocity[0] = random.choice((-1, -1, -1, 0, 1, 1, 1)) 

這個循環您嘗試的問題:

For sprite in sprite_list():   
    sprite.left < 0 or sprite.right > WIDTH: 
    sprite_velocity[1] = -1 * sprite_velocity[1] 

是,你正在檢查,看看是否精靈已通過屏幕的左邊或右邊界,但後來我不是顛倒精靈速度的x分量,而是顛倒速度的y分量。此外,您在條件開始處沒有if。試試這個:

For sprite in sprite_list: 
    if sprite.left < 0 or sprite.right > WIDTH: 
     sprite_velocity[0] = -1 * sprite_velocity[0] 
    if sprite.top < 0 or sprite.bottom > HEIGHT: 
     sprite_velocity[1] = -1 * sprite_velocity[1] 
+0

我可以用什麼來代替randint來防止33%的機會獲得0值? –

+0

您希望看到什麼值的實際範圍?你真的只想把它設置爲1或-1嗎? – Haz

+0

我編輯了我的答案,爲您提供解決方案,如果您只想選擇1或-1。 – Haz