2013-08-05 137 views
1

我有是指具有Grass.png填補pygame的窗口pygame的程序:pygame的圖像填充屏幕上

import pygame, sys 
from pygame.locals import * 

pygame.init() 

screen = pygame.display.set_mode([600, 500]) 

def DrawBackground(background, xpos, ypos): 
    screen.blit(background, [xpos, ypos]) 

background = pygame.image.load('Grass.png') 
xpos = 0 
ypos = 0 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
    while ypos >= -500: 
     while xpos <= 600: 
      DrawBackground(background, xpos, ypos) 
      xpos += 100 
     ypos -= 100 

    pygame.display.flip() 

唯一的問題是,它僅填充與圖像的前100個像素行。代碼有什麼問題?謝謝。

回答

1

你使用for循環而不是while循環來做到這一點更好。

for y in range(5): 
    for x in range(6): 
     DrawBackground(background, x*100, y*100) 

使代碼更具可讀性,更易於調試。 但在回答你的問題,像frr171說,原點(0,0)是在左上角屏幕的一角。當你向右走時,x軸增加,並且隨着你走減小 y軸增加。

enter image description here

1

當你走下屏幕時,y軸是正值 - 所以當你的第一行在y位置爲0時,下一行將在100的y位置。基本上,你應該添加到y座標,而不是減去。