2013-08-05 143 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坐标,而不是减去。