pacman循环问题,图像不显示/移动

时间:2013-04-16 01:56:23

标签: loops pygame

好吧所以我做了一个pacman任务(在bascialy pacman之前发布,鬼魂在屏幕上移动而pacman从他们身上反弹,但是当点击鼠标时鬼魂反弹并改为“害怕图像”并且到目前为止因为我可以告诉我已经解决了我的本地错误等。但是现在我的代码似乎根本没有显示任何精灵。当我将框架和边界代码移动到顶部时,它们会显示在屏幕的一个点上没有动静。有什么建议吗?

import pygame
from random import *

#Core values/images for game 
pygame.init()


#define ghost collisons in orignal mode to alter pacmans movement

def ghost_collision():

    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

#define mode 2 as ghosts changing to scared and bouncing off pacman
def mode_switch():

    global blue_right, orange_right, pink_right,\
          red_right, blue, orange, pink, red
    global mode
    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'
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'
SOUND = pygame.mixer.Sound(BOUNCE_SOUND)
WIDTH = 1200
HEIGHT = 800
BACKGROUND_COLOUR = 0, 0, 0
CAPTION = 'Pacman'

pacman_left = True
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()

#Sprites and defined 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()


    #####################################################
# Boundary sets for all sprite's (could use list but lack skill and time)
if pacman_boundary.left < 0 or pacman_boundary.right > WIDTH:
    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_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)



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

if orange_boundary.left < 0 or orange_boundary.right > WIDTH:
    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)


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

if pink_boundary.left < 0 or pink_boundary.right > WIDTH:#
    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)


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

if red_boundary.left < 0 or red_boundary.right > WIDTH:
    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)


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


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()

1 个答案:

答案 0 :(得分:2)

除非您的帖子格式化已关闭,否则您的所有游戏循环逻辑现在都在循环之外。请记住,Python对缩进很敏感,因此所有代码都以:

开头
if pacman_boundary.left < 0 or pacman_boundary.right > WIDTH:

不在循环内部:

finished = False
while not finished:

你在游戏循环后发生的所有绘图代码,所以在当前形式中,它只会被渲染一次,就在调用pygame.quit()之前。