2017-05-05 121 views
1

有沒有什麼辦法可以獲得pygame表面「滾動」功能的座標? 例如pygame得到滾動座標

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

回答

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()