【问题标题】:How to make an image move diagonally in pygame?如何在pygame中使图像对角移动?
【发布时间】:2019-04-02 06:12:01
【问题描述】:

我被教导如何在 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()

【问题讨论】:

    标签: python image pygame move


    【解决方案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()
    

    【讨论】:

    • 非常感谢您的意见,因为这也是我一直在努力的工作。但是,当我按照你说的做时,它说我没有定义“x 和 y”——我该怎么做?抱歉,如果这似乎是一个简单的问题,我对 python 很陌生,但很想学习。谢谢!
    • @SummerNguyen 我已经编辑了我的答案,首先你必须将 x 和 y 设置为 0。
    • 我刚刚发布了我修改后的代码,如果你能看一下,我将不胜感激。再次感谢您的帮助,这对我来说意义重大。
    • @SummerNguyen 它不能工作你没有移动我编辑答案的对象
    猜你喜欢
    • 2019-06-26
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多