gpt4 book ai didi

python - Pygame 图像屏幕填充

转载 作者:行者123 更新时间:2023-12-01 05:39:12 24 4
gpt4 key购买 nike

我有一个 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 像素行。代码有什么问题吗?谢谢。

最佳答案

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

关于python - Pygame 图像屏幕填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18067219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com