如何使图像在pygame中对角移动?

时间:2016-09-27 22:17:54

标签: python image pygame move

我被教会如何向左,向右,向上和向下移动pygame中的图像。我们的下一个任务是使图像对角移动,但我不明白如何。到目前为止这是我的代码:(对于奇怪的名字感到抱歉) 哦,而且,我的代码中有两个图像。我想知道是否有一种方法可以移动两个图像而屏幕上没有消失?例如,我可以使用箭头键移动一个图像,但另一个图像将消失。我也可以使用WASD移动另一个图像,但第一个图像将消失。非常感谢你!

import pygame

#set up the initial pygame window
pygame.init()
screen = pygame.display.set_mode([900,600])

#set background color
background = pygame.Surface(screen.get_size())
background.fill([204,255,229])
screen.blit(background, (0,0))

#Pull in the image to the program
my_image = pygame.image.load("google_logo.png")
person = pygame.image.load("google_logo2.png")

#copy the image pixels to the screen
left_side = 50
height = 50
diagonal = 100
down_diagonal = 100
screen.blit(my_image, [left_side, height])
screen.blit (person, [diagonal, down_diagonal])

#Display changes
pygame.display.flip()

#set up pygame event loop
running = True
while running:
for event in pygame.event.get():
    print event
    if event.type == pygame.QUIT:
            running = False
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_q:
            print "QUITTING NOW..."
            pygame.time.delay(2000)
            running = False
        if event.key == pygame.K_h:
            print "HELLO!"
            pygame.time.delay(2500)
            running = False
        if event.key == pygame.K_c:
            print "To move the original Google logo, use the arrow keys. To move the second logo, use the WASD keys."
        elif event.key == pygame.K_RIGHT:
            screen.blit(background, (0,0))
            left_side = left_side + 10
            screen.blit(my_image, [left_side, height])
            pygame.display.flip()
        elif event.key == pygame.K_LEFT:
            screen.blit(background, (0,0))
            left_side = left_side - 10
            screen.blit(my_image, [left_side, height])
            pygame.display.flip()
        elif event.key == pygame.K_UP:
            screen.blit(background, (0,0))
            height = height - 10
            screen.blit(my_image, [left_side, height])
            pygame.display.flip()
        elif event.key == pygame.K_DOWN:
            screen.blit(background, (0,0))
            height = height + 10
            screen.blit(my_image, [left_side, height])
            pygame.display.flip()
        elif event.key == pygame.K_w:
            screen.blit(background, (0,0))
            down_diagonal = down_diagonal - 10
            screen.blit(person, [diagonal, down_diagonal])
            pygame.display.flip()
        elif event.key == pygame.K_a:
            screen.blit(background, (0,0))
            diagonal = diagonal - 10
            screen.blit(person, [diagonal, down_diagonal])
            pygame.display.flip()
        elif event.key == pygame.K_s:
            screen.blit(background, (0,0))
            down_diagonal = down_diagonal + 10
            screen.blit(person, [diagonal, down_diagonal])
            pygame.display.flip()
        elif event.key == pygame.K_d:
            screen.blit(background, (0,0))
            diagonal = diagonal + 10
            screen.blit(person, [diagonal, down_diagonal])
            pygame.display.flip()

pygame.quit()
编辑:我修改了我的代码,就像你说的那样,但它仍然不适合我。 (我为这些问题再次道歉,因为我对Python很陌生)我会永远感激你的帮助。

import pygame

#set up the initial pygame window
pygame.init()
screen = pygame.display.set_mode([900,600])

#set background color
background = pygame.Surface(screen.get_size())
background.fill([204,255,229])
screen.blit(background, (0,0))

#Pull in the image to the program
my_image = pygame.image.load("google_logo.png")

#copy the image pixels to the screen
screen.blit(my_image, [x, y])

#Display changes
pygame.display.flip()

keys = {'right':False, 'up':False, 'left':False, 'down':False}

#set up pygame event loop
running = True
while running:
for event in pygame.event.get():
    print event
    if event.type == pygame.QUIT:
            running = False
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_q:
            print "QUITTING NOW..."
            pygame.time.delay(2000)
            running = False
        if event.key == pygame.K_h:
            print "HELLO!"
            pygame.time.delay(2500)
            running = False
        if event.key == pygame.K_c:
            print "To move the original Google logo, use the arrow keys. To move the second logo, use the WASD keys."
        if event.key == pygame.K_RIGHT:
            keys['right'] = True
        if event.key == pygame.K_UP:
            keys['up'] = True
        if event.key == pygame.K_DOWN:
            keys['down'] = True
        if event.key == pygame.K_RIGHT:
            keys['right'] = True
        if event.key == pygame.K_LEFT:
            keys['left'] = True
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_RIGHT:
            keys['right'] = False
        if event.key == pygame.K_UP:
            keys['up'] = False
        if event.key == pygame.K_DOWN:
            keys['down'] = False
        if event.key == pygame.K_LEFT:
            keys['left'] = False

    x = 0
    y = 0

    if keys['right']:
        x += 10
    if keys['up']:
        y += 10
    if keys['down']:
        y -=10
    if keys['left']:
        x -=10

pygame.quit()

1 个答案:

答案 0 :(得分:1)

我认为你应该监视按键并按键,然后进行数学计算。

首先设置:

keys = {'right':False, 'up':False, 'left':False, 'down':False}

然后在事件KEYDOWN上将dict[key]设置为True

if event.key == pygame.K_RIGHT:
    keys['right'] = True
if event.key == pygame.K_UP:
    keys['up'] = True
...

在事件类型KEYUP上执行相同的操作,但将keys[key]设置为False

然后在你的事件循环中:

x = 0
y = 0

if keys['right']:
   x += 10

if keys['up']:
   y += 10

....

然后使用xy移动对象。

screen.blit(my_image, [x, y])

现在您可以按住键,图像将会移动,当您松开按键时,它会停止(无需重复点按键移动)

编辑:

import pygame

#set up the initial pygame window
pygame.init()
screen = pygame.display.set_mode([900,600])

#set background color
background = pygame.Surface(screen.get_size())
background.fill([204,255,229])
screen.blit(background, (0,0))

#Pull in the image to the program
my_image = pygame.image.load("google_logo.png")

#copy the image pixels to the screen
screen.blit(my_image, [x, y])

#Display changes
pygame.display.flip()

keys = {'right':False, 'up':False, 'left':False, 'down':False}
x = 0
y = 0
#set up pygame event loop
running = True
while running:
    screen.blit(my_image, [x, y])
    pygame.display.flip()
    for event in pygame.event.get():
        print event
        if event.type == pygame.QUIT:
                running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                print "QUITTING NOW..."
                pygame.time.delay(2000)
                running = False
            if event.key == pygame.K_h:
                print "HELLO!"
                pygame.time.delay(2500)
                running = False
            if event.key == pygame.K_c:
                print "To move the original Google logo, use the arrow keys. To move the second logo, use the WASD keys."
            if event.key == pygame.K_RIGHT:
                keys['right'] = True
            if event.key == pygame.K_UP:
                keys['up'] = True
            if event.key == pygame.K_DOWN:
                keys['down'] = True
            if event.key == pygame.K_LEFT:
                keys['left'] = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                keys['right'] = False
            if event.key == pygame.K_UP:
                keys['up'] = False
            if event.key == pygame.K_DOWN:
                keys['down'] = False
            if event.key == pygame.K_LEFT:
                keys['left'] = False

        x = 0
        y = 0

        if keys['right']:
            x += 10
        if keys['up']:
            y += 10
        if keys['down']:
            y -=10
        if keys['left']:
            x -=10

pygame.quit()