Pygame图像屏幕fi

2024-04-25 19:15:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pygame程序,它将用Grass.png填充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个像素行。密码怎么了?谢谢。


Tags: fromimport程序eventpngdisplaysysscreen
2条回答

沿屏幕向下移动时,y轴为正,因此当第一行位于y位置0时,下一行位于y位置100。基本上,你应该加y坐标,而不是减。

使用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

相关问题 更多 >