gpt4 book ai didi

python - 如何在pygame中添加背景图像?

转载 作者:行者123 更新时间:2023-12-04 13:16:08 26 4
gpt4 key购买 nike

pygame 新手只是想知道我将如何将背景图像添加到游戏本身中?到目前为止,这是我的代码,我一直在使用 bg 作为导入图像的一种方式,但 py 文件本身拒绝加载。

import pygame
import sys
from pygame.locals import *

clock = pygame.time.Clock()
screen = pygame.display.set_mode((600,500))

bg = pygame.image.load("images\space.png")


pygame.mouse.set_visible(0)

ship = pygame.image.load("images\ship.png")
ship_top = screen.get_height() - ship.get_height()
ship_left = screen.get_width()/2 - ship.get_width()/2

screen.blit(ship, (ship_left,ship_top))

shot = pygame.image.load("images\shot.png")
shoot_y = 0


pygame.display.set_caption('galaxy invaders')

while True:
clock.tick(60)
screen.fill((r,0,0))
screen.blit(bg.(0,0))
x,y = pygame.mouse.get_pos()
screen.blit(ship, (x-ship.get_width()/2, ship_top))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
shoot_y = 500
shoot_x = x

if shoot_y > 0:
screen.blit(shot, (shoot_x, shoot_y))
shoot_y -= 10

pygame.display.update()

最佳答案

对于背景,我总是制作一个与我的游戏窗口大小相同或更小的图像,然后在显示所有图像之前,将该图像blit 到 0,0。

bg = pygame.image.load("bg.png")

#INSIDE OF THE GAME LOOP
gameDisplay.blit(bg, (0, 0))

#REST OF ITEMS ARE BLIT'D TO SCREEN.

希望这可以帮助。

关于python - 如何在pygame中添加背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28005641/

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