【问题标题】:Can't move a rectangle in pygame无法在pygame中移动矩形
【发布时间】:2016-11-11 17:28:34
【问题描述】:

我已经用 pygame 练习了一段时间,但我无法移动我绘制的矩形。

import pygame
pygame.init()
screen_dimensions=[600,600]
rectx=0
rect=pygame.Rect(rectx,550,50,50)
screen = pygame.display.set_mode(screen_dimensions)
running=True
color = (255, 255, 255)
rect_color=(255,0,0)
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running=False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                rectx += 100
    screen.fill(color)
    pygame.draw.rect(screen, rect_color, rect)
    pygame.display.update()

【问题讨论】:

  • 每次绘制时都必须重新创建rect
  • 谢谢,成功了!

标签: python python-2.7 pygame


【解决方案1】:

您必须更新rect.x 而不是rectx

rectx += 100

rect.x += 100

或者,您可以在更改rectx后更新矩形的位置:

rectx += 100
rect.x += rectx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 2016-06-19
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多