Pygame如何显示空间入侵者的多个图像

2024-06-07 05:51:03 发布

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

我试图在Pygame中制作游戏《太空入侵者》,我试图展示我用颜料制作的图像。当我想添加第二个图片,wallImage,并添加显示表面.blit(wallike,(wallx,wally))我看到的只有wallike,而不是太空船图像。所以我只能在显示器上得到一张图像。我做错了什么?如何同时显示多个图像?在

import pygame, sys
from pygame.locals import *
pygame.init()

FPS = 60
fpsClock = pygame.time.Clock()

#display specs
display_width = 800
display_height = 600

#colors
black=(0,0,0)
blue=(0,0, 255)
green=(0,128,0)
red=(255,0,0)
white=(255,255,255)
yellow=(255,255,0)

#center of image
shipx = (display_width * 0.45)
shipy = (display_height * 0.8)

#movement ship is 0
shipxchange = 0

#width of spaceship
spaceshipwidth = 78

#location of the wall
wallx = 50
wally = 300

DISPLAYSURF = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Space Invaders')
spaceShipImage = pygame.image.load('spaceship7.png')
spaceshipRect = pygame.Rect(8,6, 71, 79) #rectangle of ship
wallImage = pygame.image.load('wall.png')
wallRect = pygame.Rect(0,0,58,17)

while True: # main game loop
    DISPLAYSURF.fill(white)
    DISPLAYSURF.blit(spaceShipImage, (shipx, shipy))
    DISPLAYSURF.blit(wallImage, (wallx, wally))
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN and event.key == K_LEFT:
            shipxchange += -5
        elif event.type == KEYDOWN and event.key == K_RIGHT:
            shipxchange += +5
        if event.type == KEYUP and event.key == K_LEFT:
            shipxchange += +5
        elif event.type == KEYUP and event.key == K_RIGHT:
            shipxchange += -5

    shipx += shipxchange

    #bounderies for the spaceship
    if shipx >= display_width - spaceshipwidth:
        shipx -= shipxchange
    if shipx < 0:
        shipx -= shipxchange

    pygame.display.update()
    fpsClock.tick(FPS)

enter image description here

enter image description here


Tags: andofkey图像eventiftypedisplay