我想在特定水平[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窗口只是没有显示波纹管。
答案 0 :(得分:3)
未显示污垢,因为内部循环的范围不正确,并且污垢块的y位置计算不正确。
计算污物开始的y高度和污物必须覆盖的区域的高度:
dirt_start_height
在绘制污垢的嵌套循环中使用all_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()))
:
appendChild
答案 1 :(得分:1)
在“污垢绘制循环”中,您正在查看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
放在那里。