Pygame 如何知道第 1 层(背景)与屏幕上显示的精灵是什么?
How does Pygame know what is layer 1 (background) vs the sprites displayed on the screen?
我的老师今天正在查看我的代码,要我向他解释为什么我可以使用
WIDTH, HEIGHT = 900, 500
PLAYER, SIZE = 34, 34
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
Player_1 = pygame.transform.scale(pygame.image.load(os.path.join('skins', 'player.png')), (PLAYER,SIZE))
P1 = pygame.transform.rotate(Player_1, 270)
Player_2 = pygame.transform.scale(pygame.image.load(os.path.join('skins', 'enemy.png')), (PLAYER,SIZE))
P2 = pygame.transform.rotate(Player_2, 270)
SKY = pygame.transform.scale(pygame.image.load(os.path.join('skins', 'bg.png')), (WIDTH,HEIGHT))
#this will search our folder 'skins' for the file 'bg' to make our background
BULLET_SPEED = 5
BULLET_NUMBER = 5
BULLET_FIRE_SOUND = pygame.mixer.Sound(os.path.join('soundEffects', 'tiro.wav'))
BULLET_DAMAGE_SOUND = pygame.mixer.Sound(os.path.join('soundEffects', 'batida.wav'))
def draw(yellow, blue, yellow_bullets, blue_bullets, yellow_health, blue_health):
WIN.blit(SKY, (0,0))
pygame.draw.rect(WIN, WHITE, MID_BORDER)
WIN.blit(P1, (yellow.x, yellow.y))
WIN.blit(P2, (blue.x, blue.y))
#pygame starts tracking at the top left with 0,0
for bullet in blue_bullets:
pygame.draw.rect(WIN, BLUE, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
yellow_health_txt = HEALTH_FONT.render("Health: " + str(yellow_health), 1, WHITE)
blue_health_txt = HEALTH_FONT.render("Health: " + str(blue_health), 1, WHITE)
WIN.blit(yellow_health_txt,(10,10))
WIN.blit(blue_health_txt,(WIDTH - blue_health_txt.get_width() - 10, 10))
pygame.display.update()
和 pygame 知道将 bg.png
文件设为背景,将 player/enemy.png
设为前景。
我解释说这可能是由于 bg.png
采用了 HEIGHT
和 WIDTH
arg,因此拉伸了整个屏幕,但几乎立即我就发现了这个逻辑中的一个缺陷 player/enemy.png
很容易位于 bg.png
后面并完全隐藏。
很明显,它并没有按照我最后声明 bg.png
时的顺序进行声明。
那么,有什么方法可以理解 pygame 如何区分背景和前景背后的逻辑,或者它是否更深入
即:layer {1..100+}
很抱歉,如果这是一个愚蠢的问题,或者我在研究时找不到答案,我很抱歉。
编辑
经过进一步审查,我想我也知道我在左上角 (0,0)
处初始化了 SKY
。
这就是定义为背景的东西吗? (yellow.x, yellow.y)
是表示我的精灵出现的位置吗?
Pygame 按照 .blit()
的顺序绘制表面,因此 SKY
(即 bg.png
图像)首先被 blit,它是背景。
我的老师今天正在查看我的代码,要我向他解释为什么我可以使用
WIDTH, HEIGHT = 900, 500
PLAYER, SIZE = 34, 34
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
Player_1 = pygame.transform.scale(pygame.image.load(os.path.join('skins', 'player.png')), (PLAYER,SIZE))
P1 = pygame.transform.rotate(Player_1, 270)
Player_2 = pygame.transform.scale(pygame.image.load(os.path.join('skins', 'enemy.png')), (PLAYER,SIZE))
P2 = pygame.transform.rotate(Player_2, 270)
SKY = pygame.transform.scale(pygame.image.load(os.path.join('skins', 'bg.png')), (WIDTH,HEIGHT))
#this will search our folder 'skins' for the file 'bg' to make our background
BULLET_SPEED = 5
BULLET_NUMBER = 5
BULLET_FIRE_SOUND = pygame.mixer.Sound(os.path.join('soundEffects', 'tiro.wav'))
BULLET_DAMAGE_SOUND = pygame.mixer.Sound(os.path.join('soundEffects', 'batida.wav'))
def draw(yellow, blue, yellow_bullets, blue_bullets, yellow_health, blue_health):
WIN.blit(SKY, (0,0))
pygame.draw.rect(WIN, WHITE, MID_BORDER)
WIN.blit(P1, (yellow.x, yellow.y))
WIN.blit(P2, (blue.x, blue.y))
#pygame starts tracking at the top left with 0,0
for bullet in blue_bullets:
pygame.draw.rect(WIN, BLUE, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
yellow_health_txt = HEALTH_FONT.render("Health: " + str(yellow_health), 1, WHITE)
blue_health_txt = HEALTH_FONT.render("Health: " + str(blue_health), 1, WHITE)
WIN.blit(yellow_health_txt,(10,10))
WIN.blit(blue_health_txt,(WIDTH - blue_health_txt.get_width() - 10, 10))
pygame.display.update()
和 pygame 知道将 bg.png
文件设为背景,将 player/enemy.png
设为前景。
我解释说这可能是由于 bg.png
采用了 HEIGHT
和 WIDTH
arg,因此拉伸了整个屏幕,但几乎立即我就发现了这个逻辑中的一个缺陷 player/enemy.png
很容易位于 bg.png
后面并完全隐藏。
很明显,它并没有按照我最后声明 bg.png
时的顺序进行声明。
那么,有什么方法可以理解 pygame 如何区分背景和前景背后的逻辑,或者它是否更深入
即:layer {1..100+}
很抱歉,如果这是一个愚蠢的问题,或者我在研究时找不到答案,我很抱歉。
编辑
经过进一步审查,我想我也知道我在左上角 (0,0)
处初始化了 SKY
。
这就是定义为背景的东西吗? (yellow.x, yellow.y)
是表示我的精灵出现的位置吗?
Pygame 按照 .blit()
的顺序绘制表面,因此 SKY
(即 bg.png
图像)首先被 blit,它是背景。