如何在pygame中添加背景图片? [英] How to add a background image into pygame?

查看:3867
本文介绍了如何在pygame中添加背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()

解决方案

对于背景,我总是使图像的大小等于我的游戏窗口的大小或更小,然后在显示所有图像之前,我将该图像设为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.

希望这会有所帮助.

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("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()

解决方案

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.

这篇关于如何在pygame中添加背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆