简体   繁体   English

pygame图像屏幕填充

[英]Pygame image screen fill

I have a pygame program that is meant to fill the pygame window with Grass.png: 我有一个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()

Only problem is, it only fills the first 100 pixel row with the image. 唯一的问题是,它仅用图像填充前100个像素行。 What is wrong with the code? 代码有什么问题? Thanks. 谢谢。

当您向下移动屏幕时,y轴为正-因此,当第一行的y位置为0时,下一行的y位置为100。基本上,您应该将y-坐标,而不是减法。

You're much better off using for loops rather than while loops to do that. 与for循环相比,使用for循环要好得多。

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

Makes the code much more readable and easier to debug. 使代码更具可读性,更易于调试。 But in answer to your question, like frr171 said, the origin point (0, 0) is in the top left corner of the screen. 但在回答你的问题,像frr171说,原点(0,0)在屏幕的左上角 As you go right, the x axis increases and as you go down the y axis increases. 右移时,x轴增加,而下移时 ,y轴增加。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM