1

好的,所以我的代码在下面,现在我是 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()
4

2 回答 2

1

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

于 2013-01-09T06:53:45.607 回答
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()
于 2013-01-09T07:07:45.723 回答