【问题标题】:Making image move straight with pygame使用 pygame 使图像直线移动
【发布时间】:2014-07-11 17:24:54
【问题描述】:

我必须通过 pygame 窗口直接移动矩形对象。我用pygame尝试了一些代码。代码是

import pygame
from itertools import cycle

pygame.init() 
screen = pygame.display.set_mode((300, 300)) 
s_r = screen.get_rect()
player = pygame.Rect((100, 100, 50, 50))

timer = pygame.time.Clock()

movement = "straight"

x = 0

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise

    if movement == 'straight':
      x += 50

    screen.fill(pygame.color.Color('Black'))
    pygame.draw.rect(screen, pygame.color.Color('Grey'), player)

    pygame.display.flip()
    timer.tick(25)

这里的图像没有移动。我需要的是图像必须直线移动。

【问题讨论】:

  • 你能修正你的缩进吗?首先,您更改了x,但您从未使用它。看看这个:pygame.org/docs/ref/rect.html。此外,每次循环时,都需要重新绘制矩形。
  • @MattShank,我已回滚您的编辑。由于 python 是一种缩进非常重要的语言,因此对它的任何修改都应该由 OP 完成。您要求他们澄清是正确的,只是您自己没有这样做。缩进可能是他们问题的一部分,并且为他们“纠正”它可能会使问题本身无效。在这种情况下,我并不是说这是真的,但是当涉及到 python 和其他有空格意味着什么的语言时,不要乱用缩进。
  • @indivisible,明白了。
  • @user3830347 下次在代码上使用按钮 {} 来保持缩进问题。

标签: python pygame pygame-surface


【解决方案1】:

x正在添加,但不影响player,实际上影响了矩形的绘制。

import pygame
from itertools import cycle

pygame.init() 
screen = pygame.display.set_mode((300, 300)) 
s_r = screen.get_rect()


timer = pygame.time.Clock()

movement = "straight"

x = 0
player = pygame.Rect((x, 100, 50, 50))

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise

    if movement == 'straight':
      x += 10
      player = pygame.Rect((x, 100, 50, 50))

    if x >= 300:
      x = 0





    screen.fill(pygame.color.Color('Black'))
    pygame.draw.rect(screen, pygame.color.Color('Grey'), player)

    pygame.display.flip()
    timer.tick(25)

【讨论】:

  • 这不是一个有效的答案..请发布答案以使矩形与 pygame 一起移动..我只是一个初学者..所以我不太了解..如果你知道如何直接移动它..请帮助我
  • 它很有帮助..但是当它直线移动时它会离开屏幕..我怎样才能从中间再次开始..就像在点击屏幕后一次又一次地做同样的过程..
  • 您需要将x 与您的窗口宽度进行比较。如果x 大于窗口的宽度,您可以将x 重置为 0 或 100,或者您希望它开始的任何位置。
  • 您可以使用player.x += 10。您不必每次都创建Rect()
【解决方案2】:

每次更改x 时,都需要调整player 矩形。从http://www.pygame.org/docs/ref/rect.html,可以看到前两个参数是“left”和“top”。所以,如果你想让矩形从左向右移动,你会想要这样的:

player = pygame.Rect((100 + x, 100, 50, 50))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)

【讨论】:

    【解决方案3】:
    import pygame
    
    BLACK = pygame.color.Color('Black')
    GREY  = pygame.color.Color('Grey')
    
    pygame.init() 
    
    screen = pygame.display.set_mode((300, 300)) 
    screen_rect = screen.get_rect()
    
    timer = pygame.time.Clock()
    
    movement = "straight"
    
    player = pygame.Rect(0, 100, 50, 50) # four aguments in place of tuple (,,,)
    
    running = True
    
    while running:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        if movement == 'straight':
            player.x += 10
            if player.x >= 300: # check only when `player.x` was changed 
                player.x = 0
    
        screen.fill(BLACK)
        pygame.draw.rect(screen, GREY, player)
    
        pygame.display.flip()
    
        timer.tick(25)
    

    顺便说一句:

    • 不要使用raise 退出程序。
    • 使用可读变量 - 不是s_r,而是screen_rect
    • 你不需要x - 你有player.x
    • 您只能创建一次矩形
    • 向问题添加代码时删除重复的空行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-26
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多