皮加梅屏幕.blit()从非零y坐标

2024-06-16 09:16:14 发布

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

我想在屏幕上的草上画一个特定的水平[y],在那些我只想看到泥土的下面。这几乎是我所有的代码,删除的只是无关的东西

import pygame
from pygame import *

pygame.init()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))
height_of_grass = 150
run = True
speed_of_player = 1

player = pygame.image.load("images/steve.png")
grass = pygame.image.load("images/grass_block.jpg")
dirt = pygame.image.load("images/dirt_block.jpg")
sky = pygame.image.load("images/sky.png")
clouds = pygame.image.load("images/cloud.png")
oak_wood_log = pygame.image.load("images/oak_wood_log.png")
oak_leaves = pygame.image.load("images/oak_leaves.png")

keys = [False, False, False, False]
player_position = [100, 100]

while run:
    screen.fill((50, 168, 158))


    screen.blit(player, player_position)
    for x in range(int(width/grass.get_width()) + 1):
        screen.blit(grass, (x*grass.get_width(), height_of_grass))
    x = 0
    for x in range(int(width/dirt.get_width()) + 1):
        for y in range(height_of_grass, int(height / grass.get_height()) + 1):
           screen.blit(dirt, (x*dirt.get_width, y*dirt.get_height))
    pygame.display.flip()

    for event in pygame.event.get():
        if event == pygame.QUIT:
            run = False


    if player_position[0] < -16 or player_position[0] > (width + 16) or player_position[1] < -16 or player_position[1] > (height + 16):
        print("You´ve broke the game! Congratilations")
        exit(-1)

这只是代码的一部分,我有问题。我的pygame窗口只是没有显示污垢咆哮。你知道吗


Tags: ofimagefalsegetpngpositionloadwidth
2条回答

污垢不显示,因为内环的范围错误,并且污垢瓷砖的y位置计算不正确。你知道吗

计算污垢开始的y水平面和必须被污垢覆盖的区域的高度:

dirt_start_height = height_of_grass + grass.get_width()
all_dirt_height = height - dirt_start_height

在吸引污垢的嵌套循环中使用dirt_start_heightall_dirt_height

while run:

    # [...]

    dirt_start_height = height_of_grass + grass.get_width()
    all_dirt_height = height - dirt_start_height
    for x in range(int(width/dirt.get_width()) + 1):
        for y in range(int(all_dirt_height / grass.get_height()) + 1):
           screen.blit(dirt, (x*dirt.get_width(), dirt_start_height + y*dirt.get_height()))

在“污垢绘制循环”中,您看到的是grass.get_height()

for x in range(int(width/dirt.get_width()) + 1):
    for y in range(height_of_grass, int(height / grass.get_height()) + 1):
        screen.blit(dirt, (x*dirt.get_width(), y*dirt.get_height()))

我想你是想把dirt.get_height放在那里。你知道吗

相关问题 更多 >