Pygame获取滚动坐标

时间:2017-05-05 23:36:14

标签: python-3.x pygame pygame-surface

有没有办法让pygame表面的坐标“滚动”功能? e.g。

image.scroll(0,32)
scroll_coords = image.??? ### scroll_coords should be (0,32)

1 个答案:

答案 0 :(得分:0)

您可以将滚动坐标存储在矢量,列表或矩形中,每当滚动曲面时,也可以更新矢量。 (按w或s滚动曲面)

import sys
import pygame as pg


def main():
    clock = pg.time.Clock()
    screen = pg.display.set_mode((640, 480))

    image = pg.Surface((300, 300))
    image.fill((20, 100, 90))
    for i in range(10):
        pg.draw.rect(image, (160, 190, 120), (40*i, 30*i, 30, 30))

    scroll_coords = pg.math.Vector2(0, 0)

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_w:
                    scroll_coords.y -= 10
                    image.scroll(0, -10)
                elif event.key == pg.K_s:
                    scroll_coords.y += 10
                    image.scroll(0, 10)
                print(scroll_coords)

        screen.fill((50, 50, 50))
        screen.blit(image, (100, 100))

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()