Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

python - How to add a background image into pygame?

new to pygame just wondering how i would go about adding a background image into the game itself? this is my code so far, i've been using the bg as a way to import my image but the py file itself refuses to load up.

import pygame
import sys
from pygame.locals import *

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

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


pygame.mouse.set_visible(0)

ship = pygame.image.load("imagesship.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("imagesshot.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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For background I always make an image the size of my game window or smaller then before all of the images are displayed, I blit that image to 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.

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...