【问题标题】:Pygame: Image not movingPygame:图像不动
【发布时间】:2017-08-18 16:08:07
【问题描述】:

我一直在从 pythonprogramming.net 学习使用 PyGame。我目前正在窗口上移动图像:

https://pythonprogramming.net/pygame-tutorial-moving-images-key-input/?completed=/displaying-images-pygame/

这是我正在运行的代码,以防您在查看时网站已关闭。由于某种原因,我不知道为什么,汽车图像没有移动。我在观看视频时已经输入了所有内容,当我发现它不起作用时,我复制并粘贴了仍然无法获得预期的性能。我打印出要调试的事件(这是我添加的唯一一行,不在从网站复制的代码上)。

import pygame

pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')

def car(x,y):
    gameDisplay.blit(carImg, (x,y))

x =  (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
car_speed = 0

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        ############################
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -5
            elif event.key == pygame.K_RIGHT:
                x_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0
        ######################
    ##
    x += x_change
   ##         
    gameDisplay.fill(white)
    car(x,y)

    print(event) # added for debugging

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

我使用了 Spyder,但弹出的窗口冻结了我,我不得不在我的 Mac 上退出 Spyder。我切换到 PyCharm 和终端,发现两者都没有冻结但仍然没有动作。

终端上的打印输出是:

libpng warning: iCCP: known incorrect sRGB profile
<Event(4-MouseMotion {'pos': (364, 569), 'rel': (364, 569), 'buttons': (0, 0, 0)})>
<Event(5-MouseButtonDown {'pos': (364, 569), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (364, 569), 'button': 1})>
<Event(4-MouseMotion {'pos': (491, 27), 'rel': (127, -542), 'buttons': (0, 0, 0)})>
^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
^[[D^[[D^[[D<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
^[[D^[[D^[[D^[[C^[[C<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (491, 27), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (491, 27), 'button': 1})>
<Event(12-Quit {})>

我了解代码的第一行是来自How do I disable the libpng warning? (python, pygame) 的警告,不应以这种方式影响代码。

【问题讨论】:

  • 代码对我来说也可以正常工作。您是否可能尝试使用鼠标按钮移动汽车(因为您只显示MouseButtonDown/Up 事件)?您应该使用箭头键移动它。

标签: python python-3.x pygame pycharm spyder


【解决方案1】:

对不起,我想他们已经改变了一些东西,因为我正在阅读文档。它改变了很多,我彻底道歉。我想指出的一件事是,在游戏开始之前填充背景是一种很好的做法。我将对此进行更多研究,我将自己进行测试。

编辑:我刚刚测试过它,对我来说它工作得很好。这可能是您的键盘布局,但我唯一的想法是,您选择了窗口吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多