python和pygame图像运动

时间:2013-01-09 06:20:15

标签: python animation pygame

好的,所以我的代码在下面,现在我是pygame的新手并试图弄清楚如何在屏幕上移动图像。从我所看到的pygame,下面的代码应该向上移动图像(现在唯一可能的移动)。但是,当我向上推动图像不动。 Python正在认识到我正在推高,因为它在系统托盘中打印,但是没有动作,有人可以帮忙吗?

import pygame, sys
from pygame.locals import *

FPS = 30


WIN_WIDTH = 800
WIN_HEIGHT = 600
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)

COLOR = (255, 0, 255)
IMAGE = pygame.image.load('squirrel.png')
STARTSIZE = 25

LEFT = 'left'

def main():
    pygame.init()
    FPS_CLOCK = pygame.time.Clock()
    movement = 1

    moveUp = False

    DISPLAY_SURFACE = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))

    while True:
        DISPLAY_SURFACE.fill(COLOR)

        playerObj = {'surface': pygame.transform.scale(IMAGE, (STARTSIZE, STARTSIZE)),
                     'facing': LEFT,
                     'size': STARTSIZE,
                     'x': HALF_WIDTH,
                     'y': HALF_HEIGHT}

        playerObj['rect'] = pygame.Rect( (playerObj['x'], playerObj['y'], playerObj['size'], playerObj['size']) )
        DISPLAY_SURFACE.blit(playerObj['surface'], playerObj['rect'])


        for event in pygame.event.get():

            if event.type == KEYDOWN:
                if event.key == K_UP:
                    moveUp = True

            if event.type == KEYUP:
                if event.key == K_UP:
                    moveUp = False

            elif event.type == QUIT:
                pygame.quit()
                sys.exit()

        if moveUp:
            playerObj['y'] += movement
            print("moving up")

        else:
            print('stopped')

        pygame.display.update()
        FPS_CLOCK.tick(FPS)

if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:1)

每次迭代while循环时你都会创建一个新的playerObj,所以你不会看到它移动(因为它每次都将它的'y'值设置为HALF_HEIGHT)。将playerObj定义移出while循环。

答案 1 :(得分:0)

您必须在进入while循环之前创建playerObj,然后每次只将其绘制到DISPLAY_SURFACE。此外,增加词典的键y无济于事,您必须修改rect。微创解决方案将是:

import pygame, sys
from pygame.locals import *

FPS = 30


WIN_WIDTH = 800
WIN_HEIGHT = 600
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)

COLOR = (255, 0, 255)
IMAGE = pygame.image.load('squirrel.png')
STARTSIZE = 25

LEFT = 'left'

def main():
    pygame.init()
    FPS_CLOCK = pygame.time.Clock()
    movement = 1

    moveUp = False

    DISPLAY_SURFACE = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))

    playerObj = {'surface': pygame.transform.scale(IMAGE, (STARTSIZE, STARTSIZE)),
                     'facing': LEFT,
                     'size': STARTSIZE,
                     'x': HALF_WIDTH,
                     'y': HALF_HEIGHT}

    playerObj['rect'] = pygame.Rect( (playerObj['x'], playerObj['y'], playerObj['size'], playerObj['size']) )

    while True:
        DISPLAY_SURFACE.fill(COLOR)
        DISPLAY_SURFACE.blit(playerObj['surface'], playerObj['rect'])
        for event in pygame.event.get():

            if event.type == KEYDOWN:
                if event.key == K_UP:
                    moveUp = True

            if event.type == KEYUP:
                if event.key == K_UP:
                    moveUp = False

            elif event.type == QUIT:
                pygame.quit()
                sys.exit()

        if moveUp:
            playerObj['rect'].y -= movement
            print("moving up")

        else:
            print('stopped')

        pygame.display.update()
        FPS_CLOCK.tick(FPS)

if __name__ == '__main__':
    main()

它有效,我自己尝试过。但是你可以重构代码,不需要在字典中携带初始的x,y等值。

修改 随着四个方向的移动,这将是:

import pygame, sys
from pygame.locals import *

FPS = 30


WIN_WIDTH = 800
WIN_HEIGHT = 600
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)

COLOR = (255, 0, 255)
IMAGE = pygame.image.load('squirrel.png')
STARTSIZE = 25

LEFT = 'left'

def move_rect(rect, key, distance):
    if key == K_UP: 
        rect.y-=distance
    elif key == K_DOWN: 
        rect.y+=distance
    elif key == K_LEFT: 
        rect.x-=distance
    elif key == K_RIGHT: 
        rect.x+=distance

def main():
    pygame.init()
    FPS_CLOCK = pygame.time.Clock()
    movement = 1

    key2mvmt = {K_UP:False, K_DOWN:False, K_LEFT:False, K_RIGHT:False}

    DISPLAY_SURFACE = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))

    playerObj = {'surface': pygame.transform.scale(IMAGE, (STARTSIZE, STARTSIZE)),
                     'facing': LEFT,
                     'size': STARTSIZE,
                     'x': HALF_WIDTH,
                     'y': HALF_HEIGHT}

    playerObj['rect'] = pygame.Rect( (playerObj['x'], playerObj['y'], playerObj['size'], playerObj['size']) )

    while True:
        DISPLAY_SURFACE.fill(COLOR)
        DISPLAY_SURFACE.blit(playerObj['surface'], playerObj['rect'])
        for event in pygame.event.get():

            if event.type == KEYDOWN:
                if key2mvmt.has_key(event.key):
                    key2mvmt[event.key]= True

            if event.type == KEYUP:
                if key2mvmt.has_key(event.key):
                    key2mvmt[event.key]= False

            elif event.type == QUIT:
                pygame.quit()
                sys.exit()

        for k in key2mvmt.keys():
            if key2mvmt[k]:
                move_rect(playerObj['rect'], k, movement)

        else:
            print('stopped')

        pygame.display.update()
        FPS_CLOCK.tick(FPS)

if __name__ == '__main__':
    main()